This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
specs.js
178 lines (143 loc) · 6.12 KB
/
specs.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
describe('specs test', function () {
it('should pass simple test', function () {
expect(true).toBe(true);
});
});
describe('stockRetriever', function () {
beforeEach(function () {
jasmine.addMatchers({
toBeADecimalNumber: function () {
return {
compare: function (actual) {
return {
pass: typeof actual === 'number' && actual.toString().match(/\d+\.+\d+/)
}
}
};
}
});
});
var stockRetriever = window.stockRetriever;
describe('dataProvider', function () {
var dataProvider = stockRetriever.dataProvider;
it('should return single price', function (specDone) {
dataProvider
.getPrices('MSFT')
.done(function (res) {
var item = res[0];
expect(item.symbol).toBe('MSFT');
expect(item.lastTradePrice).toBeADecimalNumber();
specDone();
});
});
it('should return multiple prices', function (specDone) {
dataProvider
.getPrices(['MSFT', 'GOOG'])
.done(function (res) {
var msft = res[0];
expect(msft.symbol).toBe('MSFT');
expect(msft.lastTradePrice).toBeADecimalNumber();
var goog = res[1];
expect(goog.symbol).toBe('GOOG');
expect(goog.lastTradePrice).toBeADecimalNumber();
specDone();
});
});
it('should throw error for missing symbols', function () {
expect(function () { dataProvider.getPrices() }).toThrow();
});
it('should throw error for unexpected symbols value', function () {
expect(function () { dataProvider.getPrices(1) }).toThrow();
});
});
describe('uiProvider', function () {
var uiProvider = stockRetriever.uiProvider;
var selectors = uiProvider.configuration.selectors;
describe('displayLoading', function () {
it('should display loading for current price', function () {
spyOn(selectors.currentPrice, 'html');
uiProvider.displayLoading();
expect(selectors.currentPrice.html).toHaveBeenCalledWith('Loading ...');
});
});
describe('getSymbol', function () {
it('should get symbol from DOM', function () {
spyOn(selectors.symbol, 'val').and.returnValue('GOOG');
var symbol = uiProvider.getSymbol();
expect(symbol).toBe('GOOG');
});
it('should toUpperCase symbol', function () {
spyOn(selectors.symbol, 'val').and.returnValue('goog');
var symbol = uiProvider.getSymbol();
expect(symbol).toBe('GOOG');
});
it('should trim symbol', function () {
spyOn(selectors.symbol, 'val').and.returnValue(' GOOG ');
var symbol = uiProvider.getSymbol();
expect(symbol).toBe('GOOG');
});
});
describe('setPrice', function () {
it('should update current price', function () {
spyOn(selectors.currentPrice, 'html');
uiProvider.setPrice('GOOG', 1000.00, new Date());
expect(selectors.currentPrice.html).toHaveBeenCalled();
});
it('should append price to log', function () {
spyOn(selectors.priceLog, 'append');
uiProvider.setPrice('GOOG', 1000.00, new Date());
expect(selectors.priceLog.append).toHaveBeenCalled();
});
it('should prevent xss attacks', function () {
spyOn(selectors.currentPrice, 'html');
var now = new Date();
uiProvider.setPrice('<script>alert("pwned");</script>', 1000.00, now);
expect(selectors.currentPrice.html).toHaveBeenCalledWith(
'<strong><script>alert("pwned");</script></strong>: $1000 retrieved at ' + now.toString());
});
});
});
describe('app', function () {
var app = stockRetriever.app;
describe('init', function () {
it('should create click handler on fetchButton selector', function () {
var selectors = app.configuration.selectors;
spyOn(selectors.fetchButton, 'on');
app.init();
expect(selectors.fetchButton.on).toHaveBeenCalledWith('click', app.fetch);
});
});
describe('fetch', function () {
var dataProvider = stockRetriever.dataProvider;
var uiProvider = stockRetriever.uiProvider;
var now = new Date();
beforeEach(function () {
spyOn(dataProvider, 'getPrices').and.callFake(function () {
return $.Deferred(function (dfd) {
dfd.resolve([{
symbol: 'GOOG',
lastTradePrice: 1000.00,
retrieved: now
}]);
}).promise();
});
spyOn(uiProvider, 'displayLoading');
spyOn(uiProvider, 'getSymbol').and.returnValue('GOOG');
spyOn(uiProvider, 'setPrice');
app.fetch();
});
it('should get symbol', function () {
expect(uiProvider.getSymbol).toHaveBeenCalled();
});
it('should display loading', function () {
expect(uiProvider.displayLoading).toHaveBeenCalled();
});
it('should get price', function () {
expect(dataProvider.getPrices).toHaveBeenCalledWith('GOOG');
});
it('should update price information', function () {
expect(uiProvider.setPrice).toHaveBeenCalledWith('GOOG', 1000.00, now);
});
});
});
});