Skip to content

Commit e12b1d2

Browse files
add tests (#1040)
1 parent 2232f12 commit e12b1d2

File tree

15 files changed

+173
-42
lines changed

15 files changed

+173
-42
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
},
3636
// rules which apply to JS, TS, etc.
3737
rules: {
38+
'import/no-nodejs-modules': 0,
3839
'filenames/match-regex': 0,
3940
'eslint-comments/no-unused-disable': 0,
4041
'react/prop-types': 0,

.github/workflows/build_config.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Testing custom transforms, formats, etc. for Style Dictionary
2+
3+
on:
4+
pull_request:
5+
branches-ignore:
6+
- 'changeset-release/**'
7+
workflow_dispatch:
8+
9+
jobs:
10+
changes:
11+
uses: ./.github/workflows/hasChanged.yml
12+
13+
build:
14+
needs: changes
15+
if: needs.changes.outputs.buildConfig == 'true' || github.event_name == 'workflow_dispatch'
16+
name: Test if custom transforms, formats, etc. are working
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: 20
26+
cache: 'npm'
27+
28+
- name: Install dependencies
29+
run: npm i --no-audit --no-fund --ignore-scripts
30+
31+
- name: Run test
32+
run: |
33+
npm run test:integration

.github/workflows/hasChanged.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ on:
1212
src:
1313
description: "Boolean value indicating change in src folder"
1414
value: ${{ jobs.hasChanged.outputs.src }}
15+
buildConfig:
16+
description: "Boolean value indicating changes of the build configuration in src folder"
17+
value: ${{ jobs.hasChanged.outputs.buildConfig }}
1518
scripts:
1619
description: "Boolean value indicating change in scripts folder"
1720
value: ${{ jobs.hasChanged.outputs.scripts }}
@@ -35,6 +38,7 @@ jobs:
3538
outputAffected: ${{ steps.filter.outputs.outputAffected }}
3639
tokens: ${{ steps.filter.outputs.tokens }}
3740
src: ${{ steps.filter.outputs.src }}
41+
buildConfig: ${{ steps.filter.outputs.buildConfig }}
3842
scripts: ${{ steps.filter.outputs.scripts }}
3943
buildScripts: ${{ steps.filter.outputs.buildScripts }}
4044
dependencies: ${{ steps.filter.outputs.dependencies }}
@@ -53,6 +57,8 @@ jobs:
5357
- 'src/**/**.ts'
5458
- 'src/**/**.json'
5559
- 'src/**/**.json5'
60+
buildConfig:
61+
- 'src/**/**.ts'
5662
scripts:
5763
- 'scripts/**'
5864
buildScripts:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage/
77
docs/public/
88
color-contrast-check.json
99
blob-report
10+
integration/build

integration/build.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {PrimerStyleDictionary} from '../src/PrimerStyleDictionary'
2+
import fs from 'fs'
3+
4+
describe('PrimerStyleDictionary', () => {
5+
const basePath = `./integration`
6+
const extendedSD = PrimerStyleDictionary.extend({
7+
source: [`${basePath}/tokens/**/*.json5`],
8+
platforms: {
9+
css: {
10+
prefix: 'PREFIX',
11+
transformGroup: 'css',
12+
buildPath: `${basePath}/build/css/`,
13+
files: [
14+
{
15+
options: {
16+
showFileHeader: false,
17+
},
18+
destination: 'variables.css',
19+
format: 'css/variables',
20+
},
21+
],
22+
},
23+
advancedCss: {
24+
prefix: 'PREFIX',
25+
transformGroup: 'css',
26+
buildPath: `${basePath}/build/css/`,
27+
files: [
28+
{
29+
options: {
30+
showFileHeader: false,
31+
},
32+
destination: 'advanced.css',
33+
format: 'css/advanced',
34+
},
35+
],
36+
},
37+
},
38+
})
39+
40+
extendedSD.cleanAllPlatforms()
41+
extendedSD.buildAllPlatforms()
42+
43+
it('it should transform with css/variables format', () => {
44+
const output = fs.readFileSync(`${basePath}/build/css/variables.css`, 'utf8')
45+
const expectedOutput = `:root {
46+
--prefix-base-color-blue-500: #2C29FF; /* The primary color for interactive elements. */
47+
--prefix-fg-color-link: #2C29FF;
48+
}
49+
`
50+
expect(output).toBe(expectedOutput)
51+
})
52+
53+
it('it should transform with css/advanced format', () => {
54+
const output = fs.readFileSync(`${basePath}/build/css/advanced.css`, 'utf8')
55+
const expectedOutput = `:root {
56+
--prefix-base-color-blue-500: #2c29ff; /* The primary color for interactive elements. */
57+
--prefix-fg-color-link: #2c29ff;
58+
}
59+
`
60+
expect(output).toBe(expectedOutput)
61+
})
62+
})

integration/tokens/base.json5

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
base: {
3+
color: {
4+
blue: {
5+
"500": {
6+
"$value": '#2C29FF',
7+
"$type": 'color',
8+
"$description": 'The primary color for interactive elements.',
9+
},
10+
}
11+
}
12+
}
13+
}

integration/tokens/functional.json5

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
fgColor: {
3+
link: {
4+
$value: '{base.color.blue.500}',
5+
$type: 'color',
6+
}
7+
}
8+
}

integrationbuild/css/variables.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:root {
2+
--prefix-base-color-blue-500: #2c29ff; /* The primary color for interactive elements. */
3+
--prefix-fg-color-link: #2c29ff;
4+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"prepack": "npm run build",
4141
"release": "changeset publish",
4242
"start:storybook": "npm run build && cd docs/storybook && npm run storybook",
43-
"test": "vitest run --coverage"
43+
"test": "vitest run --coverage",
44+
"test:integration": "vitest run -r integration"
4445
},
4546
"prettier": "@github/prettier-config",
4647
"devDependencies": {

src/formats/cssAdvanced.test.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('Format: tokens nested in media query', () => {
224224
/**
225225
* Test cases for formatting tokens with simple css variables
226226
*/
227-
it('Shows comment if option.description is true', () => {
227+
it('Shows comment if option.formatting.commentStyle is long or not set', () => {
228228
const input = getMockFormatterArguments({
229229
dictionary: getMockDictionary({
230230
tokens: {
@@ -238,7 +238,9 @@ describe('Format: tokens nested in media query', () => {
238238
},
239239
}),
240240
options: {
241-
descriptions: true,
241+
formatting: {
242+
commentStyle: 'long',
243+
},
242244
},
243245
file: {
244246
destination: 'size-fine.css',
@@ -253,19 +255,7 @@ describe('Format: tokens nested in media query', () => {
253255
},
254256
})
255257

256-
const expectedOutput = syncPrettier.format(
257-
` @media (prefers-color-scheme: light){
258-
:root {
259-
--red: transformedValue; /* This is a description */
260-
}
261-
}`,
262-
{parser: 'css', printWidth: 500},
263-
)
264-
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
265-
})
266-
267-
it('Hides comment if option.description is false or not set', () => {
268-
const input = getMockFormatterArguments({
258+
const inputUnset = getMockFormatterArguments({
269259
dictionary: getMockDictionary({
270260
tokens: {
271261
subgroup: {
@@ -277,23 +267,34 @@ describe('Format: tokens nested in media query', () => {
277267
},
278268
},
279269
}),
280-
options: {
281-
descriptions: false,
282-
},
270+
options: {}, // description not set
283271
file: {
284272
destination: 'size-fine.css',
285273
options: {
286274
showFileHeader: false,
287275
queries: [
288276
{
289-
query: '@media (prefers-color-scheme: dark)',
277+
query: '@media (prefers-color-scheme: light)',
290278
},
291279
],
292280
},
293281
},
294282
})
295283

296-
const inputUnset = getMockFormatterArguments({
284+
const expectedOutput = syncPrettier.format(
285+
` @media (prefers-color-scheme: light){
286+
:root {
287+
--red: transformedValue; /* This is a description */
288+
}
289+
}`,
290+
{parser: 'css', printWidth: 500},
291+
)
292+
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
293+
expect(cssAdvanced(inputUnset)).toStrictEqual(expectedOutput)
294+
})
295+
296+
it('Hides comment if option.formatting.commentStle is set to none', () => {
297+
const input = getMockFormatterArguments({
297298
dictionary: getMockDictionary({
298299
tokens: {
299300
subgroup: {
@@ -305,7 +306,11 @@ describe('Format: tokens nested in media query', () => {
305306
},
306307
},
307308
}),
308-
options: {}, // description not set
309+
options: {
310+
formatting: {
311+
commentStyle: 'none',
312+
},
313+
},
309314
file: {
310315
destination: 'size-fine.css',
311316
options: {
@@ -328,6 +333,5 @@ describe('Format: tokens nested in media query', () => {
328333
{parser: 'css', printWidth: 500},
329334
)
330335
expect(cssAdvanced(input)).toStrictEqual(expectedOutput)
331-
expect(cssAdvanced(inputUnset)).toStrictEqual(expectedOutput)
332336
})
333337
})

0 commit comments

Comments
 (0)