|
| 1 | +const compiler = require('../compiler.js'); |
| 2 | + |
| 3 | +describe('Class Attribute Object', () => { |
| 4 | + test('Shorthand', async () => { |
| 5 | + const output = await compiler( |
| 6 | + { |
| 7 | + source: `<style module>.red { color: red; }</style>` |
| 8 | + + `<span class={{red}}>btn</span>`, |
| 9 | + }, |
| 10 | + { |
| 11 | + localIdentName: '[local]-123', |
| 12 | + } |
| 13 | + ); |
| 14 | + |
| 15 | + expect(output).toBe( |
| 16 | + `<style module>:global(.red-123) { color: red; }</style>` |
| 17 | + + `<span class={{'red-123': red}}>btn</span>`, |
| 18 | + ); |
| 19 | + }); |
| 20 | + test('Identifier key', async () => { |
| 21 | + const output = await compiler( |
| 22 | + { |
| 23 | + source: `<style module>.red { color: red; }</style>` |
| 24 | + + `<span class={{red: active}}>btn</span>`, |
| 25 | + }, |
| 26 | + { |
| 27 | + localIdentName: '[local]-123', |
| 28 | + } |
| 29 | + ); |
| 30 | + |
| 31 | + expect(output).toBe( |
| 32 | + `<style module>:global(.red-123) { color: red; }</style>` |
| 33 | + + `<span class={{'red-123': active}}>btn</span>`, |
| 34 | + ); |
| 35 | + }); |
| 36 | + test('Literal key', async () => { |
| 37 | + const output = await compiler( |
| 38 | + { |
| 39 | + source: `<style module>.red { color: red; }</style>` |
| 40 | + + `<span class={{'red': active}}>btn</span>`, |
| 41 | + }, |
| 42 | + { |
| 43 | + localIdentName: '[local]-123', |
| 44 | + } |
| 45 | + ); |
| 46 | + |
| 47 | + expect(output).toBe( |
| 48 | + `<style module>:global(.red-123) { color: red; }</style>` |
| 49 | + + `<span class={{'red-123': active}}>btn</span>`, |
| 50 | + ); |
| 51 | + }); |
| 52 | + test('Multiple literal keys', async () => { |
| 53 | + const output = await compiler( |
| 54 | + { |
| 55 | + source: `<style module>.red { color: red; } .bold { font-weight: bold; }</style>` |
| 56 | + + `<span class={{'red blue bold': active}}>btn</span>`, |
| 57 | + }, |
| 58 | + { |
| 59 | + localIdentName: '[local]-123', |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + expect(output).toBe( |
| 64 | + `<style module>:global(.red-123) { color: red; } :global(.bold-123) { font-weight: bold; }</style>` |
| 65 | + + `<span class={{'red-123 blue bold-123': active}}>btn</span>`, |
| 66 | + ); |
| 67 | + }); |
| 68 | + test('Multiple conditions', async () => { |
| 69 | + const output = await compiler( |
| 70 | + { |
| 71 | + source: `<style module>.red { color: red; } .blue { color: blue; } .bold { font-weight: bold; }</style>` |
| 72 | + + `<span class={{red, blue: !red, 'bold italic': red}}>btn</span>`, |
| 73 | + }, |
| 74 | + { |
| 75 | + localIdentName: '[local]-123', |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + expect(output).toBe( |
| 80 | + `<style module>:global(.red-123) { color: red; } :global(.blue-123) { color: blue; } :global(.bold-123) { font-weight: bold; }</style>` |
| 81 | + + `<span class={{'red-123': red, 'blue-123': !red, 'bold-123 italic': red}}>btn</span>`, |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('Class Attribute Array', () => { |
| 87 | + test('Thruthy value', async () => { |
| 88 | + const output = await compiler( |
| 89 | + { |
| 90 | + source: `<style module>.red { color: red; }</style>` |
| 91 | + + `<span class={[active && 'red']}>btn</span>`, |
| 92 | + }, |
| 93 | + { |
| 94 | + localIdentName: '[local]-123', |
| 95 | + } |
| 96 | + ); |
| 97 | + |
| 98 | + expect(output).toBe( |
| 99 | + `<style module>:global(.red-123) { color: red; }</style>` |
| 100 | + + `<span class={[active && 'red-123']}>btn</span>`, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + test('Combined thruty values', async () => { |
| 105 | + const output = await compiler( |
| 106 | + { |
| 107 | + source: `<style module>.red { color: red; } .bold { font-weight: bold; }</style>` |
| 108 | + + `<span class={[ active && 'red', bold && 'bold' ]}>btn</span>`, |
| 109 | + }, |
| 110 | + { |
| 111 | + localIdentName: '[local]-123', |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + expect(output).toBe( |
| 116 | + `<style module>:global(.red-123) { color: red; } :global(.bold-123) { font-weight: bold; }</style>` |
| 117 | + + `<span class={[ active && 'red-123', bold && 'bold-123' ]}>btn</span>`, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + test('Mixed condition', async () => { |
| 122 | + const output = await compiler( |
| 123 | + { |
| 124 | + source: `<style module>.red { color: red; } .bold { font-weight: bold; } .italic { font-style: italic; }</style>` |
| 125 | + + `<span class={[ active && 'red bold', !bold && 'light', 'small italic' ]}>btn</span>`, |
| 126 | + }, |
| 127 | + { |
| 128 | + localIdentName: '[local]-123', |
| 129 | + } |
| 130 | + ); |
| 131 | + |
| 132 | + expect(output).toBe( |
| 133 | + `<style module>:global(.red-123) { color: red; } :global(.bold-123) { font-weight: bold; } :global(.italic-123) { font-style: italic; }</style>` |
| 134 | + + `<span class={[ active && 'red-123 bold-123', !bold && 'light', 'small italic-123' ]}>btn</span>`, |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + test('has variables', async () => { |
| 139 | + const output = await compiler( |
| 140 | + { |
| 141 | + source: `<style module>.red { color: red; } .bold { font-weight: bold; }</style>` |
| 142 | + + `<span class={[ 'red bold', bold, props.class ]}>btn</span>`, |
| 143 | + }, |
| 144 | + { |
| 145 | + localIdentName: '[local]-123', |
| 146 | + } |
| 147 | + ); |
| 148 | + |
| 149 | + expect(output).toBe( |
| 150 | + `<style module>:global(.red-123) { color: red; } :global(.bold-123) { font-weight: bold; }</style>` |
| 151 | + + `<span class={[ 'red-123 bold-123', bold, props.class ]}>btn</span>`, |
| 152 | + ); |
| 153 | + }); |
| 154 | +}); |
0 commit comments