Skip to content

Commit 968fe51

Browse files
committed
test: add test for useDispatch's context passing
1 parent f9c07f6 commit 968fe51

File tree

3 files changed

+61
-66
lines changed

3 files changed

+61
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {describe, it, expect} from "vitest";
2+
import {defineComponent, h, InjectionKey} from 'vue'
3+
import {
4+
createDispatchComposition,
5+
provideStore as provideMock,
6+
useDispatch, VueReduxContextValue,
7+
} from '../src'
8+
import { createStore } from 'redux'
9+
import {render} from "@testing-library/vue";
10+
11+
const store = createStore((c: number = 1): number => c + 1)
12+
const store2 = createStore((c: number = 1): number => c + 2)
13+
14+
describe('Vue', () => {
15+
describe('compositions', () => {
16+
describe('useDispatch', () => {
17+
it("returns the store's dispatch function", () => {
18+
const Comp = defineComponent(() => {
19+
const dispatch = useDispatch();
20+
expect(dispatch).toBe(store.dispatch)
21+
22+
return () => null;
23+
})
24+
25+
const App = defineComponent(() => {
26+
provideMock({store});
27+
return () => <Comp/>
28+
})
29+
30+
render(<App/>)
31+
})
32+
})
33+
describe('createDispatchComposition', () => {
34+
it("returns the correct store's dispatch function", () => {
35+
const nestedContext = Symbol.for("mock-redux-store") as InjectionKey<VueReduxContextValue | null>
36+
const useCustomDispatch = createDispatchComposition(nestedContext)
37+
38+
const CheckDispatch = defineComponent(() => {
39+
const dispatch = useDispatch();
40+
const customDispatch = useCustomDispatch();
41+
expect(dispatch).toBe(store.dispatch)
42+
expect(customDispatch).toBe(store2.dispatch)
43+
44+
return () => null;
45+
})
46+
47+
const InnerApp = defineComponent(() => {
48+
provideMock({store: store2, context: nestedContext});
49+
return () => <CheckDispatch/>
50+
})
51+
52+
const App = defineComponent(() => {
53+
provideMock({store});
54+
return () => <InnerApp/>
55+
})
56+
57+
render(<App/>)
58+
})
59+
})
60+
})
61+
})

packages/vue-redux/tests/useDispatch.spec.tsx

-66
This file was deleted.

0 commit comments

Comments
 (0)