UNPKG

7.72 kBJavaScriptView Raw
1const o = require('ospec')
2
3const styleEl = function() {
4 const el = {
5 textContent: '',
6 sheet: {
7 cssRules: [],
8 insertRule: rule => {
9 el.sheet.cssRules.push(rule)
10 el.sheet.textContent += rule
11 }
12 },
13 style: {
14 setProperty: (prop) => {
15 if (prop === 'backgroundColor')
16 throw new Error()
17 }
18 }
19 }
20
21 return el
22}
23
24global.document = {
25 createElement: () => styleEl(),
26 head: {
27 appendChild: () => null
28 },
29 documentElement: {
30 style: {
31 color: '',
32 animation: '',
33 display: '',
34 backgroundColor: '',
35 content: '',
36 width: '0',
37 padding: '',
38 boxShadow: ''
39 }
40 }
41}
42
43global.window = {
44 navigator: {
45 userAgent: 'test'
46 }
47}
48
49const b = require('../bss')
50
51o.spec('bss', function() {
52 o('inputs', function() {
53 o(b`foo: bar; baz: boo;`.style).deepEquals({ foo: 'bar', baz: 'boo' })
54 o(b`foo: bar;`.style).deepEquals({ foo: 'bar' })
55 o(b`foo: bar`.style).deepEquals({ foo: 'bar' })
56 o(b`foo bar`.style).deepEquals({ foo: 'bar' })
57 o(b({ foo: 'bar' }).style).deepEquals({ foo: 'bar' })
58 o(b('foo', 'bar').style).deepEquals({ foo: 'bar' })
59 })
60
61 o('multiline input', function() {
62 o(b('transform scale(1)\n,rotate(0)').style).deepEquals({ transform: 'scale(1),rotate(0)' })
63 o(b('transform scale(1),\nrotate(0)').style).deepEquals({ transform: 'scale(1),rotate(0)' })
64 })
65
66 o('default css properties', function() {
67 o(b.bc('green').style).deepEquals({ backgroundColor: 'green' })
68 o(b.p(20, 10, '50%').style).deepEquals({ padding: '20px 10px 50%' })
69 o(b`p 20 10 50%`.style).deepEquals({ padding: '20px 10px 50%' })
70 o(b.backgroundColor('red').style).deepEquals({ backgroundColor: 'red' })
71 })
72
73 o('css doulbe class for specificity generation', function() {
74 const cls = b`foo: bar;`.class
75 o(b.getSheet()).equals(`.${cls}.${cls}{foo:bar;}`)
76 })
77
78 o('common style class reuse', function() {
79 const cls = b`foo: bar;`.class
80 , cls2 = b`foo: bar;`.class
81
82 o(cls).equals(cls2)
83 o(b.getSheet()).equals(`.${cls}.${cls}{foo:bar;}`)
84 })
85
86 o('values can have colons', function() {
87 const cls = b`
88 backgroundImage: url(https://bss.com/)
89 `.class
90 o(b.getSheet()).equals(`.${cls}.${cls}{background-image:url(https://bss.com/);}`)
91 })
92
93 o('pseudo', function() {
94 const cls = b.$hover(b.bc('green')).class
95 o(b.getSheet()).equals(`.${cls}.${cls}:hover{background-color:green;}`)
96 })
97
98 o('same named props', function() {
99 const cls1 = b`
100 c blue
101 bc white
102 `.class
103 const cls2 = b`
104 c blue
105 bc white
106 c white
107 `.class
108 o(b.getSheet()).equals([
109 `.${cls1}.${cls1}{color:blue;background-color:white;}`,
110 `.${cls2}.${cls2}{color:blue;background-color:white;color:white;}`
111 ].join(''))
112 })
113
114 o('same named properties string', function() {
115 const cls = b`
116 display -webkit-flex
117 display flex
118 `.class
119 o(b.getSheet()).equals(`.${cls}.${cls}{display:-webkit-flex;display:flex;}`)
120 })
121
122 o('same named properties function', function() {
123 const cls = b.d('-webkit-flex').d('flex').class
124 o(b.getSheet()).equals(`.${cls}.${cls}{display:-webkit-flex;display:flex;}`)
125 })
126
127 o('same named properties style', function() {
128 o(b.d('-webkit-flex').d('flex').style).deepEquals({display:'flex'})
129 })
130
131 o('empty content string is set to ""', function() {
132 const cls = b.$before(b.content('')).$after(b({ content: '' })).class
133 o(b.getSheet()).equals(`.${cls}.${cls}::before{content:"";}.${cls}.${cls}::after{content:"";}`)
134 })
135
136 o('allows vendor prefix', function() {
137 const cls = b('-webkit-overflow-scrolling touch').class
138 o(b.getSheet()).equals(`.${cls}.${cls}{-webkit-overflow-scrolling:touch;}`)
139 })
140
141 o('allows css variables', function() {
142 const cls = b('--primaryColor 250 250 250').class
143 o(b.getSheet()).equals(`.${cls}.${cls}{--primaryColor:250 250 250;}`)
144 })
145
146 o('single class for less specificity when using $nest', function() {
147 const cls = b.$nest('li', b('-webkit-overflow-scrolling touch')).class
148 o(b.getSheet()).equals(`.${cls} li{-webkit-overflow-scrolling:touch;}`)
149 })
150
151 o('nest multiple selectors', function() {
152 const cls = b.$nest('th, tr', b('background blue')).class
153 o(b.getSheet()).equals(`.${cls} th,.${cls} tr{background:blue;}`)
154 })
155
156 o('nest objects', function() {
157 const cls = b.$nest({ th : b('background blue') }).class
158 o(b.getSheet()).equals(`.${cls} th{background:blue;}`)
159 })
160
161 o('nest with ampersand', function() {
162 const cls = b.$nest({ 'th &' : b('background blue') }).class
163 o(b.getSheet()).equals(`.${cls} th .${cls}{background:blue;}`)
164 })
165
166 o('add px', function() {
167 o(b`w 1`.style).deepEquals({ width: '1px' })
168 o(b('width 1').style).deepEquals({ width: '1px' })
169 o(b({ width: 1 }).style).deepEquals({ width: '1px' })
170 o(b`boxShadow 1 1 10 black`.style).deepEquals({ boxShadow: '1px 1px 10px black' })
171 o(b`border 1 solid black`.style).deepEquals({ border: '1px solid black' })
172 o(b.w(1).style).deepEquals({ width: '1px' })
173 })
174
175 o('clears empty', function() {
176 o(b.width(false && 20).style).deepEquals({})
177 o(b.width(undefined && 20).style).deepEquals({})
178 o(b.width(null && 20).style).deepEquals({})
179 o(b.width('').style).deepEquals({})
180 })
181
182 o.spec('helpers', function() {
183
184 o('without args', function() {
185 b.helper('foobar', b`foo bar`)
186 o(b.foobar.style).deepEquals({ foo: 'bar' })
187 })
188
189 o('parsed', function() {
190 b.helper('foobar', `foo bar`)
191 o(b.foobar.style).deepEquals({ foo: 'bar' })
192 })
193
194 o('with args (object notation)', function() {
195 b.helper('foo', arg => b({ foo: arg }))
196 o(b.foo('bar').style).deepEquals({ foo: 'bar' })
197 })
198
199 o('with args (bss notation)', function() {
200 b.helper('foo', arg => b`foo ${arg}`)
201 o(b.foo('bar').style).deepEquals({ foo: 'bar' })
202 })
203
204 o('with and without args mixed', function() {
205 b.helper('foo', arg => b`foo ${arg}`)
206 b.helper('baz', b`baz foz`)
207 o(b.foo('bar').baz.style).deepEquals({ foo: 'bar', baz: 'foz' })
208 })
209
210 o('multiple helpers in object', function() {
211 b.helper({
212 foo: b`bar baz`,
213 bar: b`foo bar`
214 })
215 o(b.foo.bar.style).deepEquals({ bar: 'baz', foo: 'bar' })
216 })
217
218 o('helpers in strings', function() {
219 b.helper({
220 size: (w, h) => b(`width ${w};height ${h}`),
221 pointer: b('cursor pointer')
222 })
223 o(b`
224 size 20 20
225 pointer
226 `.style).deepEquals({ width: '20px', height: '20px', cursor: 'pointer' })
227 })
228 })
229
230 o('css', function() {
231 b.css('html', 'background blue')
232 o(b.getSheet()).equals('html{background:blue;}')
233 })
234
235 o('css objects', function() {
236 b.css({ html: 'background blue' })
237 o(b.getSheet()).equals('html{background:blue;}')
238 })
239
240 o('css nest', function() {
241 b.css('html', b('background blue').$nest('li', 'background red'))
242 o(b.getSheet()).equals('html{background:blue;}html li{background:red;}')
243 })
244
245 o('$keyframes', function() {
246 const anim = b.$keyframes({
247 from: 'bc red'
248 })
249 o(b.getSheet()).equals(`@keyframes ${anim}{from{background-color:red;}}`)
250 })
251
252 o('$animate', function() {
253 const cls = b.$animate('1s', {
254 from: 'bc black'
255 }).class
256 const sheet = b.getSheet()
257 o(sheet).equals(`@keyframes ${cls}{from{background-color:black;}}.${cls}.${cls}{animation:${cls} 1s;}`)
258 })
259
260 o('Override valueOf', function() {
261 const newValueOf = function() {
262 return 'test'
263 }
264 b.valueOf = newValueOf
265 o(b.valueOf).equals(newValueOf)
266 o('' + b.bc('red')).equals('test')
267 })
268})