-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.test.js
73 lines (57 loc) · 1.61 KB
/
utils.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
/* eslint-env jest */
import { filterNotes, nextNoteId } from './utils'
const notes = [
{
id: 1,
text: 'First note',
tags: ['foo']
}, {
id: 3,
text: 'Second note',
tags: ['foo', 'bar']
}
]
describe('filterNotes', () => {
test('filter single element', () => {
expect(filterNotes(notes, 'Fir')).toEqual([ notes[0] ])
expect(filterNotes(notes, 'bar')).toEqual([ notes[1] ])
})
test('filter multiple elements', () => {
expect(filterNotes(notes, 'note')).toEqual(notes)
expect(filterNotes(notes, 'oo')).toEqual(notes)
})
test('filtered array !== input array if filter parameter is truthy', () => {
expect(filterNotes(notes, 'note')).not.toBe(notes)
})
test('filtered array === input array if filter parameter is falsy', () => {
for (const val of [false, '', null, undefined]) {
expect(filterNotes(notes, val)).toBe(notes)
}
})
test('fail if elements are missing text property', () => {
expect(() => {
filterNotes([{}], 'note')
}).toThrow()
})
test('pass if elements have only text property', () => {
expect(() => {
filterNotes([{ text: 'Test note' }], 'note')
}).not.toThrow()
})
})
describe('nextNoteId', () => {
test('passing empty array', () => {
expect(nextNoteId([])).toBe(1)
})
test('passing non-empty array', () => {
expect(nextNoteId(notes)).toBe(4)
})
test('fail if notes parameter is not an array', () => {
expect(() => {
nextNoteId({})
}).toThrow()
})
test('pass if elements are missing id property', () => {
expect(nextNoteId([{ text: 'Test note' }])).toBe(1)
})
})