|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
1 | 3 | import postcss from 'postcss'
|
2 | 4 | import plugin from '../src/lib/evaluateTailwindFunctions'
|
3 |
| -import tailwind from '../src/index' |
4 |
| -import { css } from './util/run' |
| 5 | +import { run as runFull, css, html } from './util/run' |
5 | 6 |
|
6 | 7 | function run(input, opts = {}) {
|
7 |
| - return postcss([plugin({ tailwindConfig: opts })]).process(input, { from: undefined }) |
8 |
| -} |
9 |
| - |
10 |
| -function runFull(input, config) { |
11 |
| - return postcss([tailwind(config)]).process(input, { from: undefined }) |
| 8 | + return postcss([ |
| 9 | + plugin({ |
| 10 | + tailwindConfig: opts, |
| 11 | + markInvalidUtilityNode() { |
| 12 | + // no op |
| 13 | + }, |
| 14 | + }), |
| 15 | + ]).process(input, { from: undefined }) |
12 | 16 | }
|
13 | 17 |
|
14 | 18 | test('it looks up values in the theme using dot notation', () => {
|
@@ -1222,3 +1226,62 @@ it('can find values with slashes in the theme key while still allowing for alpha
|
1222 | 1226 | `)
|
1223 | 1227 | })
|
1224 | 1228 | })
|
| 1229 | + |
| 1230 | +describe('context dependent', () => { |
| 1231 | + let configPath = path.resolve(__dirname, './evaluate-tailwind-functions.tailwind.config.js') |
| 1232 | + let filePath = path.resolve(__dirname, './evaluate-tailwind-functions.test.html') |
| 1233 | + let config = { |
| 1234 | + content: [filePath], |
| 1235 | + corePlugins: { preflight: false }, |
| 1236 | + } |
| 1237 | + |
| 1238 | + // Rebuild the config file for each test |
| 1239 | + beforeEach(() => fs.promises.writeFile(configPath, `module.exports = ${JSON.stringify(config)};`)) |
| 1240 | + afterEach(() => fs.promises.unlink(configPath)) |
| 1241 | + |
| 1242 | + let warn |
| 1243 | + |
| 1244 | + beforeEach(() => { |
| 1245 | + warn = jest.spyOn(require('../src/util/log').default, 'warn') |
| 1246 | + }) |
| 1247 | + |
| 1248 | + afterEach(() => warn.mockClear()) |
| 1249 | + |
| 1250 | + it('should not generate when theme fn doesnt resolve', async () => { |
| 1251 | + await fs.promises.writeFile( |
| 1252 | + filePath, |
| 1253 | + html` |
| 1254 | + <div class="underline [--box-shadow:theme('boxShadow.doesnotexist')]"></div> |
| 1255 | + <div class="bg-[theme('boxShadow.doesnotexist')]"></div> |
| 1256 | + ` |
| 1257 | + ) |
| 1258 | + |
| 1259 | + // TODO: We need a way to reuse the context in our test suite without requiring writing to files |
| 1260 | + // It should be an explicit thing tho — like we create a context and pass it in or something |
| 1261 | + let result = await runFull('@tailwind utilities', configPath) |
| 1262 | + |
| 1263 | + // 1. On first run it should work because it's been removed from the class cache |
| 1264 | + expect(result.css).toMatchCss(css` |
| 1265 | + .underline { |
| 1266 | + text-decoration-line: underline; |
| 1267 | + } |
| 1268 | + `) |
| 1269 | + |
| 1270 | + // 2. But we get a warning in the console |
| 1271 | + expect(warn).toHaveBeenCalledTimes(1) |
| 1272 | + expect(warn.mock.calls.map((x) => x[0])).toEqual(['invalid-theme-key-in-class']) |
| 1273 | + |
| 1274 | + // 3. The second run should work fine because it's been removed from the class cache |
| 1275 | + result = await runFull('@tailwind utilities', configPath) |
| 1276 | + |
| 1277 | + expect(result.css).toMatchCss(css` |
| 1278 | + .underline { |
| 1279 | + text-decoration-line: underline; |
| 1280 | + } |
| 1281 | + `) |
| 1282 | + |
| 1283 | + // 4. But we've not received any further logs about it |
| 1284 | + expect(warn).toHaveBeenCalledTimes(1) |
| 1285 | + expect(warn.mock.calls.map((x) => x[0])).toEqual(['invalid-theme-key-in-class']) |
| 1286 | + }) |
| 1287 | +}) |
0 commit comments