-
Notifications
You must be signed in to change notification settings - Fork 0
/
createComponent.test.tsx
310 lines (298 loc) · 7.38 KB
/
createComponent.test.tsx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import React from "react";
import TestRenderer from "react-test-renderer";
import { Button } from "./Example/Button";
import { Box } from "./Example/Box";
describe("createComponent", () => {
it("should parse any kind of option", () => {
expect(Button).not.toBeUndefined();
const target = TestRenderer.create(
<>
<Button size="tiny">text</Button>
<Button size="small">text</Button>
<Button size="medium">text</Button>
<Button size="large">text</Button>
</>
);
expect(target).toMatchInlineSnapshot(`
[
<button
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base padding-medium margin-medium text-medium font-something"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base large:text large:font large:padding key:abc:a key:abc:b key:abc:c key:num:n1 key:num:n2 key:num:n3"
>
text
</button>,
]
`);
});
it("should support as=`` prop", () => {
const target = TestRenderer.create(
<>
<Button as="div" title="hello" size="tiny">
text
</Button>
<Button as="span" onClick={() => {}} size="tiny">
text
</Button>
<Button as="p" color="red" size="tiny">
text
</Button>
</>
);
expect(target).toMatchInlineSnapshot(`
[
<div
className="btn hover:btn-hover hover:text-bold btn-base btn-has-title padding-tiny margin-tiny"
title="hello"
>
text
</div>,
<span
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny"
onClick={[Function]}
>
text
</span>,
<p
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny"
color="red"
>
text
</p>,
]
`);
});
it("should forward any native prop", () => {
const target = TestRenderer.create(
<Button
size="tiny"
title="something"
disabled={true}
type="submit"
autoFocus={true}
formNoValidate
onClick={() => alert(1)}
>
text
</Button>
);
expect(target).toMatchInlineSnapshot(`
<button
autoFocus={true}
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny btn-has-title btn-disabled btn-submit form-no-validate"
disabled={true}
formNoValidate={true}
onClick={[Function]}
title="something"
type="submit"
>
text
</button>
`);
});
it("should apply variants and addClass mixers", () => {
const target = TestRenderer.create(
<Button size="tiny" variant="outline" type="reset">
text
</Button>
);
expect(target).toMatchInlineSnapshot(`
<button
className="btn hover:btn-hover hover:text-bold btn-base bg-white-500 text-black border border-gray-400 padding-tiny margin-tiny size-tiny-outline-mix btn-reset"
type="reset"
>
text
</button>
`);
});
it("should apply variants and removeClass mixers", () => {
const target = TestRenderer.create(
<Button size="tiny" variant="filled" type="button">
text
</Button>
);
expect(target).toMatchInlineSnapshot(`
<button
className="btn bg-teal-200 text-white padding-tiny margin-tiny"
type="button"
>
text
</button>
`);
});
it("should forward more classnames without repeating", () => {
const target = TestRenderer.create(
<Button size="tiny" className="btn another-class more-classes">
text
</Button>
);
expect(target).toMatchInlineSnapshot(`
<button
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny another-class more-classes"
>
text
</button>
`);
});
it("should word with alias", () => {
const target = TestRenderer.create(
<Button size="tiny" v="outline" round>
text
</Button>
);
expect(target).toMatchInlineSnapshot(`
<button
className="btn hover:btn-hover hover:text-bold btn-base padding-tiny margin-tiny bg-white-500 text-black border border-gray-400 rounded-2xl"
>
text
</button>
`);
});
it("should mix with multiple options", () => {
const target = TestRenderer.create(
<>
<Button size="medium" anotherOption="on" disabled>
text
</Button>
<Button size="medium" anotherOption="off" disabled>
text
</Button>
</>
);
expect(target).toMatchInlineSnapshot(`
[
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function option-on btn-disabled any-anotherOptions-disabled-true"
disabled={true}
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function options-off btn-disabled any-anotherOptions-disabled-true"
disabled={true}
>
text
</button>,
]
`);
});
it("should react to data attribute values", () => {
const target = TestRenderer.create(
<>
<Button size="medium" data-something="a">
text
</Button>
<Button size="medium" data-something="b">
text
</Button>
</>
);
expect(target).toMatchInlineSnapshot(`
[
<button
className="btn hover:btn-hover hover:text-bold medium-stuff class-returned-by-function something-a"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function something-b"
>
text
</button>,
]
`);
});
it("should work with dynamic values", () => {
const target = TestRenderer.create(
<>
<Button size="medium" dynamicOptions={1}>
text
</Button>
<Button size="medium" dynamicOptions={99}>
text
</Button>
<Button size="medium" dynamicOptions={50}>
text
</Button>
</>
);
expect(target).toMatchInlineSnapshot(`
[
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function less-than-50"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function more-than-50"
>
text
</button>,
<button
className="btn hover:btn-hover hover:text-bold btn-base medium-stuff class-returned-by-function value-is-50 dynamic-options-50"
>
text
</button>,
]
`);
});
it("should parse simple component config", () => {
const target = TestRenderer.create(
<Box>
<Box display="flex" />
<Box display="inline" />
<Box display="block" />
</Box>
);
expect(target).toMatchInlineSnapshot(`
<div
className="box-base"
>
<div
className="box-base display-flex"
/>
<div
className="box-base display-inline"
/>
<div
className="box-base display-block"
/>
</div>
`);
});
it("should apply forward prop mixers", () => {
const somefunction = () => { console.log("hello")};
const target = TestRenderer.create(
<Box>
<Box display="inline" />
<Box display="inline" onClick={somefunction} />
</Box>
);
expect(target).toMatchInlineSnapshot(`
<div
className="box-base"
>
<div
className="box-base display-inline"
/>
<div
className="box-base display-inline mouse-pointer"
onClick={[Function]}
/>
</div>
`);
});
});