-
Notifications
You must be signed in to change notification settings - Fork 482
/
SwitchTest.js
140 lines (125 loc) · 4.25 KB
/
SwitchTest.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
import {expect} from '../../../../util/configuredChai';
import sinon from 'sinon';
import {EventEmitter} from 'events'; // provided by webpack's node-libs-browser
import Switch, {
READ_ONLY_PROPERTIES,
READ_WRITE_PROPERTIES
} from '@cdo/apps/lib/kits/maker/Switch';
describe('Switch', () => {
let fakeJohnnyFiveSwitch;
before(() =>
sinon
.stub(Switch, '_constructFiveSwitchController')
.callsFake((board, pin) => {
fakeJohnnyFiveSwitch = new FakeJohnnyFiveSwitch({board, pin});
return fakeJohnnyFiveSwitch;
})
);
afterEach(() => (fakeJohnnyFiveSwitch = null));
after(() => Switch._constructFiveSwitchController.restore());
it('is an EventEmitter', () => {
const testObj = new Switch({});
expect(testObj).to.be.an.instanceOf(EventEmitter);
});
describe('read-only pass-through properties', () => {
READ_ONLY_PROPERTIES.forEach(readOnlyProp => {
it(`reads ${readOnlyProp} from the inner controller`, () => {
const testObj = new Switch({});
expect(testObj[readOnlyProp]).to.equal(
fakeJohnnyFiveSwitch[readOnlyProp]
);
});
it(`cannot write ${readOnlyProp}`, () => {
const testObj = new Switch({});
expect(() => {
testObj[readOnlyProp] = 'new value';
}).to.throw;
});
});
});
describe('read-write pass-through properties', () => {
READ_WRITE_PROPERTIES.forEach(readWriteProp => {
it(`reads ${readWriteProp} from the inner controller`, () => {
const testObj = new Switch({});
expect(testObj[readWriteProp]).to.equal(
fakeJohnnyFiveSwitch[readWriteProp]
);
});
it(`can write ${readWriteProp}`, () => {
const testObj = new Switch({});
testObj[readWriteProp] = 'new value';
expect(testObj[readWriteProp]).to.equal('new value');
});
});
});
describe('events', () => {
let testObj, openSpy, closeSpy, changeSpy;
beforeEach(() => {
testObj = new Switch({});
openSpy = sinon.spy();
closeSpy = sinon.spy();
changeSpy = sinon.spy();
testObj.on('open', openSpy);
testObj.on('close', closeSpy);
testObj.on('change', changeSpy);
});
it("emits 'open' and 'change' when the first event from the inner controller is 'open'", () => {
fakeJohnnyFiveSwitch.emit('open');
expect(openSpy).to.have.been.calledOnce;
expect(closeSpy).not.to.have.been.called;
expect(changeSpy).to.have.been.calledOnce.and.calledWith(
testObj.openValue
);
});
it("emits 'close' and 'change' when the first event from the inner controller is 'close'", () => {
fakeJohnnyFiveSwitch.emit('close');
expect(openSpy).not.to.have.been.called;
expect(closeSpy).to.have.been.calledOnce;
expect(changeSpy).to.have.been.calledOnce.and.calledWith(
testObj.closeValue
);
});
it("does not emit extra 'open' events", () => {
fakeJohnnyFiveSwitch.emit('open');
fakeJohnnyFiveSwitch.emit('open');
fakeJohnnyFiveSwitch.emit('open');
expect(openSpy).to.have.been.calledOnce;
expect(closeSpy).not.to.have.been.called;
expect(changeSpy).to.have.been.calledOnce.and.calledWith(
testObj.openValue
);
});
it("does not emit extra 'close' events", () => {
fakeJohnnyFiveSwitch.emit('close');
fakeJohnnyFiveSwitch.emit('close');
fakeJohnnyFiveSwitch.emit('close');
expect(openSpy).not.to.have.been.called;
expect(closeSpy).to.have.been.calledOnce;
expect(changeSpy).to.have.been.calledOnce.and.calledWith(
testObj.closeValue
);
});
it("emits events when changing from 'open' to 'close' or back", () => {
fakeJohnnyFiveSwitch.emit('open');
fakeJohnnyFiveSwitch.emit('close');
fakeJohnnyFiveSwitch.emit('open');
fakeJohnnyFiveSwitch.emit('close');
expect(openSpy).to.have.been.calledTwice;
expect(closeSpy).to.have.been.calledTwice;
expect(changeSpy).to.have.callCount(4);
});
});
});
class FakeJohnnyFiveSwitch extends EventEmitter {
constructor({board, pin}) {
super();
this.board = board;
this.pin = pin;
}
isOpen = false;
isClosed = false;
value = 0;
closeValue = 0;
openValue = 1;
invert = false;
}