Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 16, 2024
1 parent 1bad2f0 commit 95651e5
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions tests/unit/window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ global.MAIN_WINDOW_VITE_DEV_SERVER_URL = 'http://localhost:3000/'
global.MAIN_WINDOW_VITE_NAME = 'vite'

vi.mock('electron', async () => {
const BrowserWindow = vi.fn()
BrowserWindow.prototype.visible = true
BrowserWindow.prototype.destroyed = false
BrowserWindow.prototype.minimized = false
const BrowserWindow = vi.fn(function() {
this.visible = true
this.minimized = false
this.destroyed = false
})
BrowserWindow.prototype.show = vi.fn()
BrowserWindow.prototype.hide = vi.fn(() => this.visible = false)
BrowserWindow.prototype.close = vi.fn(() => this.destroyed = true)
BrowserWindow.prototype.hide = vi.fn(function() { this.visible = false })
BrowserWindow.prototype.close = vi.fn(function() { this.destroyed = true })
BrowserWindow.prototype.focus = vi.fn()
BrowserWindow.prototype.restore = vi.fn(() => this.minimized = false)
BrowserWindow.prototype.minimize = vi.fn(() => this.minimized = true)
BrowserWindow.prototype.isMinimized = vi.fn(() => this.minimized)
BrowserWindow.prototype.isVisible = vi.fn(() => this.visible)
BrowserWindow.prototype.isDestroyed = vi.fn(() => false)
BrowserWindow.prototype.restore = vi.fn(function() { this.minimized = false })
BrowserWindow.prototype.minimize = vi.fn(function() { this.minimized = true })
BrowserWindow.prototype.isMinimized = vi.fn(function() { return this.minimized })
BrowserWindow.prototype.isVisible = vi.fn(function() { return this.visible })
BrowserWindow.prototype.isDestroyed = vi.fn(function() { return false })
BrowserWindow.prototype.loadFile = vi.fn()
BrowserWindow.prototype.loadURL = vi.fn()
BrowserWindow.prototype.on = vi.fn()
Expand All @@ -32,14 +33,8 @@ vi.mock('electron', async () => {
const window2 = new BrowserWindow()
const window3 = new BrowserWindow()
const window4 = new BrowserWindow()
//window4.minimize()
for (const window of [window1, window2, window3, window4]) {
console.log(window.isVisible())
}
window3.hide()
for (const window of [window1, window2, window3, window4]) {
console.log(window.isVisible())
}
window3.visible = false
window4.minimized = true
return [window1, window2, window3, window4]
})
BrowserWindow.prototype.webContents = {
Expand All @@ -63,6 +58,14 @@ vi.mock('electron', async () => {
}
})

const expectCreateWebPreferences = (callParams) => {
expect(callParams.webPreferences.nodeIntegration).toBe(false)
expect(callParams.webPreferences.contextIsolation).toBe(true)
expect(callParams.webPreferences.webSecurity).toBe(false)
expect(callParams.webPreferences.defaultEncoding).toBe('UTF-8')
expect(callParams.webPreferences.sandbox).toBe(true)
}

beforeEach(async () => {
try { await window.closeMainWindow() } catch { /* empty */ }
try { await window.closeCommandPalette() } catch { /* empty */ }
Expand All @@ -82,6 +85,18 @@ test('Create main window', async () => {
await window.openMainWindow()
expect(window.mainWindow).toBeInstanceOf(BrowserWindow)
expect(BrowserWindow.prototype.loadURL).toHaveBeenCalledWith('http://localhost:3000/#')

})

test('Main window properties', async () => {
await window.openMainWindow()
const callParams = BrowserWindow.mock.calls[0][0]
expect(callParams.width).toBeDefined()
expect(callParams.height).toBeDefined()
expect(callParams.titleBarOverlay).toBeDefined()
expect(callParams.trafficLightPosition).toBeDefined()
expect(callParams.show).toBe(false)
expectCreateWebPreferences(callParams)
})

test('Two main windows are not created', async () => {
Expand Down Expand Up @@ -124,12 +139,16 @@ test('Create chat window', async () => {
const chatWindow = await window.openChatWindow({ promptId: '1'})
expect(chatWindow).toBeInstanceOf(BrowserWindow)
expect(BrowserWindow.prototype.loadURL).toHaveBeenCalledWith('http://localhost:3000/?promptId=1#')
const callParams = BrowserWindow.mock.calls[0][0]
expectCreateWebPreferences(callParams)
})

test('Create command palette window', async () => {
await window.openCommandPalette('1')
expect(window.commandPalette).toBeInstanceOf(BrowserWindow)
expect(BrowserWindow.prototype.loadURL).toHaveBeenCalledWith('http://localhost:3000/?textId=1#/command')
const callParams = BrowserWindow.mock.calls[0][0]
expectCreateWebPreferences(callParams)
})

test('Close command palette window', async () => {
Expand All @@ -142,6 +161,8 @@ test('Create prompt anywhere window', async () => {
await window.openPromptAnywhere()
expect(window.promptAnywhereWindow).toBeInstanceOf(BrowserWindow)
expect(BrowserWindow.prototype.loadURL).toHaveBeenCalledWith('http://localhost:3000/#/prompt')
const callParams = BrowserWindow.mock.calls[0][0]
expectCreateWebPreferences(callParams)
})

test('Resize prompt anywhere window', async () => {
Expand All @@ -160,6 +181,8 @@ test('Open waiting panel', async () => {
await window.openWaitingPanel()
expect(window.waitingPanel).toBeInstanceOf(BrowserWindow)
expect(BrowserWindow.prototype.loadURL).toHaveBeenCalledWith('http://localhost:3000/#/wait')
const callParams = BrowserWindow.mock.calls[0][0]
expectCreateWebPreferences(callParams)
})

test('Close waiting panel', async () => {
Expand All @@ -168,7 +191,13 @@ test('Close waiting panel', async () => {
expect(window.waitingPanel).toBeNull()
})

// test('Hides active windows', async () => {
// await window.hideWindows()
// expect(BrowserWindow.prototype.hide).toHaveBeenCalledTimes(2)
// })
test('Hides and restores active windows', async () => {
await window.restoreWindows()
expect(BrowserWindow.prototype.restore).toHaveBeenCalledTimes(0)
await window.hideWindows()
expect(BrowserWindow.prototype.hide).toHaveBeenCalledTimes(2)
await window.restoreWindows()
expect(BrowserWindow.prototype.restore).toHaveBeenCalledTimes(2)
await window.restoreWindows()
expect(BrowserWindow.prototype.restore).toHaveBeenCalledTimes(2)
})

0 comments on commit 95651e5

Please sign in to comment.