-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathutils.test.ts
More file actions
64 lines (57 loc) · 1.7 KB
/
utils.test.ts
File metadata and controls
64 lines (57 loc) · 1.7 KB
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
import {getItemIndexByCharacterKey} from '../utils'
import reducer from '../reducer'
describe('getItemIndexByCharacterKey', () => {
const items = ['a', 'b', 'aba', 'aab', 'bab']
const itemToString = jest.fn().mockImplementation(item => item)
const isItemDisabled = jest.fn().mockReturnValue(false)
test('returns to check from start if from highlightedIndex does not find anything', () => {
const index = getItemIndexByCharacterKey({
keysSoFar: 'a',
highlightedIndex: 3,
items,
itemToString,
isItemDisabled,
})
expect(index).toBe(0)
})
test('checks from highlightedIndex position inclusively if there is more than one key', () => {
const index = getItemIndexByCharacterKey({
keysSoFar: 'aba',
highlightedIndex: 2,
items,
itemToString,
isItemDisabled,
})
expect(index).toBe(2)
})
test('checks from highlightedIndex position exclusively if there is only one key', () => {
const index = getItemIndexByCharacterKey({
keysSoFar: 'a',
highlightedIndex: 2,
items,
itemToString,
isItemDisabled,
})
expect(index).toBe(3)
})
test('skips disabled item and moves to next', () => {
const keysSoFar = 'b'
const highlightedIndex = 0
expect(
getItemIndexByCharacterKey({
keysSoFar,
highlightedIndex,
items,
itemToString,
isItemDisabled: jest
.fn()
.mockImplementation((_item, index) => index === 1),
}),
).toEqual(4)
})
})
test('reducer throws error if called without proper action type', () => {
expect(() => {
reducer({}, {type: 'super-bogus'})
}).toThrow('Reducer called without proper action type.')
})