-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
117 lines (102 loc) · 3.26 KB
/
tests.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
import {alphabetizeByProperty} from './index.js';
import {getArrayFromProperty} from '@writetome51/get-array-from-property';
import {arraysMatch} from '@writetome51/arrays-match';
// Test 0: make sure it can alphabetize by array index:
let arrays = [
['teacher', ['thomas', 'stoppard']],
['pastor', ['terry', 'blank']],
['priest', ['sam', 'martin']],
['mayor', ['amy', 'thomas']],
['teacher', ['cathy', 'nguyen']],
['minister', ['nguc', 'leung']]
];
alphabetizeByProperty('1.1', arrays);
if (arraysMatch(arrays, [
['pastor', ['terry', 'blank']],
['minister', ['nguc', 'leung']],
['priest', ['sam', 'martin']],
['teacher', ['cathy', 'nguyen']],
['teacher', ['thomas', 'stoppard']],
['mayor', ['amy', 'thomas']]
]))
console.log('test 0 passed');
else
console.log('test 0 FAILED');
let roster = [
{name: 'Rod Carmichael', group: 'D'},
{name: 'Todd Garfunkel', group: 'B'},
{name: 'Rachel Green', group: 'E'},
{name: 'Mick Jagger', group: 'A'},
{name: 'Charlie Brown', group: 'D'},
{name: 'Flip Mavunkel', group: 'C'}
];
alphabetizeByProperty('group', roster);
let result = getArrayFromProperty('group', roster);
if (arraysMatch(result, ['A', 'B', 'C', 'D', 'D', 'E'])) console.log('test 1 passed');
else console.log('test 1 FAILED');
roster = [
{name: 'Rod Carmichael', group: 'Ò'},
{name: 'Todd Garfunkel', group: 'Í'},
{name: 'Rachel Green', group: 'I'},
{name: 'Mick Jagger', group: 'Å'},
{name: 'Charlie Brown', group: 'O'},
{name: 'Flip Mavunkel', group: 'A'},
];
alphabetizeByProperty('group', roster);
result = getArrayFromProperty('group', roster);
if (arraysMatch(result, ['A', 'I', 'O', 'Å', 'Í', 'Ò'])) console.log('test 2 passed');
else console.log('test 2 FAILED');
roster = [
{name: 'Rod Carmichael', group: undefined},
{name: 'Mick Jagger', group: 'Å'},
{name: 'Charlie Brown', group: 'ZZZ'},
{name: 'Farley Brown', group: 'Z'},
{name: 'Rachel Green'},
{name: 'Charlie Brown', group: null},
{name: 'Todd Garfunkel', group: undefined},
{name: 'Flip Mavunkel', group: 'A'},
];
alphabetizeByProperty('group', roster);
result = getArrayFromProperty('group', roster);
if (arraysMatch(result, ['A', 'Z', 'ZZZ', null, undefined, undefined, undefined, 'Å']))
console.log('test 3 passed');
else console.log('test 3 FAILED');
roster = [
{player: {name: 'Rod'}},
{player: {name: 'Charlie'}},
{player: {name: 'Todd'}},
{player: {name: 'Flip'}},
{player: {name: 'Rachel'}},
{player: {name: 'Monica'}},
{player: {name: 'Mick'}}
];
alphabetizeByProperty('player.name', roster);
result = getArrayFromProperty('player.name', roster);
if (arraysMatch(result, ['Charlie', 'Flip', 'Mick', 'Monica', 'Rachel', 'Rod', 'Todd']))
console.log('test 4 passed');
else console.log('test 4 FAILED');
// test errors
let errorTriggered = false;
try {
alphabetizeByProperty('', roster);
} catch (e) {
errorTriggered = true;
}
if (errorTriggered) console.log('test 5 passed');
else console.log('test 5 FAILED');
errorTriggered = false;
try {
alphabetizeByProperty([], roster);
} catch (e) {
errorTriggered = true;
}
if (errorTriggered) console.log('test 6 passed');
else console.log('test 6 FAILED');
errorTriggered = false;
try {
alphabetizeByProperty('prop', {});
} catch (e) {
errorTriggered = true;
}
if (errorTriggered) console.log('test 7 passed');
else console.log('test 7 FAILED');