-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathindex.test.ts
125 lines (105 loc) · 3.98 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
120
121
122
123
124
125
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
detokenize,
loadTheme,
splitStyles,
loadStyles,
configureLoadStyles,
replaceTokensWithVariables,
type IThemingInstruction
} from '../index';
describe(detokenize.name, () => {
it('handles colors', () => {
expect(detokenize('"[theme:name, default: #FFF]"')).toEqual('#FFF');
expect(detokenize('"[theme: name, default: #FFF]"')).toEqual('#FFF');
expect(detokenize('"[theme: name , default: #FFF ]"')).toEqual('#FFF');
});
it('handles rgba', () => {
expect(detokenize('"[theme:name, default: rgba(255,255,255,.5)]"')).toEqual('rgba(255,255,255,.5)');
});
it('handles fonts', () => {
expect(detokenize('"[theme:name, default: "Segoe UI"]"')).toEqual('"Segoe UI"');
});
it('respects theme', () => {
loadTheme({
color: 'red'
});
try {
expect(detokenize('"[theme:color, default: #FFF]"')).toEqual('red');
expect(detokenize('"[theme: color , default: #FFF]"')).toEqual('red');
} finally {
loadTheme(undefined);
}
});
it('ignores malformed themes', () => {
expect(detokenize('"[theme:name, default: "Segoe UI"]')).toEqual('"[theme:name, default: "Segoe UI"]');
expect(detokenize('"[theme:]"')).toEqual('"[theme:]"');
});
it('translates missing themes', () => {
expect(detokenize('"[theme:name]"')).toEqual('inherit');
});
});
describe(replaceTokensWithVariables.name, () => {
it('handles colors', () => {
expect(replaceTokensWithVariables('"[theme:name, default: #FFF]"')).toEqual('var(--name, #FFF)');
expect(replaceTokensWithVariables('"[theme: name, default: #FFF]"')).toEqual('var(--name, #FFF)');
expect(replaceTokensWithVariables('"[theme: name , default: #FFF ]"')).toEqual('var(--name, #FFF)');
});
it('handles rgba', () => {
expect(replaceTokensWithVariables('"[theme:name, default: rgba(255,255,255,.5)]"')).toEqual(
'var(--name, rgba(255,255,255,.5))'
);
});
it('handles fonts', () => {
expect(replaceTokensWithVariables('"[theme:name, default: "Segoe UI"]"')).toEqual(
'var(--name, "Segoe UI")'
);
});
it('ignores malformed themes', () => {
expect(replaceTokensWithVariables('"[theme:name, default: "Segoe UI"]')).toEqual(
'"[theme:name, default: "Segoe UI"]'
);
expect(replaceTokensWithVariables('"[theme:]"')).toEqual('"[theme:]"');
});
it('translates missing defaults', () => {
expect(replaceTokensWithVariables('"[theme:name]"')).toEqual('var(--name)');
});
});
describe(splitStyles.name, () => {
it('splits non-themable CSS', () => {
const cssString: string = '.sampleClass\n{\n color: #FF0000;\n}\n';
const arr: IThemingInstruction[] = splitStyles(cssString);
expect(arr).toHaveLength(1);
expect(arr[0].rawString).toEqual(cssString);
});
it('splits themable CSS', () => {
const arr: IThemingInstruction[] = splitStyles(
'.firstClass { color: "[theme: firstColor ]";}\n' +
' .secondClass { color: "[theme:secondColor, default: #AAA]";}\n .coach { color: #333; }'
);
expect(arr).toHaveLength(5);
for (let i: number = 0; i < arr.length; i++) {
if (i % 2 === 0) {
// even index should be a string component
expect(typeof arr[i].rawString).toEqual('string');
} else {
// odd index should be a theme instruction object
expect(typeof arr[i].theme).toEqual('string');
}
}
});
});
describe(loadStyles.name, () => {
it('passes the styles to loadStyles override callback', () => {
const expected: string = 'xxx.foo { color: #FFF }xxx';
let subject: string | undefined = undefined;
const callback: (str: string) => void = (str: string) => {
subject = 'xxx' + str + 'xxx';
};
configureLoadStyles(callback);
loadStyles('.foo { color: "[theme:fooColor, default: #FFF]" }');
expect(subject).toEqual(expected);
configureLoadStyles(undefined);
});
});