|
| 1 | +const compiler = require('./compiler.js'); |
| 2 | + |
| 3 | +describe('combining multiple classes', () => { |
| 4 | + const style = '<style>span.red.large:hover { font-size: 20px; } \n.red { color: red; }</style>'; |
| 5 | + const source = style + '\n<span class="$style.red $style.large">Red</span>'; |
| 6 | + |
| 7 | + const expectedStyle = |
| 8 | + '<style>:global(span.red-123456.large-123456:hover) { font-size: 20px; } \n:global(.red-123456) { color: red; }</style>'; |
| 9 | + const expectedOutput = expectedStyle + '\n<span class="red-123456 large-123456">Red</span>'; |
| 10 | + |
| 11 | + test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => { |
| 12 | + const output = await compiler( |
| 13 | + { |
| 14 | + source, |
| 15 | + }, |
| 16 | + { |
| 17 | + localIdentName: '[local]-123456', |
| 18 | + } |
| 19 | + ); |
| 20 | + |
| 21 | + expect(output).toBe(expectedOutput); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('Classname is part of a selector', () => { |
| 26 | + |
| 27 | + test('CSS Modules class targetting children', async () => { |
| 28 | + const source = |
| 29 | + '<style>\n' + |
| 30 | + 'div.red > sup { font-size: 12px; }\n' + |
| 31 | + '.red { color: red; }\n' + |
| 32 | + '</style>\n' + |
| 33 | + '<div class="$style.red">Red<sup>*</sup></div>'; |
| 34 | + |
| 35 | + const expectedOutput = |
| 36 | + '<style>\n' + |
| 37 | + ':global(div.red-123) > sup { font-size: 12px; }\n' + |
| 38 | + ':global(.red-123) { color: red; }\n' + |
| 39 | + '</style>\n' + |
| 40 | + '<div class="red-123">Red<sup>*</sup></div>'; |
| 41 | + |
| 42 | + const output = await compiler( |
| 43 | + { |
| 44 | + source, |
| 45 | + }, |
| 46 | + { |
| 47 | + localIdentName: '[local]-123', |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + expect(output).toBe(expectedOutput); |
| 52 | + }); |
| 53 | + |
| 54 | + test('CSS Modules class has a parent', async () => { |
| 55 | + const source = |
| 56 | + '<style>\n' + |
| 57 | + 'div .semibold .red { font-size: 20px; }\n' + |
| 58 | + '.red { color: red; }\n' + |
| 59 | + '.semibold { font-weight: 600; }\n' + |
| 60 | + '</style>\n' + |
| 61 | + '<div><strong class="$style.semibold"><span class="$style.red">Red</span></strong></div>'; |
| 62 | + |
| 63 | + const expectedOutput = |
| 64 | + '<style>\n' + |
| 65 | + 'div :global(.semibold-123) :global(.red-123) { font-size: 20px; }\n' + |
| 66 | + ':global(.red-123) { color: red; }\n' + |
| 67 | + ':global(.semibold-123) { font-weight: 600; }\n' + |
| 68 | + '</style>\n' + |
| 69 | + '<div><strong class="semibold-123"><span class="red-123">Red</span></strong></div>'; |
| 70 | + |
| 71 | + const output = await compiler( |
| 72 | + { |
| 73 | + source, |
| 74 | + }, |
| 75 | + { |
| 76 | + localIdentName: '[local]-123', |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + expect(output).toBe(expectedOutput); |
| 81 | + }); |
| 82 | + |
| 83 | + test('CSS Modules class has a global parent', async () => { |
| 84 | + const source = |
| 85 | + '<style>\n' + |
| 86 | + ':global(div) .red { font-size: 20px; }\n' + |
| 87 | + '.red { color: red; }\n' + |
| 88 | + '</style>\n' + |
| 89 | + '<div><span class="$style.red">Red</span></div>'; |
| 90 | + |
| 91 | + const expectedOutput = |
| 92 | + '<style>\n' + |
| 93 | + ':global(div) :global(.red-123) { font-size: 20px; }\n' + |
| 94 | + ':global(.red-123) { color: red; }\n' + |
| 95 | + '</style>\n' + |
| 96 | + '<div><span class="red-123">Red</span></div>'; |
| 97 | + |
| 98 | + const output = await compiler( |
| 99 | + { |
| 100 | + source, |
| 101 | + }, |
| 102 | + { |
| 103 | + localIdentName: '[local]-123', |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + expect(output).toBe(expectedOutput); |
| 108 | + }); |
| 109 | + |
| 110 | + test('CSS Modules class is used within a media query', async () => { |
| 111 | + const source = |
| 112 | + '<style>\n' + |
| 113 | + '@media (min-width: 37.5em) {\n' + |
| 114 | + '.red { color: red; }\n' + |
| 115 | + 'div.bold { font-weight: bold; }\n' + |
| 116 | + '}\n' + |
| 117 | + '</style>\n' + |
| 118 | + '<div class="$style.bold"><span class="$style.red">Red</span></div>'; |
| 119 | + |
| 120 | + const expectedOutput = |
| 121 | + '<style>\n' + |
| 122 | + '@media (min-width: 37.5em) {\n' + |
| 123 | + ':global(.red-123) { color: red; }\n' + |
| 124 | + ':global(div.bold-123) { font-weight: bold; }\n' + |
| 125 | + '}\n' + |
| 126 | + '</style>\n' + |
| 127 | + '<div class="bold-123"><span class="red-123">Red</span></div>'; |
| 128 | + |
| 129 | + const output = await compiler( |
| 130 | + { |
| 131 | + source, |
| 132 | + }, |
| 133 | + { |
| 134 | + localIdentName: '[local]-123', |
| 135 | + } |
| 136 | + ); |
| 137 | + |
| 138 | + expect(output).toBe(expectedOutput); |
| 139 | + }); |
| 140 | +}); |
0 commit comments