Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Dec 23, 2024
1 parent 7d72b08 commit 358a2f4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/unit/readaloud.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

import { vi, beforeEach, expect, test } from 'vitest'
import { Notification } from 'electron'
import ReadAloud from '../../src/automations/readaloud'
import * as window from '../../src/main/window'

// mock electron
vi.mock('electron', async() => {
const Notification = vi.fn();
Notification.prototype.show = vi.fn();
return {
Notification
}
})

// mock windows
vi.mock('../../src/main/window.ts', async () => {
return {
hideWindows: vi.fn(),
restoreWindows: vi.fn(),
releaseFocus: vi.fn(),
openReadAloudPalette: vi.fn(),
isMainWindowFocused: vi.fn(() => false),
}
})

vi.mock('../../src/main/utils', async () => {
return {
wait: vi.fn(),
putCachedText: vi.fn(() => 'textId')
}
})

// mock automator
vi.mock('../../src/automations/automator.ts', async () => {
let call = -1
const Automator = vi.fn()
Automator.prototype.getSelectedText = vi.fn(() => {
if (++call === 0) return 'Grabbed text'
else if (call === 1) return ''
else return null
})
return { default: Automator }
})

beforeEach(() => {
vi.clearAllMocks()
})

test('Open readaloud window', async () => {
await ReadAloud.read()
expect(window.hideWindows).toHaveBeenCalled()
expect(window.releaseFocus).toHaveBeenCalled()
expect(window.openReadAloudPalette).toHaveBeenCalledWith('textId')
})

test('Show no text error notification', async () => {
await ReadAloud.read()
expect(window.hideWindows).toHaveBeenCalled()
expect(window.releaseFocus).toHaveBeenCalled()
expect(Notification).toHaveBeenCalledWith({ title: 'Witsy', body: 'Please highlight the text you want to read aloud' })
})

test('Show no grab error notification', async () => {
await ReadAloud.read()
expect(window.hideWindows).toHaveBeenCalled()
expect(window.releaseFocus).toHaveBeenCalled()
expect(Notification).toHaveBeenCalledWith({ title: 'Witsy', body: 'An error occurred while trying to grab the text. Please check Privacy & Security settings.' })
})
43 changes: 43 additions & 0 deletions tests/unit/transcriber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import { vi, beforeEach, expect, test } from 'vitest'
import Transcriber from '../../src/automations/transcriber'
import Automator from '../../src/automations/automator'
import * as window from '../../src/main/window'

// mock windows
vi.mock('../../src/main/window.ts', async () => {
return {
hideWindows: vi.fn(),
restoreWindows: vi.fn(),
releaseFocus: vi.fn(),
openTranscribePalette: vi.fn(),
closeTranscribePalette: vi.fn(),
isMainWindowFocused: vi.fn(() => false),
}
})

// mock automator
vi.mock('../../src/automations/automator.ts', async () => {
const Automator = vi.fn()
Automator.prototype.pasteText = vi.fn()
return { default: Automator }
})


beforeEach(() => {
vi.clearAllMocks()
})

test('Open transcriber window', async () => {
await Transcriber.initTranscription()
expect(window.hideWindows).toHaveBeenCalled()
expect(window.releaseFocus).toHaveBeenCalled()
expect(window.openTranscribePalette).toHaveBeenCalledWith()
})

test('Insert transcription', async () => {
await Transcriber.insertTranscription('Hello, World!')
expect(window.closeTranscribePalette).toHaveBeenCalled()
expect(window.releaseFocus).toHaveBeenCalled()
expect(Automator.prototype.pasteText).toHaveBeenCalledWith('Hello, World!')
})
2 changes: 2 additions & 0 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default defineConfig({
'*.config.ts',
'build/*',
'src/*.ts',
'src/automations/computer*.ts',
'src/automations/macos*.ts',
'src/automations/nut*.ts',
'src/automations/robot.ts',
'src/automations/windows.ts',
'src/services/*worker.ts',
Expand Down

0 comments on commit 358a2f4

Please sign in to comment.