Skip to content

Commit

Permalink
Updates test to include throwing an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
cimigree committed Jan 8, 2025
1 parent 768cff9 commit 924c8f7
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/renderer/src/hooks/useConfigFileImporter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ describe('useSelectProjectConfigFile', () => {
}
})

function createWrapper() {
const queryClient = new QueryClient()
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}

it('returns filename and filepath from window.runtime.selectFile', async () => {
vi.spyOn(window.runtime, 'selectFile').mockResolvedValue({
name: 'myFile.comapeocat',
path: '/Users/cindy/documents/myFile.comapeocat',
})

const queryClient = new QueryClient()

const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)

const { result } = renderHook(() => useSelectProjectConfigFile(), {
wrapper,
wrapper: createWrapper(),
})

await act(async () => {
Expand All @@ -41,19 +42,29 @@ describe('useSelectProjectConfigFile', () => {
it('returns undefined if user cancels', async () => {
vi.spyOn(window.runtime, 'selectFile').mockResolvedValue(undefined)

const queryClient = new QueryClient()

const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)

const { result } = renderHook(() => useSelectProjectConfigFile(), {
wrapper,
wrapper: createWrapper(),
})

await act(async () => {
const val = await result.current.mutateAsync(undefined)
expect(val).toBeUndefined()
})
})

it('throws if the returned object has invalid shape', async () => {
vi.spyOn(window.runtime, 'selectFile').mockRejectedValue(
new Error('Value has invalid shape'),
)

const { result } = renderHook(() => useSelectProjectConfigFile(), {
wrapper: createWrapper(),
})

await expect(
act(async () => {
await result.current.mutateAsync()
}),
).rejects.toThrow('Value has invalid shape')
})
})

0 comments on commit 924c8f7

Please sign in to comment.