-
Notifications
You must be signed in to change notification settings - Fork 482
/
LedTest.js
119 lines (101 loc) · 2.79 KB
/
LedTest.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
/** @file Tests for our johnny-five Led wrapper */
import {expect} from '../../../../util/configuredChai';
import sinon from 'sinon';
import five from '@code-dot-org/johnny-five';
import makeStubBoard from './makeStubBoard';
import Led from '@cdo/apps/lib/kits/maker/Led';
describe('Led', function() {
it('is a five.Led', () => {
const led = newTestLed();
expect(led).to.be.an.instanceOf(five.Led);
});
describe('on()', () => {
let led;
beforeEach(() => {
sinon.stub(five.Led.prototype, 'on');
sinon.stub(five.Led.prototype, 'stop');
led = newTestLed();
});
afterEach(() => {
five.Led.prototype.stop.restore();
five.Led.prototype.on.restore();
});
it(`calls the parent on() implementation`, () => {
led.on();
expect(five.Led.prototype.on).to.have.been.calledOnce;
});
it(`calls stop() on the led to end any animations`, () => {
led.on();
expect(five.Led.prototype.stop).to.have.been.calledOnce;
});
});
describe('off()', () => {
let led;
beforeEach(() => {
sinon.stub(five.Led.prototype, 'off');
sinon.stub(five.Led.prototype, 'stop');
led = newTestLed();
});
afterEach(() => {
five.Led.prototype.stop.restore();
five.Led.prototype.off.restore();
});
it(`calls stop() on the led to end any animations`, () => {
led.off();
expect(five.Led.prototype.stop).to.have.been.calledOnce;
});
it(`calls the parent off() implementation`, () => {
led.off();
expect(five.Led.prototype.off).to.have.been.called;
});
});
describe('blink()', () => {
let led, clock;
beforeEach(() => {
clock = sinon.useFakeTimers();
led = newTestLed();
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;
});
});
});
function newTestLed() {
return new Led({
controller: makeStubController(),
board: makeStubBoard()
});
}
function makeStubController() {
return {
initialize: {
value: () => {}
},
write: {
value: () => {}
},
update: {
value: () => {}
}
};
}