|
| 1 | +import { describe, expect, jest, it } from '@jest/globals' |
1 | 2 | import type { DataContext } from '../../../src'
|
2 | 3 | import { AuthActions } from '../../../src/actions/AuthActions'
|
3 | 4 | import { createTestDataContext } from '../helper'
|
4 |
| -import sinon, { SinonStub } from 'sinon' |
5 |
| -import sinonChai from 'sinon-chai' |
6 |
| -import chai, { expect } from 'chai' |
7 | 5 | import { FoundBrowser } from '@packages/types'
|
8 | 6 |
|
9 |
| -chai.use(sinonChai) |
10 |
| - |
11 | 7 | describe('AuthActions', () => {
|
12 |
| - context('.login', () => { |
| 8 | + describe('.login', () => { |
13 | 9 | let ctx: DataContext
|
14 | 10 | let actions: AuthActions
|
15 | 11 |
|
16 | 12 | beforeEach(() => {
|
17 |
| - sinon.restore() |
18 |
| - |
19 | 13 | ctx = createTestDataContext('open')
|
20 |
| - |
21 |
| - ;(ctx._apis.authApi.logIn as SinonStub) |
22 |
| - .resolves({ name: 'steve', email: '[email protected]', authToken: 'foo' }) |
| 14 | + jest.mocked(ctx._apis.authApi.logIn).mockResolvedValue({ name: 'steve', email: '[email protected]', authToken: 'foo' }) |
23 | 15 |
|
24 | 16 | actions = new AuthActions(ctx)
|
25 | 17 | })
|
26 | 18 |
|
27 | 19 | it('sets coreData.user', async () => {
|
| 20 | + // @ts-expect-error - incorrect number of arguments |
28 | 21 | await actions.login()
|
29 |
| - expect(ctx.coreData.user).to.include({ name: 'steve', email: '[email protected]', authToken: 'foo' }) |
| 22 | + expect(ctx.coreData.user).toEqual(expect.objectContaining({ name: 'steve', email: '[email protected]', authToken: 'foo' })) |
30 | 23 | })
|
31 | 24 |
|
32 | 25 | it('focuses the main window if there is no activeBrowser', async () => {
|
33 | 26 | ctx.coreData.activeBrowser = null
|
34 | 27 |
|
| 28 | + // @ts-expect-error - incorrect number of arguments |
35 | 29 | await actions.login()
|
36 | 30 |
|
37 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce |
38 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called |
| 31 | + expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1) |
| 32 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled() |
39 | 33 | })
|
40 | 34 |
|
41 | 35 | it('focuses the main window if the activeBrowser does not support focus', async () => {
|
42 | 36 | const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
|
43 | 37 |
|
44 |
| - sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(false) |
| 38 | + jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => { |
| 39 | + if (args === browser) { |
| 40 | + return Promise.resolve(false) |
| 41 | + } |
| 42 | + |
| 43 | + return Promise.resolve(true) |
| 44 | + }) |
45 | 45 |
|
| 46 | + // @ts-expect-error - incorrect number of arguments |
46 | 47 | await actions.login()
|
47 | 48 |
|
48 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce |
49 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called |
| 49 | + expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1) |
| 50 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled() |
50 | 51 | })
|
51 | 52 |
|
52 | 53 | it('focuses the main window if the activeBrowser supports focus, but browser is closed', async () => {
|
53 | 54 | const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
|
54 | 55 |
|
55 | 56 | ctx.coreData.app.browserStatus = 'closed'
|
56 | 57 |
|
57 |
| - sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true) |
| 58 | + jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => { |
| 59 | + if (args === browser) { |
| 60 | + return Promise.resolve(true) |
| 61 | + } |
58 | 62 |
|
| 63 | + return Promise.resolve(false) |
| 64 | + }) |
| 65 | + |
| 66 | + // @ts-expect-error - incorrect number of arguments |
59 | 67 | await actions.login()
|
60 | 68 |
|
61 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce |
62 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called |
| 69 | + expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1) |
| 70 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled() |
63 | 71 | })
|
64 | 72 |
|
65 | 73 | it('focuses the main window if the activeBrowser supports focus, but browser is opening', async () => {
|
66 | 74 | const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
|
67 | 75 |
|
68 | 76 | ctx.coreData.app.browserStatus = 'opening'
|
69 | 77 |
|
70 |
| - sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true) |
| 78 | + jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => { |
| 79 | + if (args === browser) { |
| 80 | + return Promise.resolve(true) |
| 81 | + } |
| 82 | + |
| 83 | + return Promise.resolve(false) |
| 84 | + }) |
71 | 85 |
|
| 86 | + // @ts-expect-error - incorrect number of arguments |
72 | 87 | await actions.login()
|
73 | 88 |
|
74 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.be.calledOnce |
75 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called |
| 89 | + expect(ctx._apis.electronApi.focusMainWindow).toHaveBeenCalledTimes(1) |
| 90 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled() |
76 | 91 | })
|
77 | 92 |
|
78 | 93 | it('focuses the browser if the activeBrowser does support focus and browser is open', async () => {
|
79 | 94 | const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
|
80 | 95 |
|
81 | 96 | ctx.coreData.app.browserStatus = 'open'
|
82 | 97 |
|
83 |
| - sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true) |
| 98 | + jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => { |
| 99 | + if (args === browser) { |
| 100 | + return Promise.resolve(true) |
| 101 | + } |
84 | 102 |
|
| 103 | + return Promise.resolve(false) |
| 104 | + }) |
| 105 | + |
| 106 | + // @ts-expect-error - incorrect number of arguments |
85 | 107 | await actions.login()
|
86 | 108 |
|
87 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.not.be.called |
88 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.be.calledOnce |
| 109 | + expect(ctx._apis.electronApi.focusMainWindow).not.toHaveBeenCalled() |
| 110 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).toHaveBeenCalledTimes(1) |
89 | 111 | })
|
90 | 112 |
|
91 | 113 | it('does not focus anything if the activeBrowser does support focus but the main window is focused', async () => {
|
92 | 114 | const browser = ctx.coreData.activeBrowser = { name: 'foo' } as FoundBrowser
|
93 | 115 |
|
94 |
| - sinon.stub(ctx.browser, 'isFocusSupported').withArgs(browser).resolves(true) |
| 116 | + jest.spyOn(ctx.browser, 'isFocusSupported').mockImplementation((args) => { |
| 117 | + if (args === browser) { |
| 118 | + return Promise.resolve(true) |
| 119 | + } |
| 120 | + |
| 121 | + return Promise.resolve(false) |
| 122 | + }) |
95 | 123 |
|
96 |
| - ;(ctx._apis.electronApi.isMainWindowFocused as SinonStub).returns(true) |
| 124 | + jest.spyOn(ctx._apis.electronApi, 'isMainWindowFocused').mockReturnValue(true) |
97 | 125 |
|
| 126 | + // @ts-expect-error - incorrect number of arguments |
98 | 127 | await actions.login()
|
99 | 128 |
|
100 |
| - expect(ctx._apis.electronApi.focusMainWindow).to.not.be.called |
101 |
| - expect(ctx._apis.browserApi.focusActiveBrowserWindow).to.not.be.called |
| 129 | + expect(ctx._apis.electronApi.focusMainWindow).not.toHaveBeenCalled() |
| 130 | + expect(ctx._apis.browserApi.focusActiveBrowserWindow).not.toHaveBeenCalled() |
102 | 131 | })
|
103 | 132 | })
|
104 | 133 | })
|
0 commit comments