1
0
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:
2024-11-26 15:56:25 +07:00
parent 3e2b88d871
commit 1823771b4a
2 changed files with 42 additions and 14 deletions

View File

@@ -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');
});
}); });

View File

@@ -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 '../..';
/** /**
@@ -13,6 +13,22 @@ 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,8 +43,8 @@ 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;
}; };