@@ -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-
203212function createMockArpProbe ( sourceMacAddress ) {
204213 let decimals = sourceMacAddress . split ( ':' ) . map ( hex => parseInt ( hex , 16 ) ) ;
205214 assert ( decimals . length === 6 , 'MAC addresses must be six bytes' ) ;
0 commit comments