|
| 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 | +}); |
0 commit comments