-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' | ||
import { mount } from '@vue/test-utils' | ||
import { createPinia } from 'pinia' | ||
import { defineComponent } from 'vue' | ||
import { GlobalMountOptions } from 'node_modules/@vue/test-utils/dist/types' | ||
import { UseMutationOptions, useMutation } from './use-mutation' | ||
import { delay, runTimers } from '../test/utils' | ||
|
||
describe('useMutation', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
function mountSimple<TResult = number, TParams extends any[] = []>( | ||
options: Partial<UseMutationOptions<TResult, TParams>> = {}, | ||
mountOptions?: GlobalMountOptions | ||
) { | ||
const mutator = options.mutator | ||
? vi.fn(options.mutator) | ||
: vi.fn(async () => { | ||
await delay(0) | ||
return 42 | ||
}) | ||
const wrapper = mount( | ||
defineComponent({ | ||
render: () => null, | ||
setup() { | ||
return { | ||
...useMutation<TResult>({ | ||
...options, | ||
// @ts-expect-error: generic unmatched but types work | ||
mutator, | ||
}), | ||
} | ||
}, | ||
}), | ||
{ | ||
global: { | ||
plugins: [createPinia()], | ||
...mountOptions, | ||
}, | ||
} | ||
) | ||
return Object.assign([wrapper, mutator] as const, { wrapper, mutator }) | ||
} | ||
it('invokes the mutator', async () => { | ||
const { wrapper } = mountSimple() | ||
|
||
wrapper.vm.mutate() | ||
await runTimers() | ||
|
||
expect(wrapper.vm.data).toBe(42) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { vi } from 'vitest' | ||
import { nextTick } from 'vue' | ||
|
||
export const delay = (ms: number) => new Promise((r) => setTimeout(r, ms)) | ||
|
||
export const runTimers = async (onlyPending = true) => { | ||
if (onlyPending) { | ||
await vi.runOnlyPendingTimersAsync() | ||
} else { | ||
// vi.runAllTimers() | ||
await vi.runAllTimersAsync() | ||
} | ||
await nextTick() | ||
} |