1
1
import { expect , use } from 'chai'
2
2
import chaiAsPromised from 'chai-as-promised'
3
- import type { PuppeteerNode , Browser } from 'puppeteer-core'
3
+ import type { PuppeteerNode , Browser , Page } from 'puppeteer-core'
4
4
import sinon from 'sinon'
5
5
import sinonChai from 'sinon-chai'
6
6
import { MessageHandler } from '../../src/plugin/setup'
7
7
import { setup } from '../../src/plugin'
8
- import * as activateMainTabExport from '../../src/plugin/activateMainTab'
9
8
10
9
use ( chaiAsPromised )
11
10
use ( sinonChai )
12
11
13
12
type StubbedMessageHandler = sinon . SinonStub < Parameters < MessageHandler > , ReturnType < MessageHandler > >
14
13
15
14
describe ( '#setup' , ( ) => {
15
+ let mockPage : Partial < Page >
16
16
let mockBrowser : Partial < Browser >
17
17
let mockPuppeteer : Pick < PuppeteerNode , 'connect' >
18
18
let on : sinon . SinonStub
@@ -30,9 +30,13 @@ describe('#setup', () => {
30
30
}
31
31
32
32
beforeEach ( ( ) => {
33
- sinon . stub ( activateMainTabExport , 'activateMainTab' )
33
+ mockPage = {
34
+ evaluate : sinon . stub ( ) . resolves ( ) ,
35
+ }
36
+
34
37
mockBrowser = {
35
38
disconnect : sinon . stub ( ) . resolves ( ) ,
39
+ pages : sinon . stub ( ) . resolves ( [ mockPage ] ) ,
36
40
}
37
41
38
42
mockPuppeteer = {
@@ -50,8 +54,6 @@ describe('#setup', () => {
50
54
51
55
afterEach ( ( ) => {
52
56
sinon . reset ( )
53
-
54
- ; ( activateMainTabExport . activateMainTab as sinon . SinonStub ) . restore ( )
55
57
} )
56
58
57
59
it ( 'registers `after:browser:launch` and `task` handlers' , ( ) => {
@@ -229,19 +231,18 @@ describe('#setup', () => {
229
231
)
230
232
} )
231
233
232
- it ( 'calls activateMainTab if there is a page in the browser' , async ( ) => {
233
- ( activateMainTabExport . activateMainTab as sinon . SinonStub ) . withArgs ( mockBrowser ) . resolves ( )
234
+ it ( 'calls page.evaluate() if there is a page in the browser' , async ( ) => {
234
235
setup ( { on, onMessage, puppeteer : mockPuppeteer as PuppeteerNode } )
235
236
const task = getTask ( )
236
237
237
238
simulateBrowserLaunch ( )
238
239
await task ( { name : testTask , args : [ ] } )
239
240
240
- expect ( activateMainTabExport . activateMainTab ) . to . be . calledWith ( mockBrowser )
241
+ expect ( mockPage . evaluate ) . to . be . calledWith ( sinon . match . func , 2000 )
241
242
} )
242
243
243
244
it ( 'returns an error object if activateMainTab rejects' , async ( ) => {
244
- ( activateMainTabExport . activateMainTab as sinon . SinonStub ) . withArgs ( mockBrowser ) . rejects ( )
245
+ mockBrowser . pages = sinon . stub ( ) . rejects ( new Error ( 'Failed to get pages' ) )
245
246
246
247
setup ( { on, onMessage, puppeteer : mockPuppeteer as PuppeteerNode } )
247
248
simulateBrowserLaunch ( )
@@ -263,7 +264,7 @@ describe('#setup', () => {
263
264
264
265
await task ( { name : testTask , args : [ ] } )
265
266
266
- expect ( activateMainTabExport . activateMainTab ) . not . to . be . called
267
+ expect ( mockPage . evaluate ) . not . to . be . called
267
268
} )
268
269
269
270
it ( 'does not try to activate main tab when the browser is electron' , async ( ) => {
@@ -272,7 +273,7 @@ describe('#setup', () => {
272
273
const task = getTask ( )
273
274
274
275
await task ( { name : testTask , args : [ ] } )
275
- expect ( activateMainTabExport . activateMainTab ) . not . to . be . called
276
+ expect ( mockPage . evaluate ) . not . to . be . called
276
277
} )
277
278
} )
278
279
0 commit comments