forked from thinkb4/a-walk-in-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branching.test.js
215 lines (156 loc) · 4.6 KB
/
branching.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
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
describe('DAY 5: Test Branching - if...else', () => {
it('b to be true if a is truthy', () => {
let a;
let b;
if (a) {
//
} else {
//
}
expect(b).toBe(true);
});
it('b to be false if a is truthy', () => {
let a;
let b;
if (a) {
//
} else {
//
}
expect(b).toBe(false);
});
it('b to be 1 if a is truthy ( use postfix unary operator )', () => {
let a;
let b = 0;
if (a) {
//
} else {
//
}
// are prefix and postfix interchangeable here?
expect(b).toBe(1);
});
it('b to be -1 if a is truthy ( use prefix unary operator )', () => {
let a;
let b = 0;
if (a) {
//
} else {
//
}
// are prefix and postfix interchangeable here?
expect(b).toBe(-1);
});
});
describe('DAY 5: Test Branching - switch/break', () => {
it('found to be true when number 7 is the first item of array', () => {
let array = [];
let found = false;
switch (array.indexOf(7)) {
case -1:
break;
case 0:
break;
case 1:
break;
default:
break;
}
expect(found).toBe(true);
});
it('found to be true when number 7 is not an item of array', () => {
let array = [];
let found = false;
switch (array.indexOf(7)) {
case -1:
break;
case 0:
break;
case 1:
break;
default:
break;
}
expect(found).toBe(true);
});
it('found to be true when number 7 is at index 4 of array', () => {
let array = [];
let found = false;
switch (array.indexOf(7)) {
case -1:
break;
case 0:
break;
case 1:
break;
default:
found = true;
break;
}
expect(found).toBe(true);
});
it('found to be true when number 7 is at index 2 or 3 or 4 of array ( wanna try Fallthrough? )', () => {
let array = [];
let found = false;
switch (array.indexOf(7)) {
case -1:
break;
case 0:
break;
case 1:
break;
default:
found = true;
break;
}
expect(found).toBe(true);
});
});
describe('DAY 5: Test Branching - short circuit', () => {
it('c to be "hell yeah" using logical AND to evaluate a AND b, AND the value assigned to c', () => {
let a;
let b;
let c = a && b && 'hell yeah';
expect(c).toBe('hell yeah');
});
it('c to be "hell yeah" using logical OR to evaluate a OR b, AND the value assigned to c ( find the error and fix it )', () => {
let a = 1;
let b;
let c = a || b && 'hell yeah';
expect(c).toBe('hell yeah');
});
it('c to be true IF a AND b are numbers and > than 42', () => {
// read the test spec carefully before start coding ;)
let a;
let b;
let c = false;
expect(c).toBe(true);
});
});
describe('DAY 5: Test Branching - conditional operator ( ternary )', () => {
it('c to be true IF a AND b are numbers and > than 42, else it should be false', () => {
let a = null;
let b = null;
let c = null; // use ternary operator during assignment
expect(c).toBe(true);
a = null;
b = null;
c = null; // use ternary operator during assignment
expect(c.toString()).toBe('Error: Not valid values for a and b');
});
it(`if speed is faster than 140, traffic ticket should be 8000,
else if, it's faster than 130, traffic ticket should be 3000
else, traffic ticket should be 0`, () => {
let speed;
let trafficTicket; // use ternary operator during assignment
expect(trafficTicket).toBe(8000);
// change the values so the test may pass ( use ternary operator for trafficTicket )
expect(trafficTicket).toBe(3000);
// change the values so the test may pass ( use ternary operator for trafficTicket )
expect(trafficTicket).toBe(0);
/**
* it seems we had to do some repetitive work here
* any clue about how to avoid it?
*/
});
});