|
| 1 | +import { transform } from '@babel/core' |
| 2 | +import plugin from '.' |
| 3 | + |
| 4 | +const testPlugin = (code, options) => { |
| 5 | + const result = transform(code, { |
| 6 | + plugins: ['@babel/plugin-syntax-jsx', [plugin, options]], |
| 7 | + configFile: false, |
| 8 | + }) |
| 9 | + |
| 10 | + return result.code |
| 11 | +} |
| 12 | + |
| 13 | +describe('plugin', () => { |
| 14 | + it('should add simple attribute', () => { |
| 15 | + expect( |
| 16 | + testPlugin('<div />', { |
| 17 | + elements: ['div'], |
| 18 | + attributes: [{ name: 'disabled' }], |
| 19 | + }), |
| 20 | + ).toMatchInlineSnapshot(`"<div disabled />;"`) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should add attribute with value', () => { |
| 24 | + expect( |
| 25 | + testPlugin('<div />', { |
| 26 | + elements: ['div'], |
| 27 | + attributes: [{ name: 'disabled', value: true }], |
| 28 | + }), |
| 29 | + ).toMatchInlineSnapshot(`"<div disabled={true} />;"`) |
| 30 | + expect( |
| 31 | + testPlugin('<div />', { |
| 32 | + elements: ['div'], |
| 33 | + attributes: [{ name: 'disabled', value: 'true' }], |
| 34 | + }), |
| 35 | + ).toMatchInlineSnapshot(`"<div disabled=\\"true\\" />;"`) |
| 36 | + |
| 37 | + expect( |
| 38 | + testPlugin('<div />', { |
| 39 | + elements: ['div'], |
| 40 | + attributes: [{ name: 'disabled', value: 200 }], |
| 41 | + }), |
| 42 | + ).toMatchInlineSnapshot(`"<div disabled={200} />;"`) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should add literal attribute', () => { |
| 46 | + expect( |
| 47 | + testPlugin('<div />', { |
| 48 | + elements: ['div'], |
| 49 | + attributes: [{ name: 'ref', value: 'ref', literal: true }], |
| 50 | + }), |
| 51 | + ).toMatchInlineSnapshot(`"<div ref={ref} />;"`) |
| 52 | + |
| 53 | + expect( |
| 54 | + testPlugin('<div />', { |
| 55 | + elements: ['div'], |
| 56 | + attributes: [{ name: 'ref', value: 'ref ? ref : null', literal: true }], |
| 57 | + }), |
| 58 | + ).toMatchInlineSnapshot(`"<div ref={ref ? ref : null} />;"`) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should add spread attribute', () => { |
| 62 | + expect( |
| 63 | + testPlugin('<div foo><span /></div>', { |
| 64 | + elements: ['div'], |
| 65 | + attributes: [ |
| 66 | + { |
| 67 | + spread: true, |
| 68 | + name: 'props', |
| 69 | + position: 'start', |
| 70 | + }, |
| 71 | + ], |
| 72 | + }), |
| 73 | + ).toMatchInlineSnapshot(`"<div {...props} foo><span /></div>;"`) |
| 74 | + |
| 75 | + expect( |
| 76 | + testPlugin('<div><span foo="bar" /></div>', { |
| 77 | + elements: ['span'], |
| 78 | + attributes: [ |
| 79 | + { |
| 80 | + spread: true, |
| 81 | + name: 'props', |
| 82 | + position: 'end', |
| 83 | + }, |
| 84 | + ], |
| 85 | + }), |
| 86 | + ).toMatchInlineSnapshot(`"<div><span foo=\\"bar\\" {...props} /></div>;"`) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should replace attribute', () => { |
| 90 | + expect( |
| 91 | + testPlugin('<div disabled />', { |
| 92 | + elements: ['div'], |
| 93 | + attributes: [{ name: 'disabled', value: false }], |
| 94 | + }), |
| 95 | + ).toMatchInlineSnapshot(`"<div disabled={false} />;"`) |
| 96 | + }) |
| 97 | +}) |
0 commit comments