mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor(packages/vue): add app.provide() support for useContextFactory
This commit is contained in:
@@ -6,15 +6,14 @@ import { VueToolsError } from '../../utils';
|
|||||||
|
|
||||||
function testFactory<Data>(
|
function testFactory<Data>(
|
||||||
data: Data,
|
data: Data,
|
||||||
options?: { contextName?: string, fallback?: Data },
|
context: ReturnType<typeof useContextFactory<Data>>,
|
||||||
|
fallback?: Data,
|
||||||
) {
|
) {
|
||||||
const contextName = options?.contextName ?? 'TestContext';
|
const [inject, provide] = context;
|
||||||
|
|
||||||
const [inject, provide] = useContextFactory(contextName);
|
|
||||||
|
|
||||||
const Child = defineComponent({
|
const Child = defineComponent({
|
||||||
setup() {
|
setup() {
|
||||||
const value = inject(options?.fallback);
|
const value = inject(fallback);
|
||||||
return { value };
|
return { value };
|
||||||
},
|
},
|
||||||
template: `{{ value }}`,
|
template: `{{ value }}`,
|
||||||
@@ -38,7 +37,7 @@ function testFactory<Data>(
|
|||||||
|
|
||||||
describe('useContextFactory', () => {
|
describe('useContextFactory', () => {
|
||||||
it('provide and inject context correctly', () => {
|
it('provide and inject context correctly', () => {
|
||||||
const { Parent } = testFactory('test');
|
const { Parent } = testFactory('test', useContextFactory('TestContext'));
|
||||||
|
|
||||||
const component = mount(Parent);
|
const component = mount(Parent);
|
||||||
|
|
||||||
@@ -46,13 +45,13 @@ describe('useContextFactory', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('throw an error when context is not provided', () => {
|
it('throw an error when context is not provided', () => {
|
||||||
const { Child } = testFactory('test');
|
const { Child } = testFactory('test', useContextFactory('TestContext'));
|
||||||
|
|
||||||
expect(() => mount(Child)).toThrow(VueToolsError);
|
expect(() => mount(Child)).toThrow(VueToolsError);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('inject a fallback value when context is not provided', () => {
|
it('inject a fallback value when context is not provided', () => {
|
||||||
const { Child } = testFactory('test', { fallback: 'fallback' });
|
const { Child } = testFactory('test', useContextFactory('TestContext'), 'fallback');
|
||||||
|
|
||||||
const component = mount(Child);
|
const component = mount(Child);
|
||||||
|
|
||||||
@@ -60,10 +59,23 @@ describe('useContextFactory', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('correctly handle null values', () => {
|
it('correctly handle null values', () => {
|
||||||
const { Parent } = testFactory(null);
|
const { Parent } = testFactory(null, useContextFactory('TestContext'));
|
||||||
|
|
||||||
const component = mount(Parent);
|
const component = mount(Parent);
|
||||||
|
|
||||||
expect(component.text()).toBe('');
|
expect(component.text()).toBe('');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('provide context globally with app', () => {
|
||||||
|
const context = useContextFactory('TestContext');
|
||||||
|
const { Child } = testFactory(null, context);
|
||||||
|
|
||||||
|
const childComponent = mount(Child, {
|
||||||
|
global: {
|
||||||
|
plugins: [app => context[1]('test', app)],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(childComponent.text()).toBe('test');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { inject, provide, type InjectionKey } from 'vue';
|
import {inject, provide, type InjectionKey, type App} from 'vue';
|
||||||
import { VueToolsError } from '../..';
|
import { VueToolsError } from '../..';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,7 +12,23 @@ import { VueToolsError } from '../..';
|
|||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const [injectContext, provideContext] = useContextFactory('MyContext');
|
* const [injectContext, provideContext] = useContextFactory('MyContext');
|
||||||
*
|
*
|
||||||
|
* const context = provideContext('Hello World');
|
||||||
|
* const value = injectContext();
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const [injectContext, provideContext] = useContextFactory('MyContext');
|
||||||
|
*
|
||||||
|
* // In a plugin
|
||||||
|
* {
|
||||||
|
* install(app) {
|
||||||
|
* provideContext('Hello World', app);
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // In a component
|
||||||
|
* const value = injectContext();
|
||||||
|
*
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
export function useContextFactory<ContextValue>(name: string) {
|
export function useContextFactory<ContextValue>(name: string) {
|
||||||
@@ -27,10 +43,10 @@ export function useContextFactory<ContextValue>(name: string) {
|
|||||||
throw new VueToolsError(`useContextFactory: '${name}' context is not provided`);
|
throw new VueToolsError(`useContextFactory: '${name}' context is not provided`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const provideContext = (context: ContextValue) => {
|
const provideContext = (context: ContextValue, app?: App) => {
|
||||||
provide(injectionKey, context);
|
(app ? app.provide : provide)(injectionKey, context);
|
||||||
return context;
|
return context;
|
||||||
};
|
};
|
||||||
|
|
||||||
return [injectContext, provideContext] as const;
|
return [injectContext, provideContext] as const;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user