tailwindcss/tests/negative-prefix.test.js
Robin Malfait e572dc645a
Bump lightningcss dependency (#11388)
* bump `lightningcss`

* allow for non-standard selector combinators such as `::deep`

* just use Prettier when comparing CSS results

We were using 2 different engines where the stable one was not using
Lightning CSS and the Oxide one was using Lightning CSS.

To ensure that we didn't have to rewrite every single test expectation,
the `toMatchFormattedCss` parsed both the actual and expected value
using Lightning CSS (to make the result similar), then it used Prettier
to make it... pretty.

Right now we _only_ use Lightning CSS, which means that we can drop the
additional lightningcss format step and just use Prettier on both the
actual and expected values.

Pretty also only prettifies the CSS, it doesn't rewrite it. E.g.:
`@media (min-width: 768px)` will not be optimized to `@media (width >=
768px)`, that's something that Lightning CSS does for us.

This will require some changes in our test output, but it will be
consistent afterwards because there won't be hidden transformation steps
anymore. Because up until now it could be that the actual result was
`color: black` but the tests showed `color: #000` (because it is
shorter).

This change will reflect reality.

* update tests based on previous commit

* only use `toMatchFormattedCss` instead of `toMatchCss`

They both do the exact same thing right now. While `toMatchCss` is
shorter, `toMatchFormattedCss` makes a bit more sense since we are
comparing the Prettier results.

* update integration tests
2023-06-07 15:56:32 +02:00

342 lines
7.1 KiB
JavaScript

import { run, html, css } from './util/run'
test('using a negative prefix with a negative scale value', () => {
let config = {
content: [{ raw: html`<div class="mt-2 -mt-2"></div>` }],
theme: {
margin: {
2: '8px',
'-2': '-4px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-2 {
margin-top: -4px;
}
.mt-2 {
margin-top: 8px;
}
`)
})
})
test('using a negative scale value with a plugin that does not support dynamic negative values', () => {
let config = {
content: [{ raw: html`<div class="-opacity-50"></div>` }],
theme: {
opacity: {
'-50': '0.5',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-opacity-50 {
opacity: 0.5;
}
`)
})
})
test('using a negative prefix without a negative scale value', () => {
let config = {
content: [{ raw: html`<div class="mt-5 -mt-5"></div>` }],
theme: {
margin: {
5: '20px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-5 {
margin-top: -20px;
}
.mt-5 {
margin-top: 20px;
}
`)
})
})
test('being an asshole', () => {
let config = {
content: [{ raw: html`<div class="-mt-[10px]"></div>` }],
theme: {
margin: {
'-[10px]': '55px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-\[10px\] {
margin-top: 55px;
}
`)
})
})
test('being a real asshole', () => {
let config = {
content: [{ raw: html`<div class="-mt-[10px]"></div>` }],
theme: {
margin: {
'[10px]': '55px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-\[10px\] {
margin-top: -55px;
}
`)
})
})
test('a value that includes a variable', () => {
let config = {
content: [{ raw: html`<div class="mt-5 -mt-5"></div>` }],
theme: {
margin: {
5: 'var(--sizing-5)',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-5 {
margin-top: calc(var(--sizing-5) * -1);
}
.mt-5 {
margin-top: var(--sizing-5);
}
`)
})
})
test('a value that includes a calc', () => {
let config = {
content: [{ raw: html`<div class="mt-5 -mt-5"></div>` }],
theme: {
margin: {
5: 'calc(52px * -3)',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-5 {
margin-top: 156px;
}
.mt-5 {
margin-top: -156px;
}
`)
})
})
test('a value that includes min/max/clamp functions', () => {
let config = {
content: [{ raw: html`<div class="mt-min -mt-min mt-max -mt-max mt-clamp -mt-clamp"></div>` }],
theme: {
margin: {
min: 'min(100vmin, 3rem)',
max: 'max(100vmax, 3rem)',
clamp: 'clamp(1rem, 100vh, 3rem)',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-clamp {
margin-top: calc(-1 * clamp(1rem, 100vh, 3rem));
}
.-mt-max {
margin-top: calc(-1 * max(100vmax, 3rem));
}
.-mt-min {
margin-top: calc(-1 * min(100vmin, 3rem));
}
.mt-clamp {
margin-top: clamp(1rem, 100vh, 3rem);
}
.mt-max {
margin-top: max(100vmax, 3rem);
}
.mt-min {
margin-top: min(100vmin, 3rem);
}
`)
})
})
test('a keyword value', () => {
let config = {
content: [{ raw: html`<div class="-mt-auto mt-auto"></div>` }],
theme: {
margin: {
auto: 'auto',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.mt-auto {
margin-top: auto;
}
`)
})
})
test('a zero value', () => {
let config = {
content: [{ raw: html`<div class="mt-0 -mt-0"></div>` }],
theme: {
margin: {
0: '0',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-0,
.mt-0 {
margin-top: 0;
}
`)
})
})
test('a color', () => {
let config = {
content: [{ raw: html`<div class="-bg-red"></div>` }],
theme: {
colors: {
red: 'red',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css``)
})
})
test('arbitrary values', () => {
let config = {
content: [{ raw: html`<div class="-mt-[10px]"></div>` }],
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-\[10px\] {
margin-top: -10px;
}
`)
})
})
test('negating a negative scale value', () => {
let config = {
content: [{ raw: html`<div class="-mt-weird"></div>` }],
theme: {
margin: {
weird: '-15px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt-weird {
margin-top: 15px;
}
`)
})
})
test('negating a default value', () => {
let config = {
content: [{ raw: html`<div class="-mt"></div>` }],
theme: {
margin: {
DEFAULT: '15px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt {
margin-top: -15px;
}
`)
})
})
test('using a negative prefix with a negative default scale value', () => {
let config = {
content: [{ raw: html`<div class="mt -mt"></div>` }],
theme: {
margin: {
DEFAULT: '8px',
'-DEFAULT': '-4px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.-mt {
margin-top: -4px;
}
.mt {
margin-top: 8px;
}
`)
})
})
test('negating a default value with a configured prefix', () => {
let config = {
prefix: 'tw-',
content: [{ raw: html`<div class="tw--mt"></div>` }],
theme: {
margin: {
DEFAULT: '15px',
},
},
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.tw--mt {
margin-top: -15px;
}
`)
})
})
test('arbitrary value keywords should be ignored', () => {
let config = {
content: [{ raw: html`<div class="-mt-[auto]"></div>` }],
}
return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css``)
})
})