|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import formatReactFragmentNode from './formatReactFragmentNode'; |
| 4 | + |
| 5 | +const defaultOptions = { |
| 6 | + filterProps: [], |
| 7 | + showDefaultProps: true, |
| 8 | + showFunctions: false, |
| 9 | + tabStop: 2, |
| 10 | + useBooleanShorthandSyntax: true, |
| 11 | + useFragmentShortSyntax: true, |
| 12 | + sortProps: true, |
| 13 | +}; |
| 14 | + |
| 15 | +describe('formatReactFragmentNode', () => { |
| 16 | + it('should format a react fragment with a string as children', () => { |
| 17 | + const tree = { |
| 18 | + type: 'ReactFragment', |
| 19 | + childrens: [ |
| 20 | + { |
| 21 | + value: 'Hello world', |
| 22 | + type: 'string', |
| 23 | + }, |
| 24 | + ], |
| 25 | + }; |
| 26 | + |
| 27 | + expect(formatReactFragmentNode(tree, false, 0, defaultOptions)).toEqual( |
| 28 | + `<> |
| 29 | + Hello world |
| 30 | +</>` |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should format a react fragment with a key', () => { |
| 35 | + const tree = { |
| 36 | + type: 'ReactFragment', |
| 37 | + key: 'foo', |
| 38 | + childrens: [ |
| 39 | + { |
| 40 | + value: 'Hello world', |
| 41 | + type: 'string', |
| 42 | + }, |
| 43 | + ], |
| 44 | + }; |
| 45 | + |
| 46 | + expect(formatReactFragmentNode(tree, false, 0, defaultOptions)).toEqual( |
| 47 | + `<React.Fragment key="foo"> |
| 48 | + Hello world |
| 49 | +</React.Fragment>` |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should format a react fragment with multiple childrens', () => { |
| 54 | + const tree = { |
| 55 | + type: 'ReactFragment', |
| 56 | + childrens: [ |
| 57 | + { |
| 58 | + type: 'ReactElement', |
| 59 | + displayName: 'div', |
| 60 | + props: { a: 'foo' }, |
| 61 | + childrens: [], |
| 62 | + }, |
| 63 | + { |
| 64 | + type: 'ReactElement', |
| 65 | + displayName: 'div', |
| 66 | + props: { b: 'bar' }, |
| 67 | + childrens: [], |
| 68 | + }, |
| 69 | + ], |
| 70 | + }; |
| 71 | + |
| 72 | + expect(formatReactFragmentNode(tree, false, 0, defaultOptions)).toEqual( |
| 73 | + `<> |
| 74 | + <div a="foo" /> |
| 75 | + <div b="bar" /> |
| 76 | +</>` |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should format an empty react fragment', () => { |
| 81 | + const tree = { |
| 82 | + type: 'ReactFragment', |
| 83 | + childrens: [], |
| 84 | + }; |
| 85 | + |
| 86 | + expect(formatReactFragmentNode(tree, false, 0, defaultOptions)).toEqual( |
| 87 | + '<React.Fragment />' |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should format an empty react fragment with key', () => { |
| 92 | + const tree = { |
| 93 | + type: 'ReactFragment', |
| 94 | + key: 'foo', |
| 95 | + childrens: [], |
| 96 | + }; |
| 97 | + |
| 98 | + expect(formatReactFragmentNode(tree, false, 0, defaultOptions)).toEqual( |
| 99 | + '<React.Fragment key="foo" />' |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should format a react fragment using the explicit syntax', () => { |
| 104 | + const tree = { |
| 105 | + type: 'ReactFragment', |
| 106 | + childrens: [ |
| 107 | + { |
| 108 | + value: 'Hello world', |
| 109 | + type: 'string', |
| 110 | + }, |
| 111 | + ], |
| 112 | + }; |
| 113 | + |
| 114 | + expect( |
| 115 | + formatReactFragmentNode(tree, false, 0, { |
| 116 | + ...defaultOptions, |
| 117 | + ...{ useFragmentShortSyntax: false }, |
| 118 | + }) |
| 119 | + ).toEqual( |
| 120 | + `<React.Fragment> |
| 121 | + Hello world |
| 122 | +</React.Fragment>` |
| 123 | + ); |
| 124 | + }); |
| 125 | +}); |
0 commit comments