Skip to content

Commit 39ff5fd

Browse files
authored
Merge pull request #11 from ember-codemods/tests
Add Tests
2 parents 9552a00 + 1a44a8a commit 39ff5fd

4 files changed

Lines changed: 369 additions & 21 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`basic 1`] = `
4+
"==========
5+
6+
export default Component.extend({
7+
});
8+
9+
~~~~~~~~~~
10+
foo
11+
~~~~~~~~~~
12+
=> tagName: div
13+
~~~~~~~~~~
14+
15+
export default Component.extend({
16+
tagName: \\"\\"
17+
});
18+
19+
~~~~~~~~~~
20+
<div ...attributes>foo</div>
21+
=========="
22+
`;
23+
24+
exports[`handles \`attributeBindings\` correctly 1`] = `
25+
"==========
26+
27+
export default Component.extend({
28+
attributeBindings: ['foo', 'bar:baz'],
29+
});
30+
31+
~~~~~~~~~~
32+
foo
33+
~~~~~~~~~~
34+
=> tagName: div
35+
~~~~~~~~~~
36+
37+
export default Component.extend({
38+
tagName: \\"\\",
39+
});
40+
41+
~~~~~~~~~~
42+
<div foo={{this.foo}} bar={{this.baz}} ...attributes>foo</div>
43+
=========="
44+
`;
45+
46+
exports[`handles \`classNameBindings\` correctly 1`] = `
47+
"==========
48+
49+
export default Component.extend({
50+
classNameBindings: ['a:b', 'x:y:z', 'foo::bar'],
51+
});
52+
53+
~~~~~~~~~~
54+
foo
55+
~~~~~~~~~~
56+
=> tagName: div
57+
~~~~~~~~~~
58+
59+
export default Component.extend({
60+
tagName: \\"\\",
61+
});
62+
63+
~~~~~~~~~~
64+
<div class=\\"{{if this.a \\"b\\"}} {{if this.x \\"y\\" \\"z\\"}} {{unless this.foo \\"bar\\"}}\\" ...attributes>foo</div>
65+
=========="
66+
`;
67+
68+
exports[`handles \`classNames\` correctly 1`] = `
69+
"==========
70+
71+
export default Component.extend({
72+
classNames: ['foo', 'bar:baz'],
73+
});
74+
75+
~~~~~~~~~~
76+
foo
77+
~~~~~~~~~~
78+
=> tagName: div
79+
~~~~~~~~~~
80+
81+
export default Component.extend({
82+
tagName: \\"\\",
83+
});
84+
85+
~~~~~~~~~~
86+
<div class=\\"foo bar:baz\\" ...attributes>foo</div>
87+
=========="
88+
`;
89+
90+
exports[`handles \`elementId\` correctly 1`] = `
91+
"==========
92+
93+
export default Component.extend({
94+
elementId: 'qux',
95+
});
96+
97+
~~~~~~~~~~
98+
foo
99+
~~~~~~~~~~
100+
=> tagName: div
101+
~~~~~~~~~~
102+
103+
export default Component.extend({
104+
tagName: \\"\\",
105+
});
106+
107+
~~~~~~~~~~
108+
<div id=\\"qux\\" ...attributes>foo</div>
109+
=========="
110+
`;
111+
112+
exports[`handles single \`classNames\` item correctly 1`] = `
113+
"==========
114+
115+
export default Component.extend({
116+
classNames: ['foo'],
117+
});
118+
119+
~~~~~~~~~~
120+
foo
121+
~~~~~~~~~~
122+
=> tagName: div
123+
~~~~~~~~~~
124+
125+
export default Component.extend({
126+
tagName: \\"\\",
127+
});
128+
129+
~~~~~~~~~~
130+
<div class=\\"foo\\" ...attributes>foo</div>
131+
=========="
132+
`;
133+
134+
exports[`replaces existing \`tagName\` 1`] = `
135+
"==========
136+
137+
export default Component.extend({
138+
tagName: 'span',
139+
});
140+
141+
~~~~~~~~~~
142+
foo
143+
~~~~~~~~~~
144+
=> tagName: span
145+
~~~~~~~~~~
146+
147+
export default Component.extend({
148+
tagName: \\"\\",
149+
});
150+
151+
~~~~~~~~~~
152+
<span ...attributes>foo</span>
153+
=========="
154+
`;

lib/__tests__/transform.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
const { transform } = require('../transform');
2+
3+
function generateSnapshot(source, template) {
4+
let result = transform(source, template);
5+
6+
return `==========
7+
${source}
8+
~~~~~~~~~~
9+
${template}
10+
~~~~~~~~~~
11+
=> tagName: ${result.tagName}
12+
~~~~~~~~~~
13+
${result.source}
14+
~~~~~~~~~~
15+
${result.template}
16+
==========`;
17+
}
18+
19+
test('basic', () => {
20+
let source = `
21+
export default Component.extend({
22+
});
23+
`;
24+
25+
let template = `foo`;
26+
27+
expect(generateSnapshot(source, template)).toMatchSnapshot();
28+
});
29+
30+
test('replaces existing `tagName`', () => {
31+
let source = `
32+
export default Component.extend({
33+
tagName: 'span',
34+
});
35+
`;
36+
37+
let template = `foo`;
38+
39+
expect(generateSnapshot(source, template)).toMatchSnapshot();
40+
});
41+
42+
test('handles `elementId` correctly', () => {
43+
let source = `
44+
export default Component.extend({
45+
elementId: 'qux',
46+
});
47+
`;
48+
49+
let template = `foo`;
50+
51+
expect(generateSnapshot(source, template)).toMatchSnapshot();
52+
});
53+
54+
test('handles `attributeBindings` correctly', () => {
55+
let source = `
56+
export default Component.extend({
57+
attributeBindings: ['foo', 'bar:baz'],
58+
});
59+
`;
60+
61+
let template = `foo`;
62+
63+
expect(generateSnapshot(source, template)).toMatchSnapshot();
64+
});
65+
66+
test('handles `classNames` correctly', () => {
67+
let source = `
68+
export default Component.extend({
69+
classNames: ['foo', 'bar:baz'],
70+
});
71+
`;
72+
73+
let template = `foo`;
74+
75+
expect(generateSnapshot(source, template)).toMatchSnapshot();
76+
});
77+
78+
test('handles single `classNames` item correctly', () => {
79+
let source = `
80+
export default Component.extend({
81+
classNames: ['foo'],
82+
});
83+
`;
84+
85+
let template = `foo`;
86+
87+
expect(generateSnapshot(source, template)).toMatchSnapshot();
88+
});
89+
90+
test('handles `classNameBindings` correctly', () => {
91+
let source = `
92+
export default Component.extend({
93+
classNameBindings: ['a:b', 'x:y:z', 'foo::bar'],
94+
});
95+
`;
96+
97+
let template = `foo`;
98+
99+
expect(generateSnapshot(source, template)).toMatchSnapshot();
100+
});
101+
102+
test('throws if `Component.extend({ ... })` is not found', () => {
103+
let source = `
104+
export default class extends Component {
105+
}
106+
`;
107+
108+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
109+
`"Could not find \`export default Component.extend({ ... });\`"`
110+
);
111+
});
112+
113+
test('throws if `Component.extend({ ... })` argument is not found', () => {
114+
let source = `
115+
export default Component.extend();
116+
`;
117+
118+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
119+
`"Could not find object argument in \`export default Component.extend({ ... });\`"`
120+
);
121+
});
122+
123+
test('skips tagless components', () => {
124+
let source = `
125+
export default Component.extend({
126+
tagName: '',
127+
});
128+
`;
129+
130+
let template = 'foo';
131+
132+
let result = transform(source, template);
133+
expect(result.tagName).toEqual(undefined);
134+
expect(result.source).toEqual(source);
135+
expect(result.template).toEqual(template);
136+
});
137+
138+
test('throws if component is using `this.element`', () => {
139+
let source = `
140+
export default Component.extend({
141+
didInsertElement() {
142+
console.log(this.element);
143+
},
144+
});
145+
`;
146+
147+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
148+
`"Using \`this.element\` is not supported in tagless components"`
149+
);
150+
});
151+
152+
test('throws if component is using `keyDown()`', () => {
153+
let source = `
154+
export default Component.extend({
155+
keyDown() {
156+
console.log('Hello World!');
157+
},
158+
});
159+
`;
160+
161+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
162+
`"Using \`keyDown()\` is not supported in tagless components"`
163+
);
164+
});
165+
166+
test('throws if component is using `click()`', () => {
167+
let source = `
168+
export default Component.extend({
169+
click() {
170+
console.log('Hello World!');
171+
},
172+
});
173+
`;
174+
175+
expect(() => transform(source, '')).toThrowErrorMatchingInlineSnapshot(
176+
`"Using \`click()\` is not supported in tagless components"`
177+
);
178+
});

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')('tagless-ember-components-codemod');
44
const chalk = require('chalk');
55

66
const SilentError = require('./silent-error');
7-
const { transform } = require('./transform');
7+
const { transformPath } = require('./transform');
88

99
async function run() {
1010
let path = await pkgDir();
@@ -24,7 +24,7 @@ async function runForPath(path, options = {}) {
2424

2525
for (let path of paths) {
2626
try {
27-
let tagName = transform(path);
27+
let tagName = transformPath(path);
2828
if (tagName) {
2929
log(chalk.green(`${chalk.dim(path)}: <${tagName}>...</${tagName}>`));
3030
} else {

0 commit comments

Comments
 (0)