postcss-first-contiguous/index.test.js
2023-12-13 20:09:55 -06:00

90 lines
2.1 KiB
JavaScript

const postcss = require('postcss')
const { equal } = require('node:assert')
const { test } = require('node:test')
const plugin = require('./')
async function run(input, output, opts = {}) {
const result = await postcss([plugin(opts)]).process(input, {
from: undefined
})
equal(result.css, output)
equal(result.warnings().length, 0)
}
test('applies :first-contiguous', async () => {
await run(':first-contiguous(.a){}', ':is(:not(.a)+.a,.a:first-child){}', {})
})
test('includes parent of :first-contiguous selector', async () => {
await run(
'.parent :first-contiguous(.a){}',
'.parent :is(:not(.a)+.a,.a:first-child){}',
{}
)
})
test('applies :first-contiguous multiple times when comma separated', async () => {
await run(
':first-contiguous(.a, .b){}',
':is(:not(.a)+.a,.a:first-child),:is(:not(.b)+.b,.b:first-child){}',
{}
)
})
test('applies :first-contiguous with descenders', async () => {
await run(
':first-contiguous(.a) .b{}',
':is(:not(.a)+.a,.a:first-child) .b{}',
{}
)
})
test('applies :first-contiguous with multiple nodes and with descenders', async () => {
await run(
':first-contiguous(.a, .b) .c{}',
':is(:not(.a)+.a,.a:first-child) .c,:is(:not(.b)+.b,.b:first-child) .c{}',
{}
)
})
test('applies :last-contiguous', async () => {
await run(
':last-contiguous(.a){}',
':is(.a:has(+:not(.a)),.a:last-child){}',
{}
)
})
test('includes parent of :last-contiguous selector', async () => {
await run(
'.parent :last-contiguous(.a){}',
'.parent :is(.a:has(+:not(.a)),.a:last-child){}',
{}
)
})
test('applies :last-contiguous multiple times when comma separated', async () => {
await run(
':last-contiguous(.a, .b){}',
':is(.a:has(+:not(.a)),.a:last-child),:is(.b:has(+:not(.b)),.b:last-child){}',
{}
)
})
test('applies :last-contiguous with descenders', async () => {
await run(
':last-contiguous(.a) .b{}',
':is(.a:has(+:not(.a)),.a:last-child) .b{}',
{}
)
})
test('applies :last-contiguous with multiple nodes and with descenders', async () => {
await run(
':last-contiguous(.a, .b) .c{}',
':is(.a:has(+:not(.a)),.a:last-child) .c,:is(.b:has(+:not(.b)),.b:last-child) .c{}',
{}
)
})