|
| 1 | +import { run, html, css } from './util/run' |
| 2 | + |
| 3 | +let warn |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + warn = jest.spyOn(require('../src/util/log').default, 'warn') |
| 7 | +}) |
| 8 | + |
| 9 | +afterEach(() => warn.mockClear()) |
| 10 | + |
| 11 | +it('can block classes matched literally', () => { |
| 12 | + let config = { |
| 13 | + content: [ |
| 14 | + { |
| 15 | + raw: html`<div |
| 16 | + class="font-bold uppercase sm:hover:text-sm hover:text-sm bg-red-500/50 my-custom-class" |
| 17 | + ></div>`, |
| 18 | + }, |
| 19 | + ], |
| 20 | + blocklist: ['font', 'uppercase', 'hover:text-sm', 'bg-red-500/50', 'my-custom-class'], |
| 21 | + } |
| 22 | + |
| 23 | + let input = css` |
| 24 | + @tailwind utilities; |
| 25 | + .my-custom-class { |
| 26 | + color: red; |
| 27 | + } |
| 28 | + ` |
| 29 | + |
| 30 | + return run(input, config).then((result) => { |
| 31 | + return expect(result.css).toMatchCss(css` |
| 32 | + .font-bold { |
| 33 | + font-weight: 700; |
| 34 | + } |
| 35 | + .my-custom-class { |
| 36 | + color: red; |
| 37 | + } |
| 38 | + @media (min-width: 640px) { |
| 39 | + .sm\:hover\:text-sm:hover { |
| 40 | + font-size: 0.875rem; |
| 41 | + line-height: 1.25rem; |
| 42 | + } |
| 43 | + } |
| 44 | + `) |
| 45 | + }) |
| 46 | +}) |
| 47 | + |
| 48 | +it('can block classes inside @layer', () => { |
| 49 | + let config = { |
| 50 | + content: [ |
| 51 | + { |
| 52 | + raw: html`<div class="font-bold my-custom-class"></div>`, |
| 53 | + }, |
| 54 | + ], |
| 55 | + blocklist: ['my-custom-class'], |
| 56 | + } |
| 57 | + |
| 58 | + let input = css` |
| 59 | + @tailwind utilities; |
| 60 | + @layer utilities { |
| 61 | + .my-custom-class { |
| 62 | + color: red; |
| 63 | + } |
| 64 | + } |
| 65 | + ` |
| 66 | + |
| 67 | + return run(input, config).then((result) => { |
| 68 | + return expect(result.css).toMatchCss(css` |
| 69 | + .font-bold { |
| 70 | + font-weight: 700; |
| 71 | + } |
| 72 | + `) |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +it('blocklists do NOT support regexes', async () => { |
| 77 | + let config = { |
| 78 | + content: [{ raw: html`<div class="font-bold bg-[#f00d1e]"></div>` }], |
| 79 | + blocklist: [/^bg-\[[^]+\]$/], |
| 80 | + } |
| 81 | + |
| 82 | + let result = await run('@tailwind utilities', config) |
| 83 | + |
| 84 | + expect(result.css).toMatchCss(css` |
| 85 | + .bg-\[\#f00d1e\] { |
| 86 | + --tw-bg-opacity: 1; |
| 87 | + background-color: rgb(240 13 30 / var(--tw-bg-opacity)); |
| 88 | + } |
| 89 | + .font-bold { |
| 90 | + font-weight: 700; |
| 91 | + } |
| 92 | + `) |
| 93 | + |
| 94 | + expect(warn).toHaveBeenCalledTimes(1) |
| 95 | + expect(warn.mock.calls.map((x) => x[0])).toEqual(['blocklist-invalid']) |
| 96 | +}) |
| 97 | + |
| 98 | +it('can block classes generated by the safelist', () => { |
| 99 | + let config = { |
| 100 | + content: [{ raw: html`<div class="font-bold"></div>` }], |
| 101 | + safelist: [{ pattern: /^bg-red-(400|500)$/ }], |
| 102 | + blocklist: ['bg-red-500'], |
| 103 | + } |
| 104 | + |
| 105 | + return run('@tailwind utilities', config).then((result) => { |
| 106 | + return expect(result.css).toMatchCss(css` |
| 107 | + .bg-red-400 { |
| 108 | + --tw-bg-opacity: 1; |
| 109 | + background-color: rgb(248 113 113 / var(--tw-bg-opacity)); |
| 110 | + } |
| 111 | + .font-bold { |
| 112 | + font-weight: 700; |
| 113 | + } |
| 114 | + `) |
| 115 | + }) |
| 116 | +}) |
0 commit comments