-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
175 lines (136 loc) · 4.54 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
describe('readystate', function () {
'use strict';
var assume = require('assume')
, readystate = require('./readystate')
, RS = readystate.constructor;
beforeEach(function () {
readystate = new RS();
});
it('exposes a readyState property', function () {
assume(readystate.readyState).equals(RS.UNKNOWN);
});
describe('#change', function () {
it('allows strings to set the readyState', function () {
readystate.change('complete');
assume(readystate.readyState).equals(RS.COMPLETE);
});
it('allows strings to set the readyState', function () {
readystate.change(RS.LOADING);
assume(readystate.readyState).equals(RS.LOADING);
});
it('only allows changing if its a new readystate', function () {
readystate.change(RS.LOADING);
assume(readystate.readyState).equals(RS.LOADING);
readystate.change(RS.UNKNOWN);
assume(readystate.readyState).equals(RS.LOADING);
});
it('accepts random unknown states', function () {
assume(readystate.readyState).equals(RS.UNKNOWN);
readystate.change('foo bar banana');
assume(readystate.readyState).equals(RS.UNKNOWN);
});
it('executes the assigned event listeners', function (next) {
readystate.complete(function (previous) {
assume(previous).equals(RS.UNKNOWN);
next();
});
readystate.change('complete');
});
it('executes lower level event emitters', function (next) {
readystate.loading(function () {
next();
});
readystate.change('complete');
});
it('calls multiple event listeners', function (next) {
var pattern = '';
readystate.loading(function () { pattern = 'a'; });
readystate.loading(function () {
assume(pattern).equals('a');
next();
});
readystate.change('loading');
});
});
RS.states.filter(function (state) {
return RS[state] > RS.UNKNOWN;
}).forEach(function (state) {
state = state.toLowerCase();
describe('#'+ state, function () {
it('calls the event listener when the event is triggered', function (next) {
readystate[state](function changed() {
next();
});
readystate.change(state);
});
it('call the function when the state is already set', function (next) {
readystate.change(state);
readystate[state](function changed() {
next();
});
});
it('call the function when the state is greater', function (next) {
readystate.change('complete');
readystate[state](function changed() {
next();
});
});
it('is an async call', function (next) {
readystate.change('complete');
var called = false;
readystate[state](function changed() {
called = true;
next();
});
assume(called).is.false();
});
});
});
describe('#is', function () {
it('is a function', function () {
assume(readystate.is).is.a('function');
});
it('always returns `true` for the all mode', function () {
assume(readystate.is(readystate.ALL)).is.true();
assume(readystate.is('all')).is.true();
});
it('returns false for future ready states', function () {
assume(readystate.is('complete')).is.false();
readystate.change('complete');
assume(readystate.is('complete')).is.true();
});
});
describe('#clean', function () {
it('is a function', function () {
assume(readystate.clean).is.a('function');
});
it('changes strings to numbers', function () {
assume(readystate.clean('unknown', true)).equals(1);
assume(readystate.clean('UNKNOWN', true)).equals(1);
assume(readystate.clean(1, true)).equals(1);
});
it('changes numbers to strings', function () {
assume(readystate.clean('unknown')).equals('UNKNOWN');
assume(readystate.clean('UNKNOWN')).equals('UNKNOWN');
assume(readystate.clean(1)).equals('UNKNOWN');
});
});
describe('#removeAllListeners', function () {
it('is a function', function () {
assume(readystate.removeAllListeners).is.a('function');
});
it('does not react to events anymore when all have been removed', function (next) {
var interactiveReached = false;
readystate.interactive(function () {
interactiveReached = true
});
readystate
.removeAllListeners()
.change('interactive');
setTimeout(function() {
assume(interactiveReached).is.false();
next();
}, 20)
});
});
});