Skip to content

Commit 7d2d449

Browse files
chore: add global-css-to-css-module postcss unit tests (#13)
1 parent c738050 commit 7d2d449

File tree

10 files changed

+344
-149
lines changed

10 files changed

+344
-149
lines changed

src/transforms/globalCssToCssModule/__tests__/__snapshots__/globalCssToCssModule.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`globalCssToCssModule transforms correctly 1`] = `
1414
}
1515
1616
/* Let's see how you handle global top-level classes!*/
17-
:global(.theme-redesign) .kek {
17+
:global(.theme-light) .kek {
1818
color: red;
1919
}
2020
@@ -77,7 +77,7 @@ exports[`globalCssToCssModule transforms correctly 1`] = `
7777
margin: 0.5rem 0.625rem 0.5rem 0;
7878
padding: 0.25rem;
7979
80-
:global(.theme-redesign) & {
80+
:global(.theme-light) & {
8181
margin-top: 0;
8282
margin-bottom: 0;
8383
}
@@ -106,8 +106,8 @@ exports[`globalCssToCssModule transforms correctly 1`] = `
106106
/* background: var(--color-bg-1);*/
107107
background: var(--color-bg-1);
108108
109-
/* .theme-redesign & {*/
110-
:global(.theme-redesign) & {
109+
/* .theme-light & {*/
110+
:global(.theme-light) & {
111111
background: inherit;
112112
}
113113
}

src/transforms/globalCssToCssModule/__tests__/fixtures/Kek.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313
// Let's see how you handle global top-level classes!
14-
.theme-redesign .kek {
14+
.theme-light .kek {
1515
color: red;
1616
}
1717

@@ -44,8 +44,8 @@
4444
// background: var(--color-bg-1);
4545
background: var(--color-bg-1);
4646

47-
// .theme-redesign & {
48-
.theme-redesign & {
47+
// .theme-light & {
48+
.theme-light & {
4949
background: inherit;
5050
}
5151
}
@@ -68,7 +68,7 @@
6868
margin: 0.5rem 0.625rem 0.5rem 0;
6969
padding: 0.25rem;
7070

71-
.theme-redesign & {
71+
.theme-light & {
7272
margin-top: 0;
7373
margin-bottom: 0;
7474
}

src/transforms/globalCssToCssModule/css/postcssToCssModulePlugin.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

src/transforms/globalCssToCssModule/globalCssToCssModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from 'path'
33

44
import { Project } from 'ts-morph'
55

6-
import { getCssModuleExportNameMap } from './css/getCssModuleExportNameMap'
7-
import { transformFileToCssModule } from './css/transformFileToCssModule'
6+
import { getCssModuleExportNameMap } from './postcss/getCssModuleExportNameMap'
7+
import { transformFileToCssModule } from './postcss/transformFileToCssModule'
88
import { addClassNamesUtilImportIfNeeded } from './ts/classNamesUtility'
99
import { getNodesWithClassName } from './ts/getNodesWithClassName'
1010
import { STYLES_IDENTIFIER, processNodesWithClassName } from './ts/processNodesWithClassName'
@@ -41,11 +41,11 @@ export function globalCssToCssModule(project: Project): Promise<CodemodResult[]>
4141

4242
// TODO: add check if SCSS file doesn't exist and exit if it's not found.
4343
const sourceCss = readFileSync(cssFilePath, 'utf8')
44-
const exportNameMap = await getCssModuleExportNameMap(sourceCss)
4544
const { css: cssModuleSource, filePath: cssModuleFileName } = await transformFileToCssModule(
4645
sourceCss,
4746
cssFilePath
4847
)
48+
const exportNameMap = await getCssModuleExportNameMap(cssModuleSource)
4949

5050
processNodesWithClassName({
5151
exportNameMap,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getCssModuleExportNameMap } from '../getCssModuleExportNameMap'
2+
3+
describe('getCssModuleExportNameMap', () => {
4+
it('returns correct `exportNameMap`', async () => {
5+
const exportNameMap = await getCssModuleExportNameMap(`
6+
.repo-header {
7+
flex: none;
8+
9+
&:hover {
10+
opacity: 1;
11+
}
12+
13+
&__button {
14+
margin-top: 1px;
15+
}
16+
17+
&--alert {
18+
border-width: 1px 0;
19+
}
20+
21+
.navbar-nav {
22+
white-space: nowrap;
23+
}
24+
}
25+
26+
.spacer {
27+
flex: 1 1 0;
28+
}
29+
`)
30+
31+
expect(exportNameMap).toEqual({
32+
'repo-header': 'repoHeader',
33+
'repo-header__button': 'repoHeaderButton',
34+
'repo-header--alert': 'repoHeaderAlert',
35+
'navbar-nav': 'navbarNav',
36+
spacer: 'spacer',
37+
})
38+
})
39+
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { transformFileToCssModule } from '../transformFileToCssModule'
2+
3+
const replaceWhitespace = (value: string) => value.replace(/\s+/g, ' ').trim()
4+
5+
describe('transformFileToCssModule', () => {
6+
it('correctly transforms provided CSS to CSS module', async () => {
7+
const cssSource = `
8+
// .repo-header comment
9+
.repo-header {
10+
flex: none;
11+
12+
&:hover {
13+
opacity: 1;
14+
}
15+
16+
// &__button comment
17+
&__button {
18+
margin-top: 1px;
19+
}
20+
21+
&--alert {
22+
border-width: 1px 0;
23+
24+
.theme-dark & {
25+
background: black;
26+
}
27+
}
28+
29+
.navbar-nav {
30+
white-space: nowrap;
31+
}
32+
}
33+
34+
.theme-light {
35+
.spacer {
36+
flex: 1 1 0;
37+
}
38+
}
39+
`
40+
41+
const expectedCssModuleSource = `
42+
/* .repo-header comment*/
43+
.repo-header {
44+
flex: none;
45+
46+
&:hover {
47+
opacity: 1;
48+
}
49+
50+
&--alert {
51+
border-width: 1px 0;
52+
53+
:global(.theme-dark) & {
54+
background: black;
55+
}
56+
}
57+
58+
:global(.navbar-nav) {
59+
white-space: nowrap;
60+
}
61+
}
62+
63+
/* &__button comment*/
64+
.button {
65+
margin-top: 1px;
66+
}
67+
68+
:global(.theme-light) {
69+
.spacer {
70+
flex: 1 1 0;
71+
}
72+
}
73+
`
74+
75+
const { css, filePath } = await transformFileToCssModule(cssSource, 'whatever.scss')
76+
77+
expect(replaceWhitespace(css)).toEqual(replaceWhitespace(expectedCssModuleSource))
78+
expect(filePath).toEqual('whatever.module.scss')
79+
})
80+
})

src/transforms/globalCssToCssModule/css/createCssProcessor.ts renamed to src/transforms/globalCssToCssModule/postcss/createCssProcessor.ts

File renamed without changes.

src/transforms/globalCssToCssModule/css/getCssModuleExportNameMap.ts renamed to src/transforms/globalCssToCssModule/postcss/getCssModuleExportNameMap.ts

File renamed without changes.

0 commit comments

Comments
 (0)