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
184
185
186
187
188
189
190
191
192
193
|
window.USERS = [
{ type: 's', value: '18561' },
{ type: 's', value: '18562' },
{ type: 's', value: '18563' },
{ type: 's', value: '18564' },
{ type: 's', value: '18565' },
{ type: 's', value: '18566' },
{ type: 's', value: '18567' },
{ type: 's', value: '18568' },
{ type: 's', value: '18569' },
];
const deepFreeze = require('deep-freeze');
const search = require('./search').default;
const { inputChange, changeSelectedResult } = require('../actions/search');
describe('reducers', () => {
describe('search', () => {
describe('SEARCH/INPUT_CHANGE', () => {
it('Returns no results when nothing is typed in', () => {
expect(search(undefined, inputChange(''))).toEqual({
results: [],
selectedResult: null,
isExactMatch: false,
});
});
it('Returns no results when a space is typed in', () => {
expect(search(undefined, inputChange(' '))).toEqual({
results: [],
selectedResult: null,
isExactMatch: false,
});
});
it('Preforms a basic search, only returning four results', () => {
expect(search(undefined, inputChange('18'))).toEqual({
results: [
's/18561',
's/18562',
's/18563',
's/18564',
],
selectedResult: null,
isExactMatch: false,
});
});
it('Selects the first result and sets isExactMatch to true when there is an exact match', () => {
expect(search(undefined, inputChange('18561'))).toEqual({
results: [
's/18561',
],
selectedResult: 's/18561',
isExactMatch: true,
});
});
});
describe('SEARCH/CHANGE_SELECTED_RESULT', () => {
it('Does nothing when there are no results', () => {
const prevState = {
results: [],
selectedResult: null,
isExactMatch: false,
};
const actionPlus = changeSelectedResult(+1);
const actionMin = changeSelectedResult(-1);
deepFreeze([prevState, actionPlus, actionMin]);
const nextStatePlus = search(prevState, actionPlus);
const nextStateMin = search(prevState, actionMin);
expect(nextStatePlus).toEqual(prevState);
expect(nextStateMin).toEqual(prevState);
});
it('Does nothing when there is an exact match', () => {
const prevState = {
results: ['s/18561'],
selectedResult: 's/18561',
isExactMatch: true,
};
const actionPlus = changeSelectedResult(+1);
const actionMin = changeSelectedResult(-1);
deepFreeze([prevState, actionPlus, actionMin]);
const nextStatePlus = search(prevState, actionPlus);
const nextStateMin = search(prevState, actionMin);
expect(nextStatePlus).toEqual(prevState);
expect(nextStateMin).toEqual(prevState);
});
it('Switches to the correct selectedResult when no selected result is selected', () => {
const prevState = {
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: null,
isExactMatch: false,
};
const actionPlus = changeSelectedResult(+1);
const actionMin = changeSelectedResult(-1);
deepFreeze([prevState, actionPlus, actionMin]);
const nextStatePlus = search(prevState, actionPlus);
const nextStateMin = search(prevState, actionMin);
expect(nextStatePlus).toEqual({
...prevState,
selectedResult: 's/18561',
});
expect(nextStateMin).toEqual({
...prevState,
selectedResult: 's/18563',
});
});
it('Switches to the correct selectedResult when there is a selected result selected', () => {
const prevState = {
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: 's/18562',
isExactMatch: false,
};
const actionPlus = changeSelectedResult(+1);
const actionMin = changeSelectedResult(-1);
deepFreeze([prevState, actionPlus, actionMin]);
const nextStatePlus = search(prevState, actionPlus);
const nextStateMin = search(prevState, actionMin);
expect(nextStatePlus).toEqual({
...prevState,
selectedResult: 's/18563',
});
expect(nextStateMin).toEqual({
...prevState,
selectedResult: 's/18561',
});
});
it('Properly wraps arround when incrementing', () => {
expect(search({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: 's/18563',
isExactMatch: false,
}, changeSelectedResult(+1))).toEqual({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: null,
isExactMatch: false,
});
expect(search({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: null,
isExactMatch: false,
}, changeSelectedResult(+1))).toEqual({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: 's/18561',
isExactMatch: false,
});
});
it('Properly wraps arround when decrementing', () => {
expect(search({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: 's/18561',
isExactMatch: false,
}, changeSelectedResult(-1))).toEqual({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: null,
isExactMatch: false,
});
expect(search({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: null,
isExactMatch: false,
}, changeSelectedResult(-1))).toEqual({
results: ['s/18561', 's/18562', 's/18563'],
selectedResult: 's/18563',
isExactMatch: false,
});
});
});
});
});
|