Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Commit 400c41c

Browse files
committed
Added comprehensive tests for new and changed functions
1 parent fa49dc9 commit 400c41c

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

src/utils/general.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ const wrapKeyframes = content => `@keyframes {${content}}\n`
6060
exports.wrapKeyframes = wrapKeyframes
6161
exports.wrapSelector = wrapSelector
6262
exports.fixIndentation = fixIndentation
63+
exports.reverseString = reverseString
6364
exports.nextNonWhitespaceChar = nextNonWhitespaceChar
6465
exports.isLastDeclarationCompleted = isLastDeclarationCompleted

test/utils.test.js

+132
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const interleave = require('../src/utils/tagged-template-literal').interleave
2+
const isLastDeclarationCompleted = require('../src/utils/general').isLastDeclarationCompleted
3+
const nextNonWhitespaceChar = require('../src/utils/general').nextNonWhitespaceChar
4+
const reverseString = require('../src/utils/general').reverseString
25

36
describe('utils', () => {
47
describe('interleave', () => {
@@ -157,6 +160,135 @@ describe('utils', () => {
157160
expect(interleave(quasis2, expressions2)).toEqual(
158161
'\n display: $dummyValue; -styled-mixin0: dummyValue;\n'
159162
)
163+
164+
/**
165+
* It is important to also have this one as interleave would fail this if it simply
166+
* checked the previous quasi and not the previous processed text.
167+
* Here we also check the whole expression with and without a semi-colon in the quasi
168+
*/
169+
const quasis3 = [
170+
{
171+
value: {
172+
raw: '\n display: '
173+
}
174+
},
175+
{
176+
value: {
177+
raw: '; '
178+
}
179+
},
180+
{
181+
value: {
182+
raw: ' '
183+
}
184+
},
185+
{
186+
value: {
187+
raw: '\n'
188+
}
189+
}
190+
]
191+
const expressions3 = [
192+
{
193+
name: 'display'
194+
},
195+
{
196+
name: undefined
197+
},
198+
{
199+
name: undefined
200+
}
201+
]
202+
expect(interleave(quasis3, expressions3)).toEqual(
203+
'\n display: $dummyValue; -styled-mixin0: dummyValue; -styled-mixin1: dummyValue;\n'
204+
)
205+
})
206+
})
207+
208+
describe('reverseString', () => {
209+
const fn = reverseString
210+
211+
it('reverses a string', () => {
212+
expect(fn('abcd')).toEqual('dcba')
213+
})
214+
215+
it('handles empty string', () => {
216+
expect(fn('')).toEqual('')
217+
})
218+
})
219+
220+
describe('nextNonWhitespaceChar', () => {
221+
const fn = nextNonWhitespaceChar
222+
223+
it('handles empty string', () => {
224+
expect(fn('')).toBe(null)
225+
})
226+
227+
it('handles all whitespace', () => {
228+
expect(fn(' \t \n \t')).toBe(null)
229+
})
230+
231+
it('handles no leading whitespace', () => {
232+
expect(fn('abc')).toBe('a')
233+
})
234+
235+
it('handles spaces', () => {
236+
expect(fn(' b')).toBe('b')
237+
})
238+
239+
it('handles tabs', () => {
240+
expect(fn('\tc')).toBe('c')
241+
})
242+
243+
it('handles newlines', () => {
244+
expect(fn('\nd')).toBe('d')
245+
})
246+
247+
it('handles a mix', () => {
248+
expect(fn(' \n\t\ra \t\r\nb')).toBe('a')
249+
})
250+
})
251+
252+
describe('isLastDeclarationCompleted', () => {
253+
const fn = isLastDeclarationCompleted
254+
255+
it('handles all whitespace', () => {
256+
expect(fn(' \n \n \t \r')).toBe(true)
257+
})
258+
259+
it('handles empty string', () => {
260+
expect(fn('')).toBe(true)
261+
})
262+
263+
it('handles one-line css', () => {
264+
const prevCSS = 'display: block; color: red '
265+
expect(fn(prevCSS)).toBe(false)
266+
267+
expect(fn(`${prevCSS};`)).toBe(true)
268+
})
269+
270+
it('handles multi-line css', () => {
271+
const prevCSS = `
272+
display: block;
273+
color: red`
274+
expect(fn(prevCSS)).toBe(false)
275+
276+
expect(fn(`${prevCSS};\n`)).toBe(true)
277+
})
278+
279+
it('handles irregular css', () => {
280+
const prevCSS = `display : block
281+
; color:
282+
red `
283+
expect(fn(prevCSS)).toBe(false)
284+
285+
expect(
286+
fn(`${prevCSS}
287+
288+
;
289+
290+
`)
291+
).toBe(true)
160292
})
161293
})
162294
})

0 commit comments

Comments
 (0)