-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
119 lines (96 loc) · 3.6 KB
/
index.test.ts
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
import { describe, expect, it, beforeEach } from "bun:test"
import { css, classNames, cssRules, configure } from "./index"
beforeEach(() => {
document.head.innerHTML = ""
document.body.innerHTML = ""
configure({
target: document.head,
append: "each",
mode: "style",
})
})
describe("cssRules", () => {
it("should insert CSS into the DOM and return a generated class name", () => {
const template = [
"&& { background-color: ",
"; color: ",
"; }",
] as unknown as TemplateStringsArray
const params = ["red", "white"]
const className = cssRules(template, ...params)
// Assert that the generated class name is returned
expect(className).toMatch(/^css_\w+$/)
// Assert that the CSS has been inserted into the DOM
const styleElements = document.querySelectorAll("style")
expect(styleElements.length).toBe(1)
})
it("should handle template strings with no params", () => {
const template = [
"&& { background-color: red; }",
] as unknown as TemplateStringsArray
const className = cssRules(template)
// Assert that the CSS has been inserted into the DOM
const styleElements = document.querySelectorAll("style")
expect(styleElements.length).toBe(1)
// Assert that the generated class name is returned
expect(className).toMatch(/^css_\w+$/)
})
})
describe("classNames", () => {
it("should concatenate multiple strings", () => {
const result = classNames("foo", "bar", "baz")
expect(result).toBe("foo bar baz")
})
it("should include object keys with truthy values", () => {
const result = classNames("foo", { bar: true, baz: false, qux: true })
expect(result).toBe("foo bar qux")
})
it("should ignore null and undefined values", () => {
const result = classNames("foo", null, undefined, "bar")
expect(result).toBe("foo bar")
})
it("should return an empty string if no arguments are provided", () => {
const result = classNames()
expect(result).toBe("")
})
})
describe("css", () => {
it("should insert CSS into the DOM and return a function that returns the className", () => {
const template = [
"&& { background-color: ",
"; color: ",
"; }",
] as unknown as TemplateStringsArray
const params = ["red", "white"]
const classNames = css(template, ...params)
expect(classNames).toBeFunction()
// Assert that the generated class name is returned
let className = classNames()
expect(className).toMatch(/^css_\w+$/)
// Assert that the CSS has been inserted into the DOM
const styleElements = document.querySelectorAll("style")
expect(styleElements.length).toBe(1)
expect(classNames("Test")).toMatch(/^css_\w+ Test$/)
expect(classNames("Test", { isSelected: true, isReadonly: false })).toMatch(
/^css_\w+ Test isSelected$/
)
// .$() is a shorthand for classNames() without prefixing the generated class name.
expect(
classNames.$("Test", { isSelected: false }, { isReadonly: true })
).toMatch(/^Test isReadonly$/)
})
it("should handle template strings with no params", () => {
const template = [
"&& { background-color: red; }",
] as unknown as TemplateStringsArray
const classNames = css(template)
expect(classNames).toBeFunction()
// Assert that the generated class name is returned
let className = classNames()
expect(className).toMatch(/^css_\w+$/)
expect(classNames("Test")).toMatch(/^css_\w+ Test$/)
// Assert that the CSS has been inserted into the DOM
const styleElements = document.querySelectorAll("style")
expect(styleElements.length).toBe(1)
})
})