Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge tests of all formatters that got replaced by css/advanced into one file #935

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 136 additions & 6 deletions src/formats/cssAdvanced.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,71 @@ import {format} from 'prettier'
import type {TransformedToken} from 'style-dictionary'

describe('Format: tokens nested in media query', () => {
it('Wraps in :root if no media query defined', () => {
/**
* Test cases for formatting tokens with simple css variables
*/
it('Formats tokens as css variables with default :root selector', () => {
const input = getMockFormatterArguments()
const expectedOutput = format(
`:root {
--red: transformedValue;
}`,
{
parser: 'css',
printWidth: 500,
},
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})

it('Formats tokens with custom selectors', () => {
const input = getMockFormatterArguments({
file: {
destination: 'test.css',
options: {
showFileHeader: false,
selector: `[data-color-mode="dark"]`,
},
},
})
const expectedOutput = format(
` [data-color-mode="dark"] {
--red: transformedValue;
}`,
{parser: 'css', printWidth: 500},
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})

/**
* Test cases for formatting using media queries
*/
it('Formats all tokens with screen media query and default selector', () => {
const input = getMockFormatterArguments({
file: {
destination: 'tokens.ts',
options: {
showFileHeader: false,
queries: [
{
query: '@media screen',
},
],
},
},
})
const expectedOutput = format(
` @media screen {
:root {
--red: transformedValue;
}
}`,
{parser: 'css', printWidth: 500},
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})

it('Wrap all tokens in screen media query', () => {
it('Formats all tokens with screen media query and custom selector', () => {
const input = getMockFormatterArguments({
file: {
destination: 'tokens.ts',
Expand All @@ -24,14 +77,15 @@ describe('Format: tokens nested in media query', () => {
queries: [
{
query: '@media screen',
selector: '[data-color-mode="dark"]',
},
],
},
},
})
const expectedOutput = format(
` @media screen {
:root {
[data-color-mode="dark"] {
--red: transformedValue;
}
}`,
Expand All @@ -40,7 +94,7 @@ describe('Format: tokens nested in media query', () => {
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})

it('Ignore if no matching media query is found', () => {
it('Outputs nothing if no matching media query is found', () => {
const input = getMockFormatterArguments({
file: {
destination: 'size-coarse.css',
Expand All @@ -58,7 +112,7 @@ describe('Format: tokens nested in media query', () => {
expect(cssAdvanced(input)).toStrictEqual('')
})

it('Wraps in defined media query if files match', () => {
it('Formats only matching tokens in defined media query using matcher', () => {
const input = getMockFormatterArguments({
dictionary: getMockDictionary({
tokens: {
Expand All @@ -67,6 +121,10 @@ describe('Format: tokens nested in media query', () => {
name: 'tokens-subgroup-gap',
filePath: 'size-fine.json',
}),
bigGap: getMockToken({
name: 'tokens-subgroup-gap',
filePath: 'size-coarse.json',
}),
},
},
}),
Expand All @@ -93,8 +151,80 @@ describe('Format: tokens nested in media query', () => {
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})
/**
* Test cases for formatting using selectors
*/
it('Formats all tokens with custom selectors', () => {
const input = getMockFormatterArguments({
file: {
destination: 'dark.css',
options: {
showFileHeader: false,
queries: [
{
selector:
'[data-color-mode="dark"][data-dark-theme="dark"], [data-color-mode="auto"][data-light-theme="dark"]',
},
],
},
},
})
const expectedOutput = format(
` [data-color-mode="dark"][data-dark-theme="dark"],
[data-color-mode="auto"][data-light-theme="dark"] {
--red: transformedValue;
}`,
{parser: 'css', printWidth: 500},
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})

it('Show comment if option.description is true', () => {
it('Formats only matching tokens with custom selectors', () => {
const input = getMockFormatterArguments({
dictionary: getMockDictionary({
tokens: {
subgroup: {
gap: getMockToken({
name: 'tokens-subgroup-gap',
filePath: 'size-fine.json',
}),
bigGap: getMockToken({
name: 'tokens-subgroup-gap',
filePath: 'size-coarse.json', // should not show up in output
}),
},
},
}),
file: {
destination: 'dark.css',
options: {
showFileHeader: false,
queries: [
{
selector:
'[data-color-mode="dark"][data-dark-theme="dark"], [data-color-mode="auto"][data-light-theme="dark"]',
matcher: (token: TransformedToken) => token.filePath.includes('size.json'), // no token in this file
},
{
selector: '[data-color-mode="auto"][data-dark-theme="dark"]',
matcher: (token: TransformedToken) => token.filePath.includes('size-fine.json'),
},
],
},
},
})
const expectedOutput = format(
`[data-color-mode="auto"][data-dark-theme="dark"] {
--tokens-subgroup-gap: transformedValue;
}`,
{parser: 'css', printWidth: 500},
)
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
})
/**
* Test cases for formatting tokens with simple css variables
*/
it('Shows comment if option.description is true', () => {
const input = getMockFormatterArguments({
dictionary: getMockDictionary({
tokens: {
Expand Down
77 changes: 0 additions & 77 deletions src/formats/cssThemed.test.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/formats/cssVariables.test.ts

This file was deleted.