Skip to content

Commit

Permalink
Update dialogs.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed Mar 5, 2024
1 parent 89d9323 commit c3e6080
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions app-shell-odd/src/dialogs/__tests__/dialogs.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import Electron from 'electron'

import { describe, it, expect, vi } from 'vitest'
import * as Dialogs from '..'

jest.mock('electron')

const mockShowOpenDialog = Electron.dialog
.showOpenDialog as jest.MockedFunction<typeof Electron.dialog.showOpenDialog>
vi.mock('electron')

const mockMainWindow = ({
mainWindow: true,
Expand All @@ -14,76 +11,97 @@ const mockMainWindow = ({
describe('dialog boxes', () => {
describe('showOpenDirectoryDialog', () => {
it('directory select with cancel', () => {
mockShowOpenDialog.mockResolvedValue({ canceled: true, filePaths: [] })
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: true,
filePaths: [],
})

return Dialogs.showOpenDirectoryDialog(mockMainWindow).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openDirectory', 'createDirectory'],
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openDirectory', 'createDirectory'],
}
)
expect(filePaths).toEqual([])
})
})

it('directory select with files', () => {
mockShowOpenDialog.mockResolvedValue({
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: false,
filePaths: ['/path/to/dir'],
})

return Dialogs.showOpenDirectoryDialog(mockMainWindow).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openDirectory', 'createDirectory'],
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openDirectory', 'createDirectory'],
}
)
expect(filePaths).toEqual(['/path/to/dir'])
})
})

it('directory select with default location', () => {
mockShowOpenDialog.mockResolvedValue({
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: false,
filePaths: ['/path/to/dir'],
})

return Dialogs.showOpenDirectoryDialog(mockMainWindow, {
defaultPath: '/foo',
}).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openDirectory', 'createDirectory'],
defaultPath: '/foo',
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openDirectory', 'createDirectory'],
defaultPath: '/foo',
}
)
expect(filePaths).toEqual(['/path/to/dir'])
})
})
})

describe('showOpenFileDialog', () => {
it('file select with cancel', () => {
mockShowOpenDialog.mockResolvedValue({ canceled: true, filePaths: [] })
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: true,
filePaths: [],
})

return Dialogs.showOpenFileDialog(mockMainWindow).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openFile'],
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openFile'],
}
)
expect(filePaths).toEqual([])
})
})

it('file select with files', () => {
mockShowOpenDialog.mockResolvedValue({
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: false,
filePaths: ['/path/to/file.json'],
})

return Dialogs.showOpenFileDialog(mockMainWindow).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openFile'],
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openFile'],
}
)
expect(filePaths).toEqual(['/path/to/file.json'])
})
})

it('file select with filters', () => {
mockShowOpenDialog.mockResolvedValue({
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: false,
filePaths: ['/path/to/file.json'],
})
Expand All @@ -92,7 +110,9 @@ describe('dialog boxes', () => {

return Dialogs.showOpenFileDialog(mockMainWindow, options).then(
filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
expect(
vi.mocked(Electron.dialog.showOpenDialog)
).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openFile'],
filters: [{ name: 'JSON', extensions: ['json'] }],
})
Expand All @@ -102,18 +122,21 @@ describe('dialog boxes', () => {
})

it('file select with default location', () => {
mockShowOpenDialog.mockResolvedValue({
vi.mocked(Electron.dialog.showOpenDialog).mockResolvedValue({
canceled: false,
filePaths: ['/path/to/file.json'],
})

return Dialogs.showOpenFileDialog(mockMainWindow, {
defaultPath: '/foo',
}).then(filePaths => {
expect(mockShowOpenDialog).toHaveBeenCalledWith(mockMainWindow, {
properties: ['openFile'],
defaultPath: '/foo',
})
expect(vi.mocked(Electron.dialog.showOpenDialog)).toHaveBeenCalledWith(
mockMainWindow,
{
properties: ['openFile'],
defaultPath: '/foo',
}
)
expect(filePaths).toEqual(['/path/to/file.json'])
})
})
Expand Down

0 comments on commit c3e6080

Please sign in to comment.