| 1 | let postcss = require('postcss')
|
|---|
| 2 |
|
|---|
| 3 | let clamp = require('./')
|
|---|
| 4 |
|
|---|
| 5 | async function run (input, output, opts) {
|
|---|
| 6 | let result = await postcss([clamp(opts)]).process(input, {
|
|---|
| 7 | from: '/test.css'
|
|---|
| 8 | })
|
|---|
| 9 | expect(result.css).toEqual(output)
|
|---|
| 10 | expect(result.warnings()).toHaveLength(0)
|
|---|
| 11 | return result
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | it('handle simple transformation (only values)', async () => {
|
|---|
| 15 | await run(
|
|---|
| 16 | 'a{ width: clamp(10px, 64px, 80px); }',
|
|---|
| 17 | 'a{ width: max(10px, min(64px, 80px)); }'
|
|---|
| 18 | )
|
|---|
| 19 | })
|
|---|
| 20 |
|
|---|
| 21 | it('handle simple transformation (only values) with preserve', async () => {
|
|---|
| 22 | await run(
|
|---|
| 23 | 'a{ width: clamp(10px, 64px, 80px); }',
|
|---|
| 24 | 'a{ width: max(10px, min(64px, 80px)); width: clamp(10px, 64px, 80px); }',
|
|---|
| 25 | { preserve: true }
|
|---|
| 26 | )
|
|---|
| 27 | })
|
|---|
| 28 |
|
|---|
| 29 | it('handle transformation with functions', async () => {
|
|---|
| 30 | await run(
|
|---|
| 31 | 'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
|
|---|
| 32 | 'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); }'
|
|---|
| 33 | )
|
|---|
| 34 | })
|
|---|
| 35 |
|
|---|
| 36 | it('handle transformation with functions with preserve', async () => {
|
|---|
| 37 | await run(
|
|---|
| 38 | 'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
|
|---|
| 39 | 'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); ' +
|
|---|
| 40 | 'width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
|
|---|
| 41 | { preserve: true }
|
|---|
| 42 | )
|
|---|
| 43 | })
|
|---|
| 44 |
|
|---|
| 45 | it('handle transformation with different units', async () => {
|
|---|
| 46 | await run(
|
|---|
| 47 | 'a{ width: clamp(10%, 2px, 4rem); }',
|
|---|
| 48 | 'a{ width: max(10%, min(2px, 4rem)); }'
|
|---|
| 49 | )
|
|---|
| 50 | })
|
|---|
| 51 |
|
|---|
| 52 | it('handle transformation with different units and preserve', async () => {
|
|---|
| 53 | await run(
|
|---|
| 54 | 'a{ width: clamp(10%, 2px, 4rem); }',
|
|---|
| 55 | 'a{ width: max(10%, min(2px, 4rem)); width: clamp(10%, 2px, 4rem); }',
|
|---|
| 56 | { preserve: true }
|
|---|
| 57 | )
|
|---|
| 58 | })
|
|---|
| 59 |
|
|---|
| 60 | it('transform only function with 3 parameters', async () => {
|
|---|
| 61 | await run(
|
|---|
| 62 | 'a{ width: clamp(10%, 2px, 4rem);' +
|
|---|
| 63 | '\nheight: clamp(10px, 20px, 30px, 40px); }',
|
|---|
| 64 | 'a{ width: max(10%, min(2px, 4rem));' +
|
|---|
| 65 | '\nheight: clamp(10px, 20px, 30px, 40px); }'
|
|---|
| 66 | )
|
|---|
| 67 | })
|
|---|
| 68 |
|
|---|
| 69 | it('transform only clamp function', async () => {
|
|---|
| 70 | await run(
|
|---|
| 71 | 'a{ width: clamp(10%, 2px, 4rem);\nheight: calc(10px + 100%); }',
|
|---|
| 72 | 'a{ width: max(10%, min(2px, 4rem));\nheight: calc(10px + 100%); }'
|
|---|
| 73 | )
|
|---|
| 74 | })
|
|---|
| 75 |
|
|---|
| 76 | it('precalculate second and third with the same unit (int values)', async () => {
|
|---|
| 77 | await run('a{ width: clamp(10%, 2px, 5px); }', 'a{ width: max(10%, 7px); }', {
|
|---|
| 78 | precalculate: true
|
|---|
| 79 | })
|
|---|
| 80 | })
|
|---|
| 81 |
|
|---|
| 82 | it('precalculate second and third with the same unit (float values)', async () => {
|
|---|
| 83 | await run(
|
|---|
| 84 | 'a{ width: clamp(10%, 2.5px, 5.1px); }',
|
|---|
| 85 | 'a{ width: max(10%, 7.6px); }',
|
|---|
| 86 | { precalculate: true }
|
|---|
| 87 | )
|
|---|
| 88 | })
|
|---|
| 89 |
|
|---|
| 90 | it('precalculate second and third with the same unit (float and int values)', async () => {
|
|---|
| 91 | await run(
|
|---|
| 92 | 'a{ width: clamp(10%, 2.5px, 5px); }',
|
|---|
| 93 | 'a{ width: max(10%, 7.5px); }',
|
|---|
| 94 | { precalculate: true }
|
|---|
| 95 | )
|
|---|
| 96 | })
|
|---|
| 97 |
|
|---|
| 98 | it('precalculate 2nd & 3rd with the same unit (float and int vals) & preserve', async () => {
|
|---|
| 99 | await run(
|
|---|
| 100 | 'a{ width: clamp(10%, 2.5px, 5px); }',
|
|---|
| 101 | 'a{ width: max(10%, 7.5px); width: clamp(10%, 2.5px, 5px); }',
|
|---|
| 102 | { precalculate: true, preserve: true }
|
|---|
| 103 | )
|
|---|
| 104 | })
|
|---|
| 105 |
|
|---|
| 106 | it('precalculate all values with the same unit (int values)', async () => {
|
|---|
| 107 | await run('a{ width: clamp(10px, 2px, 5px); }', 'a{ width: 17px; }', {
|
|---|
| 108 | precalculate: true
|
|---|
| 109 | })
|
|---|
| 110 | })
|
|---|
| 111 |
|
|---|
| 112 | it('precalculate all values with the same unit (float values)', async () => {
|
|---|
| 113 | await run(
|
|---|
| 114 | 'a{ width: clamp(10.4px, 2.11px, 5.9px); }',
|
|---|
| 115 | 'a{ width: 18.41px; }',
|
|---|
| 116 | { precalculate: true }
|
|---|
| 117 | )
|
|---|
| 118 | })
|
|---|
| 119 |
|
|---|
| 120 | it('precalculate all values with the same unit (int and float values)', async () => {
|
|---|
| 121 | await run('a{ width: clamp(10.4px, 2px, 5.9px); }', 'a{ width: 18.3px; }', {
|
|---|
| 122 | precalculate: true
|
|---|
| 123 | })
|
|---|
| 124 | })
|
|---|
| 125 |
|
|---|
| 126 | it('handle function with enable precalculation as third', async () => {
|
|---|
| 127 | await run(
|
|---|
| 128 | 'a{ width: clamp(10px, 2px, calc(10px + 100%)); }',
|
|---|
| 129 | 'a{ width: max(10px, min(2px, calc(10px + 100%))); }',
|
|---|
| 130 | { precalculate: true }
|
|---|
| 131 | )
|
|---|
| 132 | })
|
|---|
| 133 |
|
|---|
| 134 | it('handle function with enable precalculation as second', async () => {
|
|---|
| 135 | await run(
|
|---|
| 136 | 'a{ width: clamp(10px, calc(10px + 100%), 2px); }',
|
|---|
| 137 | 'a{ width: max(10px, min(calc(10px + 100%), 2px)); }',
|
|---|
| 138 | { precalculate: true }
|
|---|
| 139 | )
|
|---|
| 140 | })
|
|---|
| 141 |
|
|---|
| 142 | it('handle function with enable precalculation as first', async () => {
|
|---|
| 143 | await run(
|
|---|
| 144 | 'a{ width: clamp(calc(10px + 100%), 10px, 2px); }',
|
|---|
| 145 | 'a{ width: max(calc(10px + 100%), 12px); }',
|
|---|
| 146 | { precalculate: true }
|
|---|
| 147 | )
|
|---|
| 148 | })
|
|---|
| 149 |
|
|---|
| 150 | it('handle function with enable precalculation as all', async () => {
|
|---|
| 151 | await run(
|
|---|
| 152 | 'a{ width: clamp(calc(10px + 100%), calc(10rem + 200%), 10px); }',
|
|---|
| 153 | 'a{ width: max(calc(10px + 100%), min(calc(10rem + 200%), 10px)); }',
|
|---|
| 154 | { precalculate: true }
|
|---|
| 155 | )
|
|---|
| 156 | })
|
|---|
| 157 |
|
|---|
| 158 | it('handle not valid values', async () => {
|
|---|
| 159 | await run('a{ width: clamp(a, b, c); }', 'a{ width: max(a, min(b, c)); }', {
|
|---|
| 160 | precalculate: true
|
|---|
| 161 | })
|
|---|
| 162 | })
|
|---|
| 163 |
|
|---|
| 164 | it('handle not valid values with preserve', async () => {
|
|---|
| 165 | await run(
|
|---|
| 166 | 'a{ width: clamp(a, b, c); }',
|
|---|
| 167 | 'a{ width: max(a, min(b, c)); width: clamp(a, b, c); }',
|
|---|
| 168 | { precalculate: true, preserve: true }
|
|---|
| 169 | )
|
|---|
| 170 | })
|
|---|
| 171 |
|
|---|
| 172 | it('handle not valid values mixed with valid', async () => {
|
|---|
| 173 | await run(
|
|---|
| 174 | 'a{ width: clamp(a, 1px, 2em); }',
|
|---|
| 175 | 'a{ width: max(a, min(1px, 2em)); }',
|
|---|
| 176 | { precalculate: true }
|
|---|
| 177 | )
|
|---|
| 178 | })
|
|---|
| 179 |
|
|---|
| 180 | it('handle not valid values mixed with valid and preserve', async () => {
|
|---|
| 181 | await run(
|
|---|
| 182 | 'a{ width: clamp(a, 1px, 2em); }',
|
|---|
| 183 | 'a{ width: max(a, min(1px, 2em)); width: clamp(a, 1px, 2em); }',
|
|---|
| 184 | { precalculate: true, preserve: true }
|
|---|
| 185 | )
|
|---|
| 186 | })
|
|---|
| 187 |
|
|---|
| 188 | it('handle complex values', async () => {
|
|---|
| 189 | await run(
|
|---|
| 190 | 'a{ grid-template-columns: clamp(22rem, 40%, 32rem) minmax(0, 1fr); }',
|
|---|
| 191 | 'a{ grid-template-columns: max(22rem, min(40%, 32rem)) minmax(0, 1fr); }'
|
|---|
| 192 | )
|
|---|
| 193 | })
|
|---|
| 194 |
|
|---|
| 195 | it('handle multiple complex values', async () => {
|
|---|
| 196 | await run(
|
|---|
| 197 | 'a{ margin: clamp(1rem, 2%, 3rem) 4px clamp(5rem, 6%, 7rem) 8rem; }',
|
|---|
| 198 | 'a{ margin: max(1rem, min(2%, 3rem)) 4px max(5rem, min(6%, 7rem)) 8rem; }'
|
|---|
| 199 | )
|
|---|
| 200 | })
|
|---|
| 201 |
|
|---|
| 202 | it('handle calc', async () => {
|
|---|
| 203 | await run(
|
|---|
| 204 | 'a{ margin: 0 40px 0 calc(-1 * clamp(32px, 16vw, 64px)); }',
|
|---|
| 205 | 'a{ margin: 0 40px 0 calc(-1 * max(32px, min(16vw, 64px))); }'
|
|---|
| 206 | )
|
|---|
| 207 | })
|
|---|
| 208 |
|
|---|
| 209 | it('handle multiple calc', async () => {
|
|---|
| 210 | await run(
|
|---|
| 211 | 'a{ margin: calc(-1 * clamp(1px, 2vw, 3px)) calc(-1 * clamp(4px, 5vw, 6px)); }',
|
|---|
| 212 | 'a{ margin: calc(-1 * max(1px, min(2vw, 3px))) calc(-1 * max(4px, min(5vw, 6px))); }'
|
|---|
| 213 | )
|
|---|
| 214 | })
|
|---|
| 215 |
|
|---|
| 216 | it('handle nested clamp', async () => {
|
|---|
| 217 | await run(
|
|---|
| 218 | 'a{ font-size: clamp(clamp(1rem, 2vw, 3rem), 4vw, 5rem); }',
|
|---|
| 219 | 'a{ font-size: max(max(1rem, min(2vw, 3rem)), min(4vw, 5rem)); }'
|
|---|
| 220 | )
|
|---|
| 221 | })
|
|---|