-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
class.test.ts
61 lines (51 loc) · 2.73 KB
/
class.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
import { ClassParser } from '../../src/utils/parser';
describe('ClassParser', () => {
it('should remove duplicated classes', () => {
expect(new ClassParser('font-bold font-bold bg-red-300').parse().length).toEqual(2);
expect(new ClassParser('font-bold font-bold bg-red-300').parse(false).length).toEqual(3);
});
it('parse', () => {
const classes = 'font-bold text-green-300 dark:font-medium dark:p-4 -sm:border +sm:float-right md: hover: (bg-black-300 text-gray-200 text-lg dark:(bg-black-300 text-gray-200)) md:text-red-500 hover: text-red-300 text-green-300 sm:hover:bg-red-500 dark:hover:bg-black-300 sm:dark:hover:bg-gray-300 abc bg-cool-gray-300 bg-hex-fff';
const parser = new ClassParser(undefined, ':', ['dark', '-sm', '+sm', 'md', 'hover', 'sm']);
expect(parser.parse().length).toBe(0);
parser.classNames = classes;
expect(parser.parse()).toMatchSnapshot('class parser');
});
it('parse important', () => {
const classes = '!text-green-300 font-bold !hover:(p-4 bg-red-500) focus:(!border float-right)';
const parser = new ClassParser(classes, ':', ['hover', 'focus']);
expect(parser.parse()).toMatchSnapshot('parse important');
});
it('parse alias', () => {
const classes = 'font-bold *hstack sm:(*vstack text-green-200)';
const parser = new ClassParser(classes, ':', ['sm']);
expect(parser.parse()).toMatchSnapshot('parse alias');
});
it('parse bad class', () => {
const classes = 'css`height:calc-(100 float-left w-250px h-full bg-hex-f7f7fa overflow-y-auto height:calc-(100';
const parser = new ClassParser(classes, ':', []);
expect(parser.parse()).toMatchSnapshot('bad classes');
});
it('parse bad class with quotes', () => {
const classes = '${ active ? \'text-green-400\' : \'text-orange-400\' }';
const parser = new ClassParser(classes, ':', []);
expect(parser.parse()).toMatchSnapshot('bad classes');
});
it('parse bad class with bracket', () => {
const classes = 'xyz() p-5 bg-red-500 xyz-() xy-( xy) bg-yellow-600';
const parser = new ClassParser(classes, ':', []);
expect(parser.parse()).toMatchSnapshot('bad bracket');
});
it('parse bad class with half bracket', () => {
const classes = 'abc( text-lg font-bold';
const parser = new ClassParser(classes, ':', []);
expect(parser.parse()).toMatchSnapshot('bad half bracket');
});
it('parse function with parameter deconstruction', () => {
const classes = 'function ComponentA({ name } Props) { return div className= grid }';
const result = new ClassParser(classes, ':', []).parse();
const findGrid = (els: any[]) => els.find(item => item.raw === 'grid');
expect(findGrid(result)).toBeTruthy();
expect(result).toMatchSnapshot('jsx function component');
});
});