-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcss.test.js
More file actions
152 lines (123 loc) · 4.05 KB
/
Copy pathcss.test.js
File metadata and controls
152 lines (123 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
getCurrentVersion,
getPackageStats,
getSelectorDiff,
getVariableDiff,
currentVersionDeprecations,
} from './utils/css'
import fs from 'fs'
import semver from 'semver'
import {createRequire} from 'module'
let selectorsDiff, variablesDiff, version
const showOnFocusSelector = '.show-on-focus {'
beforeAll(async () => {
selectorsDiff = getSelectorDiff()
variablesDiff = getVariableDiff()
version = getCurrentVersion()
})
describe('css', () => {
it('The support.css file contains no compiled css', () => {
const supportStats = getPackageStats('support')
expect(supportStats.size).toEqual(0)
})
})
describe('utilities', () => {
it('keeps show-on-focus declarations important', () => {
const content = fs.readFileSync('./src/utilities/visibility-display.scss', 'utf8')
const showOnFocus = getShowOnFocus(content)
if (showOnFocus === null) {
throw new Error('Could not find .show-on-focus block')
}
for (const declaration of [
'position: absolute !important;',
'width: 1px !important;',
'height: 1px !important;',
'padding: 0 !important;',
'overflow: hidden !important;',
'clip: rect(1px, 1px, 1px, 1px) !important;',
'border: 0 !important;',
]) {
expect(showOnFocus).toContain(declaration)
}
})
it('contains !important annotations only in show-on-focus', () => {
const files = getFiles('./src/utilities').filter(file => file.endsWith('.scss'))
for (const file of files) {
const content = stripShowOnFocus(fs.readFileSync(file, 'utf8'))
if (content.includes('!important')) {
throw new Error(`${file} contains !important outside show-on-focus`)
}
}
})
})
describe('deprecations', () => {
it('expects deprecations and their replacement to not be equal.', () => {
const deprecations = currentVersionDeprecations()
Object.keys(deprecations['selectors']).forEach(deprecation => {
const replacement = deprecations['selectors'][deprecation]
expect(deprecation).not.toEqual(replacement)
})
})
})
describe('classnames', () => {
let classNames
beforeAll(async () => {
classNames = (await import('../dist/classnames.js')).default
})
it('exports a non-empty Set', () => {
expect(classNames).toBeInstanceOf(Set)
expect(classNames.size).toBeGreaterThan(0)
})
it('contains known classnames', () => {
expect(classNames.has('btn')).toBe(true)
expect(classNames.has('Box-body')).toBe(true)
expect(classNames.has('d-flex')).toBe(true)
})
it('contains bare tokens without a leading dot', () => {
for (const className of classNames) {
expect(className.startsWith('.')).toBe(false)
}
})
it('exposes the same Set from the CommonJS build', () => {
const require = createRequire(import.meta.url)
const cjsClassNames = require('../dist/classnames.cjs')
expect(cjsClassNames).toBeInstanceOf(Set)
expect([...cjsClassNames].sort()).toEqual([...classNames].sort())
})
})
function getFiles(directory) {
return fs.readdirSync(directory, {withFileTypes: true}).flatMap(entry => {
const file = `${directory}/${entry.name}`
return entry.isDirectory() ? getFiles(file) : file
})
}
function stripShowOnFocus(content) {
const showOnFocus = getShowOnFocusRange(content)
if (showOnFocus === null) {
return content
}
return `${content.slice(0, showOnFocus.start)}${content.slice(showOnFocus.end)}`
}
function getShowOnFocus(content) {
const range = getShowOnFocusRange(content)
return range === null ? null : content.slice(range.start, range.end)
}
function getShowOnFocusRange(content) {
const start = content.indexOf(showOnFocusSelector)
if (start === -1) {
return null
}
const blockStart = content.indexOf('{', start)
let depth = 0
for (let index = blockStart; index < content.length; index++) {
if (content[index] === '{') {
depth++
} else if (content[index] === '}') {
depth--
}
if (depth === 0) {
return {start, end: index + 1}
}
}
throw new Error('Could not find closing brace for .show-on-focus block')
}