This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.js
155 lines (111 loc) · 4.48 KB
/
app_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
(function($, TS, backend) {
describe('After loading application', function() {
before(TS.resetApp)
it('disables search button', expectSearchButtonIs(':disabled'))
it('hides loader', expectSearchButtonLoaderIsShown(false))
describe('when entering space for search', function() {
before(function() {
TS.query(' ')
})
it('does not perform search', expectNumCalls(0))
it('disables search button', expectSearchButtonIs(':disabled'))
it('hides loader', expectSearchButtonLoaderIsShown(false))
})
describe('when entering space, term, and space for search, and response has not arrived', function() {
var request = {}
before(function() {
backend.captureRequestTo(request)
TS.query(' term ')
})
after(backend.reset)
it('performs search', expectNumCalls(1))
it('trims query input', expectQueryInRequestIs('term', request))
it('disables search button', expectSearchButtonIs(':disabled'))
it('shows loader', expectSearchButtonLoaderIsShown(true))
describe('and when successful response arrives', function() {
before(function() {
backend.respondSuccessTo(request)
})
it('enables search button', expectSearchButtonIs(':enabled'))
it('hides loader', expectSearchButtonLoaderIsShown(false))
it('shows successful results', expectResultsIs('success'))
})
})
describe('when query results to backend response failure', function() {
before(function(done) {
backend.respondFailure().then(done)
TS.query('another term')
})
after(backend.reset)
it('shows failure results', expectResultsIs('failure'))
})
describe('when querying the backend twice in sequence and the response to the second last request arrives last', function() {
before(function() {
var firstRequest = {}
var secondRequest = {}
backend.captureRequestTo(firstRequest)
TS.query('lol')
backend.captureRequestTo(secondRequest)
TS.query('zap')
backend.respondSuccessTo(secondRequest)
backend.respondSuccessTo(firstRequest)
})
after(backend.reset)
it('issues two requests', expectNumCalls(2))
it('shows the result corresponding to the last request', expectResultsContains('original: zap'))
})
describe('when entering input and changing it back to empty within throttling period (after page load)', function() {
var throttler
before(function() {
throttler = TS.pauseThrottling()
TS.resetApp()
TS.query('input')
TS.query('')
throttler.resumeLast()
})
after(function() {
throttler.restore()
TS.resetApp()
})
it('does not perform search', expectNumCalls(0))
it('disables search button', expectSearchButtonIs(':disabled'))
describe('and then entering new input and waiting for the throttling period', function() {
before(function(done) {
backend.respondSuccess().then(done)
TS.query('input')
throttler.resumeLast()
})
after(backend.reset)
it('performs search', expectNumCalls(1))
it('enables search button', expectSearchButtonIs(':enabled'))
describe('and then changing the input and changing it back within throttling period', function() {
before(function() {
TS.query('inp')
TS.query('input')
throttler.resumeLast()
})
it('does not perform search', expectNumCalls(0))
it('enables search button', expectSearchButtonIs(':enabled'))
})
})
})
function expectNumCalls(num) {
return function() { backend.checkCalls().should.equal(num) }
}
function expectQueryInRequestIs(expected, request) {
return function() { request.query.should.equal(expected) }
}
function expectSearchButtonIs(status) {
return function() { $('#search .controls button').is(status).should.be.ok }
}
function expectSearchButtonLoaderIsShown(isShown) {
return function() { $('#search .controls .searchButton').hasClass('loading').should.equal(isShown) }
}
function expectResultsIs(klass) {
return function() { $('#search .results').should.have.class(klass) }
}
function expectResultsContains(text) {
return function() { $('#search .results').should.contain(text) }
}
})
})(window.jQuery, window.Test.Support, window.Test.fakeBackend)