Skip to content

Commit

Permalink
test: use cssesc to test with escaped class names
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang committed Jan 11, 2024
1 parent 4b2e9ca commit b44696e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -55,6 +55,7 @@
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.11.5",
"@intlify/vue-i18n-loader": "^3.0.0",
"@types/cssesc": "^3.0.2",
"@types/estree": "^0.0.45",
"@types/hash-sum": "^1.0.0",
"@types/jest": "^26.0.13",
Expand All @@ -65,6 +66,7 @@
"cache-loader": "^4.1.0",
"conventional-changelog-cli": "^2.1.1",
"css-loader": "^4.3.0",
"cssesc": "^3.0.0",
"file-loader": "^6.1.0",
"html-webpack-plugin": "^4.5.0",
"html-webpack-plugin-v5": "npm:html-webpack-plugin@^5.3.2",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions test/advanced.spec.ts
@@ -1,5 +1,6 @@
import { SourceMapConsumer } from 'source-map'
import { fs as mfs } from 'memfs'
import cssesc from 'cssesc'
import {
bundle,
mockBundleAndRun,
Expand All @@ -14,7 +15,7 @@ test('support chaining with other loaders', async () => {
const { componentModule } = await mockBundleAndRun({
entry: 'basic.vue',
modify: (config) => {
config!.module!.rules[0] = {
config!.module!.rules![0] = {
test: /\.vue$/,
use: [DEFAULT_VUE_USE, require.resolve('./mock-loaders/js')],
}
Expand All @@ -28,7 +29,7 @@ test.skip('inherit queries on files', async () => {
const { componentModule } = await mockBundleAndRun({
entry: 'basic.vue?change',
modify: (config) => {
config!.module!.rules[0] = {
config!.module!.rules![0] = {
test: /\.vue$/,
use: [DEFAULT_VUE_USE, require.resolve('./mock-loaders/query')],
}
Expand Down Expand Up @@ -199,9 +200,10 @@ test('support rules with oneOf', async () => {
const { window, instance } = await run('css-modules-simple.vue')

const className = instance.$style.red
expect(className).toMatch(/^red_\w{5}/)
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
expect(className).toMatch(/^red_.{5}/)
style = normalizeNewline(window.document.querySelector('style')!.textContent!)
expect(style).toContain('.' + className + ' {\n color: red;\n}')
expect(style).toContain('.' + escapedClassName + ' {\n color: red;\n}')
})

test.todo('should work with eslint loader')
Expand Down
12 changes: 9 additions & 3 deletions test/style.spec.ts
@@ -1,3 +1,4 @@
import cssesc from 'cssesc'
import {
mockBundleAndRun,
genId,
Expand Down Expand Up @@ -130,6 +131,7 @@ test('CSS Modules', async () => {

// get local class name
const className = instance.$style.red
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
expect(className).toMatch(regexToMatch)

// class name in style
Expand All @@ -140,7 +142,7 @@ test('CSS Modules', async () => {
})
.join('\n')
style = normalizeNewline(style)
expect(style).toContain('.' + className + ' {\n color: red;\n}')
expect(style).toContain('.' + escapedClassName + ' {\n color: red;\n}')

// animation name
const match = style.match(/@keyframes\s+(\S+)\s+{/)
Expand All @@ -151,9 +153,12 @@ test('CSS Modules', async () => {

// default module + pre-processor + scoped
const anotherClassName = instance.$style.red
const escapedAnotherClassName = cssesc(instance.$style.red, {
isIdentifier: true,
})
expect(anotherClassName).toMatch(regexToMatch)
const id = 'data-v-' + genId('css-modules.vue')
expect(style).toContain('.' + anotherClassName + '[' + id + ']')
expect(style).toContain('.' + escapedAnotherClassName + '[' + id + ']')
}

// default ident
Expand Down Expand Up @@ -194,8 +199,9 @@ test('CSS Modules Extend', async () => {
})

expect(instance.$el.className).toBe(instance.$style.red)
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
const style = window.document.querySelectorAll('style')![1]!.textContent
expect(style).toContain(`.${instance.$style.red} {\n color: #FF0000;\n}`)
expect(style).toContain(`.${escapedClassName} {\n color: #FF0000;\n}`)
})

test('v-bind() in CSS', async () => {
Expand Down

0 comments on commit b44696e

Please sign in to comment.