Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Commit 7d8a937

Browse files
committed
[Tests] Make tests pass
1 parent 57274a6 commit 7d8a937

4 files changed

Lines changed: 47 additions & 28 deletions

File tree

src/MacAddresses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
import padStart from 'lodash/padStart';
33

4-
let MacAddresses = {
4+
const MacAddresses = {
55
getEthernetSource(packet): string {
66
return MacAddresses.decimalToHex(packet.payload.shost.addr);
77
},

src/__mocks__/pcap.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import events from 'events';
2+
13
let pcap = require.requireActual('pcap');
24

35
let pcapMock = jest.genMockFromModule('pcap');
46
pcapMock.decode = pcap.decode;
57

8+
pcapMock.createSession.mockImplementation((interfaceName, filter = '') => {
9+
let session = new events.EventEmitter();
10+
session.device_name = interfaceName;
11+
session.filter = filter;
12+
session.close = jest.genMockFunction();
13+
return session;
14+
});
15+
616
export default pcapMock;

src/__tests__/DashButton-test.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ describe('DashButton', () => {
1717
DashButton = require('../DashButton');
1818
NetworkInterfaces = require('../NetworkInterfaces');
1919

20-
pcap.createSession.mockImplementation(() => createMockPcapSession());
2120
NetworkInterfaces.getDefault.mockReturnValue(NETWORK_INTERFACE);
2221
});
2322

@@ -42,9 +41,25 @@ describe('DashButton', () => {
4241
expect(pcap.createSession.mock.calls.length).toBe(1);
4342
});
4443

44+
it(`creates a pcap session on the default interface`, () => {
45+
let button = new DashButton(MAC_ADDRESS);
46+
button.addListener(() => {});
47+
48+
expect(pcap.createSession.mock.calls.length).toBe(1);
49+
expect(pcap.createSession.mock.calls[0][0]).toBe(NETWORK_INTERFACE);
50+
});
51+
52+
it(`creates a pcap session on the specified interface`, () => {
53+
let button = new DashButton(MAC_ADDRESS, { networkInterface: 'wlan0' });
54+
button.addListener(() => {});
55+
56+
expect(pcap.createSession.mock.calls.length).toBe(1);
57+
expect(pcap.createSession.mock.calls[0][0]).toBe('wlan0');
58+
});
59+
4560
it(`notifies the appropriate listeners for each packet`, () => {
46-
let mockSession = createMockPcapSession();
47-
pcap.createSession.mockReturnValue(mockSession);
61+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
62+
pcap.createSession.mockReturnValueOnce(mockSession);
4863

4964
let button1Listener = jest.genMockFunction();
5065
let button2Listener = jest.genMockFunction();
@@ -67,15 +82,15 @@ describe('DashButton', () => {
6782

6883
it(`waits for listeners for a prior packet to asynchronously complete ` +
6984
`before handling any new packets`, async () => {
70-
let mockSession = createMockPcapSession();
85+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
7186
let listenerCompletion = null;
7287
let originalAddListener = mockSession.addListener;
7388
mockSession.addListener = function addListener(eventName, listener) {
7489
originalAddListener.call(this, eventName, function(...args) {
7590
listenerCompletion = listener.apply(this, args);
7691
});
7792
};
78-
pcap.createSession.mockReturnValue(mockSession);
93+
pcap.createSession.mockReturnValueOnce(mockSession);
7994

8095
let button = new DashButton(MAC_ADDRESS);
8196
let calls = 0;
@@ -93,8 +108,8 @@ describe('DashButton', () => {
93108
});
94109

95110
it(`waits for all listeners even if some threw an error`, async () => {
96-
let mockSession = createMockPcapSession();
97-
pcap.createSession.mockReturnValue(mockSession);
111+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
112+
pcap.createSession.mockReturnValueOnce(mockSession);
98113

99114
let button = new DashButton(MAC_ADDRESS);
100115
let errorCount = 0;
@@ -128,8 +143,8 @@ describe('DashButton', () => {
128143
});
129144

130145
it(`runs its async listeners concurrently`, () => {
131-
let mockSession = createMockPcapSession();
132-
pcap.createSession.mockReturnValue(mockSession);
146+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
147+
pcap.createSession.mockReturnValueOnce(mockSession);
133148

134149
let button = new DashButton(MAC_ADDRESS);
135150
let calls = 0;
@@ -149,8 +164,8 @@ describe('DashButton', () => {
149164
});
150165

151166
it(`removes packet listeners when a button has no more listeners`, () => {
152-
let mockSession = createMockPcapSession();
153-
pcap.createSession.mockReturnValue(mockSession);
167+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
168+
pcap.createSession.mockReturnValueOnce(mockSession);
154169

155170
let button = new DashButton(MAC_ADDRESS);
156171
let subscription1 = button.addListener(() => {});
@@ -164,8 +179,8 @@ describe('DashButton', () => {
164179
});
165180

166181
it(`doesn't throw if you remove a subscription twice`, () => {
167-
let mockSession = createMockPcapSession();
168-
pcap.createSession.mockReturnValue(mockSession);
182+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
183+
pcap.createSession.mockReturnValueOnce(mockSession);
169184

170185
let button = new DashButton(MAC_ADDRESS);
171186
let subscription = button.addListener(() => {});
@@ -176,8 +191,8 @@ describe('DashButton', () => {
176191
});
177192

178193
it(`closes the pcap session when no more buttons are listening`, () => {
179-
let mockSession = createMockPcapSession();
180-
pcap.createSession.mockReturnValue(mockSession);
194+
let mockSession = pcap.createSession(NetworkInterfaces.getDefault());
195+
pcap.createSession.mockReturnValueOnce(mockSession);
181196

182197
let button1Listener = jest.genMockFunction();
183198
let button2Listener = jest.genMockFunction();
@@ -194,12 +209,6 @@ describe('DashButton', () => {
194209
});
195210
});
196211

197-
function createMockPcapSession() {
198-
let session = new events.EventEmitter();
199-
session.close = jest.genMockFunction();
200-
return session;
201-
}
202-
203212
function createMockArpProbe(sourceMacAddress) {
204213
let decimals = sourceMacAddress.split(':').map(hex => parseInt(hex, 16));
205214
assert(decimals.length === 6, 'MAC addresses must be six bytes');

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,9 @@ eslint-plugin-babel@^4.0.0:
963963
version "4.0.0"
964964
resolved "https://registry.yarnpkg.com/eslint-plugin-babel/-/eslint-plugin-babel-4.0.0.tgz#a92114e2c493ac3034b030d7ecf96e174a76ef3f"
965965

966-
eslint-plugin-flowtype@^2.25.0:
967-
version "2.25.0"
968-
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.25.0.tgz#c462521ab20ce3d8db819f10ad3c9f1bc7f3f819"
966+
eslint-plugin-flowtype@^2.26.1:
967+
version "2.29.1"
968+
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.29.1.tgz#74cc5603ff0baff6e224482bbd17406b0980f6c3"
969969
dependencies:
970970
lodash "^4.15.0"
971971

@@ -984,9 +984,9 @@ eslint-plugin-import@^2.2.0:
984984
minimatch "^3.0.3"
985985
pkg-up "^1.0.0"
986986

987-
eslint@^3.10.0:
988-
version "3.10.2"
989-
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.10.2.tgz#c9a10e8bf6e9d65651204778c503341f1eac3ce7"
987+
eslint@^3.11.0:
988+
version "3.11.1"
989+
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.11.1.tgz#408be581041385cba947cd8d1cd2227782b55dbf"
990990
dependencies:
991991
babel-code-frame "^6.16.0"
992992
chalk "^1.1.3"

0 commit comments

Comments
 (0)