-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.test.ts
160 lines (132 loc) · 3.75 KB
/
alias.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
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
153
154
155
156
157
158
159
160
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
import { alias, aliasMap, lookup } from './alias.ts'
Deno.test({
name: '[alias] should substitute classes',
fn() {
const map = aliasMap([alias('alias-name', 'base-styles to-apply')])
const input = 'alias-name alias-name-foo-bar alias-name-foo-bar-baz'
const output = 'base-styles to-apply base-styles to-apply base-styles to-apply'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] should substitute classes with newlines',
fn() {
const map = aliasMap([alias('alias-name', 'base-styles to-apply')])
const input = `
alias-name
alias-name-foo-bar alias-name-foo-bar-baz`
const output = 'base-styles to-apply base-styles to-apply base-styles to-apply'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] should apply variations',
fn() {
const map = aliasMap([
alias('alias-name', {
base: 'base-styles to-apply',
variations: [
[{ key: 'foo', style: 'apply-foo' }],
[{ key: 'bar', style: 'apply-bar' }],
[{ key: 'baz', style: 'apply-baz' }],
],
}),
])
const input = 'alias-name alias-name-foo-bar alias-name-foo-bar-baz'
const output = 'base-styles to-apply base-styles to-apply apply-foo apply-bar base-styles to-apply apply-foo apply-bar apply-baz'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] should respect variation groups and defaults',
fn() {
const map = aliasMap([
alias('btn', {
base: 'button-class',
variations: [
// Color-related variations
[
{ key: 'gray', style: 'bg-gray', isDefault: true },
{ key: 'primary', style: 'bg-primary' },
],
// Padding-related variations
[
{ key: null, style: 'p-2', isDefault: true },
{ key: 'spacious', style: 'p-4' },
],
],
}),
])
const input = 'btn btn-gray btn-spacious btn-primary-spacious'
const output = 'button-class bg-gray p-2 button-class bg-gray p-2 button-class bg-gray p-4 button-class bg-primary p-4'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] recursive matching',
fn() {
const map = aliasMap([
// Aliases can reference each other
alias('foo', 'bar'),
alias('bar', 'baz-foo'),
// Even if the reference is recursive
alias('baz', {
base: null,
variations: [
[
{ key: 'foo', style: 'baz-bar' },
{ key: 'bar', style: 'wow-i-was-referenced-recursively' },
],
],
}),
])
const input = 'foo'
const output = 'wow-i-was-referenced-recursively'
assertEquals(lookup(map, input), output)
},
})
// TODO tests for warnings if the same styles are added more than once
Deno.test({
name: '[alias] complex alias matching should be supported',
fn() {
const map = aliasMap([
alias('size', sections => {
const size = sections[0]
if (!size) return null // don't match this alias
return `w-${size} h-${size}`
}),
])
const input = 'size-20'
const output = 'w-20 h-20'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] should cancel match and keep looking if complex alias returns null',
fn() {
const map = aliasMap([
alias('sized-box', sections => {
const size = sections[0]
if (!size) return null
return `box w-${size} h-${size}`
}),
alias('sized', {
base: 'base-class',
variations: [[{ key: 'box', style: 'box-class' }]],
}),
])
const input = 'sized-box'
const output = 'base-class box-class'
assertEquals(lookup(map, input), output)
},
})
Deno.test({
name: '[alias] should not allow infinitely recursive resolutions',
fn() {
const map = aliasMap([alias('btn', 'btn')])
const input = 'btn red'
const output = 'btn red'
assertEquals(lookup(map, input), output)
},
})