-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
186 lines (149 loc) · 4.25 KB
/
index.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
var sinon;
var expect;
if ((typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')) {
// here, export any references you need for tests //
sinon = require('sinon');
expect = require('chai').expect;
}
describe("Test Guru", function () {
/* global expect, sinon*/
beforeEach(function () {
sinon.spy(console, 'log');
});
afterEach(function () {
console.log.restore();
});
it('Our First Test', function () {
var value = "hello tests";
var some_number = 484;
// ┌ Change this to what it should be
expect(value === '???').to.be.true;
// ┌ Change this to what it should be
expect(some_number === '???').to.be.true;
});
it("Functions can access/modify variables in parent scope.", function(){
var outside_the_function = null;
function yay(){
var inside_the_function = "can you see me?";
outside_the_function = inside_the_function;
}
yay();
expect(outside_the_function === '???').to.be.true;
});
it("Function Parameters become scoped to the function.", function(){
function yay(param){
expect(param === '???').to.be.true;
}
yay("a fine kettle of fish");
});
it("A functions local scope is not available in an outer scope.", function(){
function yay(){
var kix = "kid tested mother approved";
expect(kix === '???').to.be.true;
}
yay();
var has_kix;
// NOTE:
// "this" is a special object that by default represents the global scope.
// variables declared globally are stored as a property on the object: this.<variable>
// if the variable is not global then this.<variable> will be undefined
if(this.kix !== undefined){
has_kix = kix;
} else {
has_kix = "i prefer cheerios";
}
expect(has_kix === '???').to.be.true;
});
it("Functions don't have access to each other's scope", function(){
function yay(){
var from_yay = "i'm inside yay;";
}
function foo(){
var in_foo = "i'm in foo";
if(this.from_yay !== undefined){
in_foo = this.from_yay;
}
expect(in_foo === '???').to.be.true;
expect(this.from_yay === '???').to.be.true;
}
yay();
foo();
});
it("Inner scope variables have preference over outter scope variables with the same name.", function(){
var peanuts = 300;
function yay(){
var peanuts = "roasted";
expect(peanuts === '???').to.be.true;
}
yay();
expect(peanuts === '???').to.be.true;
});
it("Variables created with var in a function are re-created each time", function(){
function yay(){
if(this.counter !== undefined){
counter = counter + 1;
} else {
var counter = 10;
}
}
yay();
expect(this.counter === '???').to.be.true;
yay();
expect(this.counter === '???').to.be.true;
yay();
expect(this.counter === '???').to.be.true;
});
it("Inner scope can access outer scope", function(){
var im_outside = "alpha";
function yay(){
var im_inside = "omega";
return im_outside + im_inside;
}
expect(yay() === '???').to.be.true;
});
it("Functions retain outer scope references between calls.", function(){
var im_outside = 13;
function yay(){
im_outside += 1;
}
yay();
expect(im_outside === '???').to.be.true;
yay();
expect(im_outside === '???').to.be.true;
});
it("We can do goofy stuff with outer scope", function(){
var hello = "greg";
var name = "";
function yay(){
name += hello;
}
yay();
expect(name === '???').to.be.true;
yay();
expect(name === '???').to.be.true;
yay();
expect(name === '???').to.be.true;
});
it("We can pass functions to other functions and then run them.", function(){
var im_outter = 10;
function yay(){
im_outter /= 5;
}
function something(whatever){
im_outter *= 20;
whatever();
}
something(yay);
expect(im_outter === '???').to.be.true;
});
it("We can get crazy with returns.", function(){
function yay(){
return " is dog";
}
function foo(whatever){
return "hello, this" + whatever();
}
expect(foo(yay) === '???').to.be.true;
});
});