-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
185 lines (162 loc) · 4.62 KB
/
utils.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
const tts = require("./basic-tts");
/**
* Function that does nothing.
*/
const NOP = () => {};
const basicMockSpeechSynthesis = {
getVoices: NOP,
speak: NOP,
};
const basicMockWindow = {
speechSynthesis: basicMockSpeechSynthesis,
SpeechSynthesisUtterance: NOP,
};
class MockSpeechSynthesisUtterance {
constructor(text) {
this.voice = null;
this.text = text;
this.lang = "en-US";
this.volume = -1;
this.pitch = -1;
this.rate = -1;
this.onerror = NOP;
this.onend = NOP;
}
}
/**
* Generate a mock Window instance with specific getVoices behavior.
*
* The mocked getVoices will return an empty array for `n` times,
* after which it will return the provided data.
*
* @param {Number} n - The number of attempts before returning data.
* @param {Object=} data - The data to return. Should have a length attribute.
* Otherwise, the input will be overwritten with an empty array.
* @returns {Object}
*/
const getMockWindowWithAttempts = (n, data) => (
{
SpeechSynthesisUtterance: MockSpeechSynthesisUtterance,
speechSynthesis: {
getVoices: mockGetVoices(n, data),
speak: NOP,
}
}
);
/**
* Check that the properties of two utterances are equal.
*
* @param {SpeechSynthesisUtterance|MockSpeechSynthesisUtterance} utterance -
* The utterance to check.
* @param {Object} props - The expected property values.
*/
const assertUtterancePropsEqual = (utterance, props) => {
for (key of ["rate", "voice", "pitch", "text", "volume", "lang"]) {
expect(utterance[key]).toEqual(props[key]);
}
};
/**
* Create a mock Window with a duck-typed utterance class.
*
* @param names - The names of the speakers to provide as data.
* @returns {Object}
*/
const getMockWindowWithVoices = (...names) => (
getMockWindowWithAttempts(0, names.map(name => (
{name}
)))
);
/**
* Force an object to be an array-like.
*
* If the input does not have a length attribute, it will be
* overwritten as an empty array.
*
* @param {Object} obj - The force to force to be an array.
* @returns {Array}
*/
const forceAsArray = (obj) => {
obj = obj || [];
return "length" in obj ? obj : [];
};
/**
* Check the behavior of checkVoices.
*
* If valid return data is provided, we expect no rejected Promises. If no
* valid data is provided, we expected no resolved Promises.
*
* @param {Function} done - Jasmine function used indicating a finished test.
* @param {Number} n - The number of attempts to retrieve voices.
* @param {Object=} data - The data to return. Should have a length attribute.
* Otherwise, the input will be overwritten with an empty array.
* @returns {Promise}
*/
const checkCheckVoices = (done, n, data) => {
data = forceAsArray(data);
tts.enableTesting(getMockWindowWithAttempts(n, data));
return tts.checkVoices(n).then((result) => {
if (data.length === 0) {
done(new Error(`Unexpected data received: ${result}`));
} else {
expect(result.voices).toEqual(data);
done();
}
}).catch((err) => {
if (data.length === 0) {
expect(err).toEqual({
msg: "No voices available for use."
});
done();
} else {
done(new Error(`Unexpected error: ${JSON.stringify(err)}`));
}
});
};
/**
* Wrapper around test functions to check `console.warns` content.
*
* @param {Function} fn - The test function to call.
* @param stmts - List of strings to check in log content.
*/
const checkWarns = (fn, ...stmts) => {
const log = [];
const original = console.warn;
global.console.warn = (content) => {
log.push(content);
};
fn();
for (const stmt of stmts) {
expect(log).toContain(stmt);
}
global.console.warn = original;
};
/**
* Mock getting voices and returning data after a certain number of attempts.
*
* @param {Number} n - The number of attempts before returning data.
* @param {Object=} data - The data to return. Should have a length attribute.
* Otherwise, the input will be overwritten with an empty array.
* @returns {Function}
*/
const mockGetVoices = (n, data) => {
data = forceAsArray(data);
let counter = 0;
return () => {
if (counter < n) {
counter++;
return [];
} else {
return data;
}
};
};
module.exports = {
NOP,
basicMockSpeechSynthesis,
basicMockWindow,
getMockWindowWithVoices,
getMockWindowWithAttempts,
assertUtterancePropsEqual,
checkCheckVoices,
checkWarns,
};