-
Notifications
You must be signed in to change notification settings - Fork 742
/
load.js
204 lines (179 loc) · 4.99 KB
/
load.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
/* global Rule, commons */
describe('axe._load', function() {
'use strict';
afterEach(function() {
axe._audit = null;
});
it('should be a function', function() {
assert.isFunction(axe._load);
});
it('should create a new audit', function() {
var success = false;
var orig = window.Audit;
var audit = { rules: [] };
window.Audit = function() {
success = true;
};
axe._load(audit);
assert.isTrue(success);
window.Audit = orig;
});
it('should push rules on the Audit', function() {
var mockAudit = {
rules: [{ id: 'monkeys' }, { id: 'bananas' }]
};
axe._load(mockAudit);
assert.instanceOf(axe._audit.rules[0], Rule);
assert.instanceOf(axe._audit.rules[1], Rule);
assert.equal(axe._audit.rules[0].id, 'monkeys');
assert.equal(axe._audit.rules[1].id, 'bananas');
});
it('should locally define commons', function() {
axe._load({
commons: 'foo'
});
assert.equal(commons, 'foo');
});
it('should define commons on axe', function() {
axe._load({
commons: 'foo'
});
assert.equal(axe.commons, 'foo');
});
it('should load with a lang', function() {
axe._load({
lang: 'ja'
});
assert.equal(axe._audit.lang, 'ja');
});
describe('respondable subscriber', function() {
it('should add a respondable subscriber', function() {
var mockAudit = {
rules: [{ id: 'monkeys' }, { id: 'bananas' }]
};
var orig = window.utils.respondable.subscribe;
axe.utils.respondable.subscribe = function(topic, callback) {
assert.ok(topic.indexOf('axe.') === 0);
assert.isFunction(callback);
};
axe._load(mockAudit);
window.utils.respondable.subscribe = orig;
});
describe('given command rules', function() {
it('should call `runRules` and default context to empty object', function(done) {
var mockAudit = {
rules: []
};
var origSub = window.utils.respondable.subscribe;
var orig = window.runRules;
window.runRules = function(context, options, callback) {
assert.deepEqual(context, {});
assert.isFunction(callback);
done();
};
axe.utils.respondable.subscribe = function(topic, callback) {
callback({ data: 'iscool', command: 'rules' }, undefined, function(
response
) {
// ping callback will call this response function
assert.ok(response);
});
};
axe._load(mockAudit);
window.utils.respondable.subscribe = origSub;
window.runRules = orig;
});
it('should pass data.context to `runRules`', function(done) {
var origSub = window.utils.respondable.subscribe;
var orig = window.runRules;
window.runRules = function(context, options, callback) {
assert.deepEqual(context, { include: ['monkeys'] });
assert.isFunction(callback);
done();
};
axe.utils.respondable.subscribe = function(topic, callback) {
callback(
{ command: 'rules', context: { include: ['monkeys'] } },
undefined,
function(response) {
assert.ok(response);
}
);
};
axe._load({
rules: []
});
window.utils.respondable.subscribe = origSub;
window.runRules = orig;
});
it('should default include to current document if none are found', function(done) {
var origSub = axe.utils.respondable.subscribe;
var orig = window.runRules;
var expected = { include: [document] };
window.runRules = function(context) {
assert.deepEqual(context, expected);
done();
};
axe.utils.respondable.subscribe = function(topic, callback) {
callback(
{ command: 'rules', context: { include: [] } },
undefined,
function() {}
);
};
axe._load({
rules: []
});
window.runRules = orig;
axe.utils.respondable.subscribe = origSub;
});
});
describe('given command cleanup-plugins', function() {
it('should call `cleanupPlugins`', function(done) {
var mockAudit = {
rules: []
};
var origSub = window.utils.respondable.subscribe;
var orig = window.cleanupPlugins;
window.cleanupPlugins = function(callback) {
assert.isFunction(callback);
done();
};
axe.utils.respondable.subscribe = function(topic, callback) {
callback(
{
command: 'cleanup-plugin'
},
undefined,
function(response) {
// ping callback will call this response function
assert.ok(response);
}
);
};
axe._load(mockAudit);
window.utils.respondable.subscribe = origSub;
window.cleanupPlugins = orig;
});
});
it('should respond', function() {
var origSub = window.utils.respondable.subscribe;
var expected = { data: { include: ['monkeys'] } };
axe.utils.respondable.subscribe = function(topic, callback) {
callback({}, undefined, function responder(data) {
if (topic === 'axe.start') {
assert.equal(data, expected);
} else if (topic === 'axe.ping') {
assert.deepEqual(data, { axe: true });
} else {
assert.ok(false);
}
});
};
axe._load({
rules: []
});
window.utils.respondable.subscribe = origSub;
});
});
});