From f2b7fbae6b1834d7f7ef908c3a12b24b300699e2 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 22 Aug 2024 12:58:56 -0400 Subject: [PATCH] Add config test --- .../tailwindcss/src/compat/config.test.ts | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 packages/tailwindcss/src/compat/config.test.ts diff --git a/packages/tailwindcss/src/compat/config.test.ts b/packages/tailwindcss/src/compat/config.test.ts new file mode 100644 index 000000000..b5e89ad4f --- /dev/null +++ b/packages/tailwindcss/src/compat/config.test.ts @@ -0,0 +1,197 @@ +import { test } from 'vitest' +import { compile } from '..' +import plugin from '../plugin' + +const css = String.raw + +test('Config files can add content', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ content: ['./file.txt'] }), + }) + + expect(compiler.globs).toEqual(['./file.txt']) +}) + +test('Config files can change dark mode (media)', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ darkMode: 'media' }), + }) + + expect(compiler.build(['dark:underline'])).toMatchInlineSnapshot(` + ".dark\\:underline { + @media (prefers-color-scheme: dark) { + text-decoration-line: underline; + } + } + " + `) +}) + +test('Config files can change dark mode (selector)', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ darkMode: 'selector' }), + }) + + expect(compiler.build(['dark:underline'])).toMatchInlineSnapshot(` + ".dark\\:underline { + &:where(.dark, .dark *) { + text-decoration-line: underline; + } + } + " + `) +}) + +test('Config files can change dark mode (variant)', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ darkMode: ['variant', '&:where(:not(.light))'] }), + }) + + expect(compiler.build(['dark:underline'])).toMatchInlineSnapshot(` + ".dark\\:underline { + &:where(:not(.light)) { + text-decoration-line: underline; + } + } + " + `) +}) + +test('Config files can add plugins', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ + plugins: [ + plugin(function ({ addUtilities }) { + addUtilities({ + '.no-scrollbar': { + 'scrollbar-width': 'none', + }, + }) + }), + ], + }), + }) + + expect(compiler.build(['no-scrollbar'])).toMatchInlineSnapshot(` + ".no-scrollbar { + scrollbar-width: none; + } + " + `) +}) + +test('Plugins loaded from config files can contribute to the config', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ + plugins: [ + plugin(() => {}, { + darkMode: ['variant', '&:where(:not(.light))'], + }), + ], + }), + }) + + expect(compiler.build(['dark:underline'])).toMatchInlineSnapshot(` + ".dark\\:underline { + &:where(:not(.light)) { + text-decoration-line: underline; + } + } + " + `) +}) + +test('Config file presets can contribute to the config', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ + presets: [ + { + darkMode: ['variant', '&:where(:not(.light))'], + }, + ], + }), + }) + + expect(compiler.build(['dark:underline'])).toMatchInlineSnapshot(` + ".dark\\:underline { + &:where(:not(.light)) { + text-decoration-line: underline; + } + } + " + `) +}) + +test('Config files can affect the theme', async ({ expect }) => { + let input = css` + @tailwind utilities; + @config "./config.js"; + ` + + let compiler = await compile(input, { + loadConfig: async () => ({ + theme: { + extend: { + colors: { + primary: '#c0ffee', + }, + }, + }, + + plugins: [ + plugin(function ({ addUtilities, theme }) { + addUtilities({ + '.scrollbar-primary': { + scrollbarColor: theme('colors.primary'), + }, + }) + }), + ], + }), + }) + + // TODO: This does not work yet but should + // expect(compiler.build(['bg-primary'])).toMatchInlineSnapshot() + + expect(compiler.build(['scrollbar-primary'])).toMatchInlineSnapshot(` + ".scrollbar-primary { + scrollbar-color: #c0ffee; + } + " + `) +})