|
| 1 | +import { expect, use } from 'chai' |
| 2 | +import chaiAsPromised from 'chai-as-promised' |
| 3 | +import sinon from 'sinon' |
| 4 | +import sinonChai from 'sinon-chai' |
| 5 | +import type { Browser, Page } from 'puppeteer-core' |
| 6 | +import { activateMainTab, ACTIVATION_TIMEOUT } from '../../src/plugin/activateMainTab' |
| 7 | + |
| 8 | +use(chaiAsPromised) |
| 9 | +use(sinonChai) |
| 10 | + |
| 11 | +describe('activateMainTab', () => { |
| 12 | + let clock: sinon.SinonFakeTimers |
| 13 | + let prevWin: Window |
| 14 | + let prevDoc: Document |
| 15 | + let prevTop: Window & typeof globalThis |
| 16 | + let window: Partial<Window> |
| 17 | + let mockDocument: Partial<Document> & { |
| 18 | + defaultView: Window & typeof globalThis |
| 19 | + } |
| 20 | + let mockTop: Partial<Window & typeof globalThis> |
| 21 | + let mockBrowser: Partial<Browser> |
| 22 | + let mockPage: Partial<Page> |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + clock = sinon.useFakeTimers() |
| 26 | + |
| 27 | + window = { |
| 28 | + addEventListener: sinon.stub(), |
| 29 | + removeEventListener: sinon.stub(), |
| 30 | + |
| 31 | + // @ts-ignore sinon gets confused about postMessage type declaration |
| 32 | + postMessage: sinon.stub(), |
| 33 | + } |
| 34 | + |
| 35 | + mockDocument = { |
| 36 | + defaultView: window as Window & typeof globalThis, |
| 37 | + } |
| 38 | + |
| 39 | + mockTop = mockDocument.defaultView |
| 40 | + |
| 41 | + // activateMainTab is eval'd in browser context, but the tests exec in a |
| 42 | + // node context. We don't necessarily need to do this swap, but it makes the |
| 43 | + // tests more portable. |
| 44 | + // @ts-ignore |
| 45 | + prevWin = global.window |
| 46 | + prevDoc = global.document |
| 47 | + // @ts-ignore |
| 48 | + prevTop = global.top |
| 49 | + //@ts-ignore |
| 50 | + global.window = window |
| 51 | + global.document = mockDocument as Document |
| 52 | + //@ts-ignore |
| 53 | + global.top = mockTop |
| 54 | + |
| 55 | + mockPage = { |
| 56 | + evaluate: sinon.stub().callsFake((fn, ...args) => fn(...args)), |
| 57 | + } |
| 58 | + |
| 59 | + mockBrowser = { |
| 60 | + pages: sinon.stub(), |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + clock.restore() |
| 66 | + // @ts-ignore |
| 67 | + global.window = prevWin |
| 68 | + // @ts-ignore |
| 69 | + global.top = prevTop |
| 70 | + global.document = prevDoc |
| 71 | + }) |
| 72 | + |
| 73 | + it('sends a tab activation request to the plugin, and resolves when the ack event is received', async () => { |
| 74 | + const pagePromise = Promise.resolve([mockPage]) |
| 75 | + |
| 76 | + ;(mockBrowser.pages as sinon.SinonStub).returns(pagePromise) |
| 77 | + const p = activateMainTab(mockBrowser as Browser) |
| 78 | + |
| 79 | + await pagePromise |
| 80 | + // @ts-ignore |
| 81 | + window.addEventListener.withArgs('message').yield({ data: { message: 'cypress:extension:main:tab:activated' } }) |
| 82 | + expect(window.postMessage).to.be.calledWith({ message: 'cypress:extension:activate:main:tab' }) |
| 83 | + |
| 84 | + expect(p).to.eventually.be.true |
| 85 | + }) |
| 86 | + |
| 87 | + it('sends a tab activation request to the plugin, and rejects if it times out', async () => { |
| 88 | + const pagePromise = Promise.resolve([mockPage]) |
| 89 | + |
| 90 | + ;(mockBrowser.pages as sinon.SinonStub).returns(pagePromise) |
| 91 | + await pagePromise |
| 92 | + |
| 93 | + const p = activateMainTab(mockBrowser as Browser) |
| 94 | + |
| 95 | + clock.tick(ACTIVATION_TIMEOUT + 1) |
| 96 | + |
| 97 | + expect(p).to.be.rejected |
| 98 | + }) |
| 99 | + |
| 100 | + describe('when cy in cy', () => { |
| 101 | + beforeEach(() => { |
| 102 | + mockDocument.defaultView = {} as Window & typeof globalThis |
| 103 | + }) |
| 104 | + |
| 105 | + it('does not try to send tab activation message', async () => { |
| 106 | + const pagePromise = Promise.resolve([mockPage]) |
| 107 | + |
| 108 | + ;(mockBrowser.pages as sinon.SinonStub).returns(pagePromise) |
| 109 | + |
| 110 | + const p = activateMainTab(mockBrowser as Browser) |
| 111 | + |
| 112 | + await pagePromise |
| 113 | + expect(window.postMessage).not.to.be.called |
| 114 | + expect(window.addEventListener).not.to.be.called |
| 115 | + await p |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments