This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.test.js
183 lines (159 loc) · 3.96 KB
/
index.test.js
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
import styledMap, { mapToTheme, _convertToObject, _dotProp } from './index';
const theme = {
buttonColor: {
primary: '#0af',
info: '#ff0',
other: '#aab',
warning: '#c00',
},
};
const nestedTheme = {
button: {
foreground: {
primary: '#0af',
default: '#fff',
},
},
};
const props = { theme };
const styles = {
primary: '#0af',
warning: '#c00',
lastItem: '#c0c',
};
const stylesTTL = _convertToObject`
primary: #0af;
warning: #c00;
lastItem: #c0c;
`(props);
const stylesWithDefault = {
default: '#c0c',
primary: '#0af',
warning: '#c00',
};
const stylesWithDefaultTTL = _convertToObject`
default: #c0c;
primary: #0af;
warning: #c00;
`(props);
describe('styledMap', () => {
it('returns a function', () => {
const map = styledMap();
expect(typeof styledMap()).toBe('function');
});
it('returns a matching prop from a component', () => {
const result = styledMap(styles)({
primary: true,
other: 'blah',
});
expect(result).toEqual(styles['primary']);
});
it('returns the last matching', () => {
const result = styledMap(styles)({
primary: true,
warning: true,
yellow: true,
});
expect(result).toEqual(styles['warning']);
});
it('returns the default prop if nothing matches', () => {
const result = styledMap(stylesWithDefault)({
other: 'blah',
another: 'blah',
});
expect(result).toEqual(stylesWithDefault['default']);
});
it('returns the last prop if no default is specified', () => {
const result = styledMap(styles)({
other: 'blah',
another: 'blah',
});
expect(result).toEqual(styles['lastItem']);
});
it('ignores falsey props', () => {
const result = styledMap(stylesWithDefault)({
primary: false,
warning: null,
other: 'blah',
});
expect(result).toEqual(stylesWithDefault['default']);
});
it('uses prop values instead of keys if the first arg is a string', () => {
const result = styledMap('type', styles)({
type: 'primary',
other: 'blah',
});
expect(result).toEqual(styles['primary']);
});
it('handles css syntax', () => {
const result = styledMap`
first: #0af;
second: 13px;
`({
first: true,
});
expect(result).toBe('#0af');
const result2 = styledMap`
first: #0af;
second: 13px;
`({});
expect(result2).toBe('13px');
});
it('css syntax handles missing final semicolon and whitespace', () => {
const result = styledMap`
first: #0af;
second: 100%
`({
second: true,
});
expect(result).toBe('100%');
});
});
describe('mapToTheme', () => {
it('correctly maps theme to props', () => {
const result = mapToTheme('buttonColor')({ theme })({
primary: false,
info: true,
});
expect(result).toEqual('#ff0');
});
it('supports nested object properties', () => {
const result = mapToTheme('button.foreground')({ theme: nestedTheme })({
primary: true,
});
expect(result).toEqual(_dotProp('button.foreground.primary', nestedTheme));
});
it('uses the prop value when passed a second argument', () => {
const result = mapToTheme('buttonColor', 'kind')({theme})({
kind: 'other'
});
expect(result).toEqual('#aab');
})
});
describe('_convertToObject', () => {
it('converts the css-like syntax to a style object', () => {
expect(stylesTTL).toMatchObject(styles);
expect(stylesWithDefaultTTL).toMatchObject(stylesWithDefault);
});
});
describe('dotProp', () => {
it('parses the string and returns the correct value', () => {
const obj = {
first: {
second: {
third: {
fourth: 'it works'
},
},
lorem: {
ipsum: 'dolor sit amet',
}
},
foo: {
blah: null,
}
};
const result = _dotProp('first.second.third.fourth', obj);
expect(result).toEqual('it works');
});
});