|
| 1 | +import postcss from 'postcss' |
| 2 | +import path from 'path' |
| 3 | +import tailwind from '../../src/jit/index.js' |
| 4 | + |
| 5 | +function run(input, config = {}) { |
| 6 | + const { currentTestName } = expect.getState() |
| 7 | + |
| 8 | + return postcss(tailwind(config)).process(input, { |
| 9 | + from: `${path.resolve(__filename)}?test=${currentTestName}`, |
| 10 | + }) |
| 11 | +} |
| 12 | + |
| 13 | +test('basic color opacity modifier', async () => { |
| 14 | + let config = { |
| 15 | + mode: 'jit', |
| 16 | + purge: [ |
| 17 | + { |
| 18 | + raw: '<div class="bg-red-500/50"></div>', |
| 19 | + }, |
| 20 | + ], |
| 21 | + theme: {}, |
| 22 | + plugins: [], |
| 23 | + } |
| 24 | + |
| 25 | + let css = `@tailwind utilities` |
| 26 | + |
| 27 | + return run(css, config).then((result) => { |
| 28 | + expect(result.css).toMatchFormattedCss(` |
| 29 | + .bg-red-500\\/50 { |
| 30 | + background-color: rgba(239, 68, 68, 0.5); |
| 31 | + } |
| 32 | + `) |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +test('colors with slashes are matched first', async () => { |
| 37 | + let config = { |
| 38 | + mode: 'jit', |
| 39 | + purge: [ |
| 40 | + { |
| 41 | + raw: '<div class="bg-red-500/50"></div>', |
| 42 | + }, |
| 43 | + ], |
| 44 | + theme: { |
| 45 | + extend: { |
| 46 | + colors: { |
| 47 | + 'red-500/50': '#ff0000', |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + plugins: [], |
| 52 | + } |
| 53 | + |
| 54 | + let css = `@tailwind utilities` |
| 55 | + |
| 56 | + return run(css, config).then((result) => { |
| 57 | + expect(result.css).toMatchFormattedCss(` |
| 58 | + .bg-red-500\\/50 { |
| 59 | + --tw-bg-opacity: 1; |
| 60 | + background-color: rgba(255, 0, 0, var(--tw-bg-opacity)); |
| 61 | + } |
| 62 | + `) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +test('arbitrary color opacity modifier', async () => { |
| 67 | + let config = { |
| 68 | + mode: 'jit', |
| 69 | + purge: [ |
| 70 | + { |
| 71 | + raw: 'bg-red-500/[var(--opacity)]', |
| 72 | + }, |
| 73 | + ], |
| 74 | + theme: {}, |
| 75 | + plugins: [], |
| 76 | + } |
| 77 | + |
| 78 | + let css = `@tailwind utilities` |
| 79 | + |
| 80 | + return run(css, config).then((result) => { |
| 81 | + expect(result.css).toMatchFormattedCss(` |
| 82 | + .bg-red-500\\/\\[var\\(--opacity\\)\\] { |
| 83 | + background-color: rgba(239, 68, 68, var(--opacity)); |
| 84 | + } |
| 85 | + `) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +test('missing alpha generates nothing', async () => { |
| 90 | + let config = { |
| 91 | + mode: 'jit', |
| 92 | + purge: [ |
| 93 | + { |
| 94 | + raw: '<div class="bg-red-500/"></div>', |
| 95 | + }, |
| 96 | + ], |
| 97 | + theme: {}, |
| 98 | + plugins: [], |
| 99 | + } |
| 100 | + |
| 101 | + let css = `@tailwind utilities` |
| 102 | + |
| 103 | + return run(css, config).then((result) => { |
| 104 | + expect(result.css).toMatchFormattedCss(``) |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
| 108 | +test('values not in the opacity config are ignored', async () => { |
| 109 | + let config = { |
| 110 | + mode: 'jit', |
| 111 | + purge: [ |
| 112 | + { |
| 113 | + raw: '<div class="bg-red-500/29"></div>', |
| 114 | + }, |
| 115 | + ], |
| 116 | + theme: { |
| 117 | + opacity: { |
| 118 | + 0: '0', |
| 119 | + 25: '0.25', |
| 120 | + 5: '0.5', |
| 121 | + 75: '0.75', |
| 122 | + 100: '1', |
| 123 | + }, |
| 124 | + }, |
| 125 | + plugins: [], |
| 126 | + } |
| 127 | + |
| 128 | + let css = `@tailwind utilities` |
| 129 | + |
| 130 | + return run(css, config).then((result) => { |
| 131 | + expect(result.css).toMatchFormattedCss(``) |
| 132 | + }) |
| 133 | +}) |
| 134 | + |
| 135 | +test('function colors are supported', async () => { |
| 136 | + let config = { |
| 137 | + mode: 'jit', |
| 138 | + purge: [ |
| 139 | + { |
| 140 | + raw: '<div class="bg-blue/50"></div>', |
| 141 | + }, |
| 142 | + ], |
| 143 | + theme: { |
| 144 | + colors: { |
| 145 | + blue: ({ opacityValue }) => { |
| 146 | + return `rgba(var(--colors-blue), ${opacityValue})` |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + plugins: [], |
| 151 | + } |
| 152 | + |
| 153 | + let css = `@tailwind utilities` |
| 154 | + |
| 155 | + return run(css, config).then((result) => { |
| 156 | + expect(result.css).toMatchFormattedCss(` |
| 157 | + .bg-blue\\/50 { |
| 158 | + background-color: rgba(var(--colors-blue), 0.5); |
| 159 | + } |
| 160 | + `) |
| 161 | + }) |
| 162 | +}) |
| 163 | + |
| 164 | +test('utilities that support any type are supported', async () => { |
| 165 | + let config = { |
| 166 | + mode: 'jit', |
| 167 | + purge: [ |
| 168 | + { |
| 169 | + raw: ` |
| 170 | + <div class="from-red-500/50"></div> |
| 171 | + <div class="fill-red-500/25"></div> |
| 172 | + <div class="placeholder-red-500/75"></div> |
| 173 | + `, |
| 174 | + }, |
| 175 | + ], |
| 176 | + theme: { |
| 177 | + extend: { |
| 178 | + fill: (theme) => theme('colors'), |
| 179 | + }, |
| 180 | + }, |
| 181 | + plugins: [], |
| 182 | + } |
| 183 | + |
| 184 | + let css = `@tailwind utilities` |
| 185 | + |
| 186 | + return run(css, config).then((result) => { |
| 187 | + expect(result.css).toMatchFormattedCss(` |
| 188 | + .from-red-500\\/50 { |
| 189 | + --tw-gradient-from: rgba(239, 68, 68, 0.5); |
| 190 | + --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to, rgba(239, 68, 68, 0)); |
| 191 | + } |
| 192 | + .fill-red-500\\/25 { |
| 193 | + fill: rgba(239, 68, 68, 0.25); |
| 194 | + } |
| 195 | + .placeholder-red-500\\/75::placeholder { |
| 196 | + color: rgba(239, 68, 68, 0.75); |
| 197 | + } |
| 198 | + `) |
| 199 | + }) |
| 200 | +}) |
0 commit comments