This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
pickNotes.spec.js
252 lines (222 loc) · 7.36 KB
/
pickNotes.spec.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import expectErrorResponse from '~testHelpers/expectErrorResponse';
import {
NUMBER_OF_INPUT_NOTES,
} from '~/config/settings';
import generateSortedValues from '../utils/generateSortedValues';
import pickNotes from '../utils/pickNotes';
import getStartIndex from '../utils/pickNotes/getStartIndex';
import pickKeysByValues from '../utils/pickNotes/pickKeysByValues';
import * as validate from '../utils/pickNotes/validate';
import * as pickValues from '../utils/pickNotes/pickValues';
describe('getStartIndex', () => {
it('return a start index that will guarantee at least one valid combination from that point', () => {
const sortedValues = [0, 1, 2, 3, 4, 5, 6];
expect(getStartIndex(sortedValues, 4, 1)).toBe(4);
expect(getStartIndex(sortedValues, 4, 2)).toBe(0);
expect(getStartIndex(sortedValues, 4, 3)).toBe(0);
expect(getStartIndex(sortedValues, 9, 2)).toBe(3);
expect(getStartIndex(sortedValues, 9, 3)).toBe(0);
});
it('properly process repeating values', () => {
const sortedValues = [0, 1, 1, 1, 1, 1, 2, 3, 4, 5];
expect(getStartIndex(sortedValues, 3, 1)).toBe(7);
expect(getStartIndex(sortedValues, 3, 2)).toBe(0);
expect(getStartIndex(sortedValues, 6, 2)).toBe(1);
expect(getStartIndex(sortedValues, 7, 2)).toBe(6);
});
it('return -1 if there is no valid combination in it', () => {
const sortedValues = [0, 1, 2, 3, 4, 5, 6];
expect(getStartIndex(sortedValues, 7, 1)).toBe(-1);
expect(getStartIndex(sortedValues, 12, 2)).toBe(-1);
});
});
describe('pickKeysByValues', () => {
it('pick note keys from noteValues mapping with selected values', () => {
const noteValues = {
0: ['n:0', 'n:1'],
1: ['n:2', 'n:3', 'n:4'],
10: ['n:5'],
};
expect(pickKeysByValues(noteValues, [0])).toEqual([
expect.stringMatching(/^n:[0|1]$/),
]);
expect(pickKeysByValues(noteValues, [1, 10])).toEqual([
expect.stringMatching(/^n:[2|3|4]$/),
'n:5',
]);
expect(pickKeysByValues(noteValues, [1, 1])).toEqual([
expect.stringMatching(/^n:[2|3]$/),
expect.stringMatching(/^n:[3|4]$/),
]);
expect(pickKeysByValues(noteValues, [1, 1, 1])).toEqual([
'n:2',
'n:3',
'n:4',
]);
});
});
describe('pickNotes', () => {
it('pick notes from noteValues mapping whose sum is equal to or larger than minSum', () => {
const noteValues = {
0: ['n:1', 'n:5'],
1: ['n:2', 'n:3'],
4: ['n:6'],
10: ['n:4'],
12: [],
};
const sortedValues = generateSortedValues(noteValues);
expect(pickNotes({
noteValues,
sortedValues,
minSum: 6,
numberOfNotes: 0,
})).toEqual([]);
expect(pickNotes({
noteValues,
sortedValues,
minSum: 6,
numberOfNotes: 1,
})).toEqual([
{
key: 'n:4',
value: 10,
},
]);
expect(pickNotes({
noteValues,
sortedValues,
minSum: 13,
numberOfNotes: 2,
})).toEqual([
{
key: 'n:6',
value: 4,
},
{
key: 'n:4',
value: 10,
},
]);
});
it('pick random number of notes if numberOfNotes is not undefined', () => {
const validateSpy = jest.spyOn(validate, 'default');
const noteValues = {
0: ['n:1'],
1: ['n:2'],
2: ['n:3'],
4: ['n:6'],
8: ['n:4'],
};
const sortedValues = generateSortedValues(noteValues);
const notes1 = pickNotes({
noteValues,
sortedValues,
minSum: 6,
});
expect(NUMBER_OF_INPUT_NOTES >= 2).toBe(true);
expect(notes1.length).toBe(NUMBER_OF_INPUT_NOTES);
expect(validateSpy).toHaveBeenCalledTimes(1);
const notes2 = pickNotes({
noteValues,
sortedValues,
minSum: 13,
});
expect(notes2.length >= 3).toBe(true);
expect(validateSpy).toHaveBeenCalledTimes(3);
const notes3 = pickNotes({
noteValues,
sortedValues,
minSum: 15,
});
expect(notes3.length >= 4).toBe(true);
});
it('throw error if there is no note combinations >= min sum', () => {
expectErrorResponse(() => pickNotes({
noteValues: {
0: ['n:0'],
10: ['n:1'],
100: ['n:2'],
},
sortedValues: [0, 10, 100],
minSum: 1000,
numberOfNotes: 1,
})).toBe('note.pick.minSum');
});
it('return all notes by default if total number of notes is less than numberOfNotes', () => {
expect(pickNotes({
noteValues: {
2: ['n:1'],
5: ['n:0'],
},
sortedValues: [2, 5],
minSum: 6,
numberOfNotes: 3,
})).toEqual([
{
key: 'n:1',
value: 2,
},
{
key: 'n:0',
value: 5,
},
].sort());
});
it('throw error if total number of notes is less than required numberOfNotes and allowLessNumberOfNotes is false', () => {
expectErrorResponse(() => pickNotes({
noteValues: {
2: ['n:1'],
5: ['n:0'],
},
sortedValues: [2, 5],
minSum: 1,
numberOfNotes: 3,
allowLessNumberOfNotes: false,
})).toBe('note.pick.count');
});
it('skip repeating min values if current sum is not enough', () => {
const pickValuesSpy = jest.spyOn(pickValues, 'default')
.mockImplementationOnce(() => [
1,
2,
]);
const noteValues = {
1: ['n:0', 'n:1', 'n:2', 'n:3', 'n:4'],
2: ['n:5', 'n:6'],
3: ['n:7'],
};
const sortedValues = generateSortedValues(noteValues);
pickNotes({
noteValues,
sortedValues,
minSum: 4,
numberOfNotes: 2,
});
expect(pickValuesSpy).toHaveBeenCalledTimes(2);
expect(pickValuesSpy.mock.calls[1][2]).toEqual(5); // start index
pickValuesSpy.mockRestore();
});
it('select from values that has up to prevNumberOfSelectedMinValues - 1 min values from previous combinations', () => {
const pickValuesSpy = jest.spyOn(pickValues, 'default')
.mockImplementationOnce(() => [
1,
1,
1,
]);
const noteValues = {
1: ['n:0', 'n:1', 'n:2', 'n:3', 'n:4'],
2: ['n:5', 'n:6'],
3: ['n:7'],
};
const sortedValues = generateSortedValues(noteValues);
pickNotes({
noteValues,
sortedValues,
minSum: 4,
numberOfNotes: 3,
});
expect(pickValuesSpy).toHaveBeenCalledTimes(2);
expect(pickValuesSpy.mock.calls[1][2]).toEqual(3);
pickValuesSpy.mockRestore();
});
});