Skip to content

Commit

Permalink
test: add tests for useReduxContext
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Sep 5, 2024
1 parent 968fe51 commit a136620
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/vue-redux/src/compositions/use-redux-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createReduxContextComposition(context = ContextKey) {

if (process.env.NODE_ENV !== 'production' && !contextValue) {
throw new Error(
'could not find react-redux context value; please ensure the component is wrapped in a <Provider>',
'could not find vue-redux context value; please ensure the component is wrapped in a <Provider>',
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/vue-redux/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './provider/context'
export * from './compositions/use-store'
export * from './compositions/use-dispatch'
export * from './compositions/use-selector'
export * from './compositions/use-redux-context'
2 changes: 1 addition & 1 deletion packages/vue-redux/src/provider/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export interface VueReduxContextValue<
subscription: Subscription
}

export const ContextKey = Symbol.for(`react-redux-context`) as InjectionKey<VueReduxContextValue>
export const ContextKey = Symbol.for(`vue-redux-context`) as InjectionKey<VueReduxContextValue>
48 changes: 48 additions & 0 deletions packages/vue-redux/tests/use-redux-context.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {describe, it, expect, vi} from "vitest";
import {
createReduxContextComposition,
useReduxContext,
} from '../src'
import {defineComponent, h} from "vue";
import type {InjectionKey} from "vue";
import {render} from "@testing-library/vue";

describe('Vue', () => {
describe('compositions', () => {
describe('useReduxContext', () => {
it('throws if component is not wrapped in provider', () => {
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})

const App = defineComponent(() => {
useReduxContext();
return () => null;
})

expect(() => render(<App/>)).toThrow(
/could not find vue-redux context value/,
)
spy.mockRestore()
})
})
describe('createReduxContextHook', () => {
it('throws if component is not wrapped in provider', () => {
const customContext = Symbol.for("testing") as InjectionKey<VueReduxContextValue | null>;
const useCustomReduxContext = createReduxContextComposition(customContext)
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})


const App = defineComponent(() => {
useCustomReduxContext();
return () => null;
})


expect(() => render(<App/>)).toThrow(
/could not find vue-redux context value/,
)

spy.mockRestore()
})
})
})
})

0 comments on commit a136620

Please sign in to comment.