-
Notifications
You must be signed in to change notification settings - Fork 482
/
NeoPixelTest.js
257 lines (223 loc) · 6.31 KB
/
NeoPixelTest.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/** @file Tests for our johnny-five Led.RGB wrapper */
import {expect} from '../../../../util/configuredChai';
import sinon from 'sinon';
import five from '@code-dot-org/johnny-five';
import NeoPixel from '@cdo/apps/lib/kits/maker/NeoPixel';
describe('NeoPixel', function() {
beforeEach(() => {
// We stub five.Led.RGB's superclass to avoid calling any johnny-five
// logic that requires a board.
sinon.stub(five.Board, 'Component');
});
afterEach(() => {
five.Board.Component.restore();
});
it('is a five.Led.RGB', () => {
const led = new NeoPixel({
controller: makeStubController()
});
expect(led).to.be.an.instanceOf(five.Led.RGB);
});
describe('on()', () => {
let led;
beforeEach(() => {
sinon.spy(five.Led.RGB.prototype, 'on');
sinon.spy(five.Led.RGB.prototype, 'stop');
led = new NeoPixel({
controller: makeStubController()
});
});
afterEach(() => {
five.Led.RGB.prototype.stop.restore();
five.Led.RGB.prototype.on.restore();
});
it(`calls the parent on() implementation`, () => {
five.Led.RGB.prototype.on.reset();
led.on();
expect(five.Led.RGB.prototype.on).to.have.been.calledOnce;
});
it(`calls stop() on the led to end any animations`, () => {
five.Led.RGB.prototype.stop.reset();
led.on();
expect(five.Led.RGB.prototype.stop).to.have.been.calledOnce;
});
});
describe('off()', () => {
let led;
beforeEach(() => {
sinon.spy(five.Led.RGB.prototype, 'off');
sinon.spy(five.Led.RGB.prototype, 'stop');
led = new NeoPixel({
controller: makeStubController()
});
});
afterEach(() => {
five.Led.RGB.prototype.stop.restore();
five.Led.RGB.prototype.off.restore();
});
it(`calls the parent off() implementation`, () => {
five.Led.RGB.prototype.off.reset();
led.off();
expect(five.Led.RGB.prototype.off).to.have.been.called;
});
it(`calls stop() on the led to end any animations`, () => {
five.Led.RGB.prototype.stop.reset();
led.off();
expect(five.Led.RGB.prototype.stop).to.have.been.calledOnce;
});
});
describe('blink()', () => {
let led, clock;
beforeEach(() => {
clock = sinon.useFakeTimers();
led = new NeoPixel({
controller: makeStubController()
});
sinon.spy(led, 'stop');
sinon.spy(led, 'toggle');
});
afterEach(() => {
led.toggle.restore();
led.stop.restore();
clock.restore();
});
it(`calls stop() only once when blink starts`, () => {
led.stop.reset();
led.blink(100);
expect(led.stop).to.have.been.calledOnce;
// Pass some time and make sure it doesn't happen again
led.stop.reset();
clock.tick(100);
expect(led.toggle).to.have.been.calledOnce;
expect(led.stop).not.to.have.been.called;
clock.tick(100);
expect(led.toggle).to.have.been.calledTwice;
expect(led.stop).not.to.have.been.called;
clock.tick(100);
expect(led.toggle).to.have.been.calledThrice;
expect(led.stop).not.to.have.been.called;
});
});
describe('color() valid arguments', () => {
let led;
// The tests are a little redundant with similar tests in johnny-five
// (the color method is just a pass-through) but I left these in because
// we particularly care about this behavior in Maker Toolkit.
// Note: The Mozilla color value documentation was very
// helpful when writing these tests:
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
beforeEach(() => {
led = new NeoPixel({
controller: makeStubController()
});
});
it('hexadecimal color "#306090"', () => {
led.color('#306090');
expect(led.color()).to.deep.equal({
red: 0x30,
green: 0x60,
blue: 0x90
});
});
it('CSS1 color keywords "lime"', () => {
led.color('lime');
expect(led.color()).to.deep.equal({
red: 0x00,
green: 0xff,
blue: 0x00
});
});
it('CSS2 color keywords "orange"', () => {
led.color('orange');
expect(led.color()).to.deep.equal({
red: 0xff,
green: 0xa5,
blue: 0x00
});
});
it('CSS3 color keywords "chocolate"', () => {
led.color('chocolate');
expect(led.color()).to.deep.equal({
red: 0xd2,
green: 0x69,
blue: 0x1e
});
});
it('CSS4 color keywords "rebeccapurple"', () => {
// See: https://codepen.io/trezy/post/honoring-a-great-man
led.color('rebeccapurple');
expect(led.color()).to.deep.equal({
red: 0x66,
green: 0x33,
blue: 0x99
});
});
it('CSS functional notation "rgb(30, 60, 90)"', () => {
led.color('rgb(30, 60, 90)');
expect(led.color()).to.deep.equal({
red: 30,
green: 60,
blue: 90
});
});
it('CSS functional notation "rgba(30, 60, 90, 0.5)"', () => {
led.color('rgba(30, 60, 90, 0.5)');
expect(led.color()).to.deep.equal({
red: 15,
green: 30,
blue: 45
});
});
it('CSS4 functional notation "rgb(30, 60, 90, 0.5)"', () => {
led.color('rgb(30, 60, 90, 0.5)');
expect(led.color()).to.deep.equal({
red: 15,
green: 30,
blue: 45
});
});
it('CSS4 functional notation "rgba(30, 60, 90)"', () => {
led.color('rgba(30, 60, 90)');
expect(led.color()).to.deep.equal({
red: 30,
green: 60,
blue: 90
});
});
it('Array of color values [30, 60, 90]', () => {
led.color([30, 60, 90]);
expect(led.color()).to.deep.equal({
red: 30,
green: 60,
blue: 90
});
});
it('Color object {red: 30, green: 60, blue: 90}', () => {
led.color({red: 30, green: 60, blue: 90});
expect(led.color()).to.deep.equal({
red: 30,
green: 60,
blue: 90
});
});
it('Separate color arguments (30, 60, 90)', () => {
led.color(30, 60, 90);
expect(led.color()).to.deep.equal({
red: 30,
green: 60,
blue: 90
});
});
});
});
function makeStubController() {
return {
initialize: {
value: () => {}
},
write: {
writable: true,
value: () => {}
}
};
}