-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
152 lines (122 loc) · 4.38 KB
/
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
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
var SovereignState = require('./sovereignty');
var failed = 0;
function test(description, fn){
if(!fn()){
failed++;
console.error(description + ' x ');
return;
};
console.log(description + ' ✓ ')
}
function throwsError(fn){
var result = false;
try {
fn();
} catch (e) {
result = true;
}
return result;
}
const initialState = {
foo: 0,
bar: '',
baz: [],
quux: {},
quuz: null,
corge: undefined
}
test('SovereignState in scope', function(){
return SovereignState !== undefined;
});
console.log('SovereignStateUtils.constructor')
test(' - initialState', function(){
var stateUtils = new SovereignState(initialState);
return stateUtils.initialState === initialState;
});
test(' - typeLookup', function () {
var stateUtils = new SovereignState(initialState);
var number = stateUtils.typeLookup['foo'] === initialState['foo'].constructor.name;
var string = stateUtils.typeLookup['bar'] === initialState['bar'].constructor.name;
var array = stateUtils.typeLookup['baz'] === initialState['baz'].constructor.name;
var object = stateUtils.typeLookup['quux'] === initialState['quux'].constructor.name;
var nullAny = stateUtils.typeLookup['quuz'] === 'any';
var undefinedAny = stateUtils.typeLookup['corge'] === 'any';
return number &&
string &&
array &&
object &&
nullAny &&
undefinedAny;
});
// prototype
console.log('SovereignStateUtils.prototype');
console.log(' - validateKeys()')
test(' - it returns true for keys present in initialState', function() {
var stateUtils = new SovereignState(initialState);
return stateUtils.validateKeys({ foo: 1 });
});
test(' - it throws for keys not-present in initialState', function () {
var stateUtils = new SovereignState(initialState);
return throwsError(function () { return stateUtils.validateKeys({ banana: 1 }) });
});
test(' - it throws with mixed present and not', function () {
var stateUtils = new SovereignState(initialState);
return throwsError(function () { return stateUtils.validateKeys({ banana: 1, foo: 1 }) });
});
console.log(' - validateValueTypes()')
test(' - it returns true for values that match the value types of initialState', function () {
var stateUtils = new SovereignState(initialState);
return stateUtils.validateValueTypes({ foo: 1 });
});
test(' - it returns true for keys with value \'any\' (those which had null/undefined values in initialState)', function () {
var stateUtils = new SovereignState(initialState);
return stateUtils.validateValueTypes({ quuz: SovereignState });
});
test(' - it throws for value types that do not match', function () {
var stateUtils = new SovereignState(initialState);
return throwsError(function () {
return stateUtils.validateValueTypes({ foo: 'foo' });
});
});
test(' - it throws for value types that both do and do not match', function () {
var stateUtils = new SovereignState(initialState);
return throwsError(function () {
return stateUtils.validateValueTypes({ foo: 1, bar: SovereignState });
});
});
console.log(' - update()');
var newState = {
foo: 4,
bar: 'dude'
};
test(' - it returns an updated store with a valid object', function(){
var stateUtils = new SovereignState(initialState);
var updatedState = stateUtils.update(initialState, newState);
return updatedState['foo'] === 4 && updatedState['bar'] === 'dude';
});
test(' - it does not mutate its arguments', function () {
var stateUtils = new SovereignState(initialState);
var updatedState = stateUtils.update(initialState, newState);
return initialState['foo'] !== updatedState['foo'];
});
test(' - it is deterministic', function () {
var stateUtils = new SovereignState(initialState);
var firstUpdate = stateUtils.update(initialState, newState);
var secondUpdate = stateUtils.update(firstUpdate, initialState);
return initialState['foo'] === secondUpdate['foo'];
});
test(' - falsey values for type \'any\' (or 0) do not throw', function(){
var stateUtils = new SovereignState(initialState);
return !throwsError(function () {
return stateUtils.validateValueTypes({ foo: 0, bar: '', quuz: undefined });
});
});
test(' - falsey values for all other situations do throw', function () {
var stateUtils = new SovereignState(initialState);
return throwsError(function () {
return stateUtils.validateValueTypes({ foo: undefined });
});
});
if(failed !== 0) {
process.exit(1);
}