-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add more test provide * chore: test * fix: test * fix: lint
- Loading branch information
1 parent
61f20e5
commit 9e5bb91
Showing
4 changed files
with
247 additions
and
68 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
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,233 @@ | ||
import type { Component } from 'vue' | ||
import { defineComponent, h } from 'vue' | ||
import { describe, expect, test, vitest } from 'vitest' | ||
import { mount } from '@vue/test-utils' | ||
import { ScopePropObject, createProvide, createProvideScope } from '.' | ||
|
||
describe('Provide', () => { | ||
test('createProvide consumerName emty test', async () => { | ||
const spy = vitest.spyOn(global.console, 'warn').mockImplementation(() => { }) | ||
|
||
const [_AvatarProvider, useAvatarInject] = createProvide('Avatar') | ||
|
||
const demoComponent = { | ||
setup(props, { attrs, slots }) { | ||
useAvatarInject('AvatarFallback') | ||
|
||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
} as Component | ||
|
||
const component = () => mount(demoComponent) | ||
expect(() => component()).toThrowError(new Error('`AvatarFallback` must be used within `Avatar`')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy.mock.calls[0][0]).toContain('[Vue warn]: injection "Symbol(Avatar)" not found.') | ||
}) | ||
|
||
test('createProvide consumerName emty test', async () => { | ||
const spy = vitest.spyOn(global.console, 'warn').mockImplementation(() => { }) | ||
|
||
const [_AvatarProvider, useAvatarInject] = createProvide('Avatar') | ||
|
||
const demoComponent = { | ||
setup(props, { attrs, slots }) { | ||
useAvatarInject('AvatarFallback') | ||
|
||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
} as Component | ||
|
||
const component = () => mount(demoComponent) | ||
expect(() => component()).toThrowError(new Error('`AvatarFallback` must be used within `Avatar`')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy.mock.calls[0][0]).toContain('[Vue warn]: injection "Symbol(Avatar)" not found.') | ||
}) | ||
|
||
test('createProvide get inject data', async () => { | ||
const spy = vitest.spyOn(global.console, 'warn').mockImplementation(() => { }) | ||
|
||
const [_avatarProvider, useAvatarInject] = createProvide<{ | ||
test: string | ||
}>('Avatar') | ||
|
||
const AvatarFallback = { | ||
setup(props, { attrs, slots }) { | ||
useAvatarInject('AvatarFallback') | ||
|
||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
} as Component | ||
|
||
const component = () => mount(AvatarFallback) | ||
expect(() => component()).toThrowError(new Error('`AvatarFallback` must be used within `Avatar`')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy.mock.calls[0][0]).toContain('[Vue warn]: injection "Symbol(Avatar)" not found.') | ||
}) | ||
|
||
test('createProvide provider', async () => { | ||
const [avatarProvider, useAvatarInject] = createProvide<{ | ||
testValue: string | ||
}>('Avatar') | ||
|
||
const Avatar = defineComponent({ | ||
setup(props, { attrs, slots }) { | ||
avatarProvider({ | ||
testValue: 'Merhaba', | ||
}) | ||
|
||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
}) | ||
|
||
const AvatarFallback = { | ||
components: { | ||
Avatar, | ||
}, | ||
setup() { | ||
const inject = useAvatarInject('AvatarFallback') | ||
|
||
return () => (inject as any).testValue | ||
}, | ||
} as Component | ||
|
||
const testComponent = defineComponent({ | ||
components: { | ||
AvatarFallback, | ||
Avatar, | ||
}, | ||
setup(props, { attrs }) { | ||
return () => h(Avatar, { ...attrs }, [h(AvatarFallback)], | ||
) | ||
}, | ||
}) | ||
|
||
const component = mount(testComponent) | ||
expect(component.html()).toContain('<div>Merhaba</div>') | ||
}) | ||
|
||
test('createProvide defaultProvide', async () => { | ||
const [_avatarProvider, useAvatarInject] = createProvide<{ | ||
testValue: string | ||
}>('Avatar', { | ||
testValue: 'Merhaba asdasda', | ||
}) | ||
|
||
const Avatar = defineComponent({ | ||
setup(props, { attrs, slots }) { | ||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
}) | ||
|
||
const AvatarFallback = { | ||
components: { | ||
Avatar, | ||
}, | ||
setup() { | ||
const inject = useAvatarInject('AvatarFallback') | ||
|
||
return () => (inject as any).testValue | ||
}, | ||
} as Component | ||
|
||
const testComponent = defineComponent({ | ||
components: { | ||
AvatarFallback, | ||
Avatar, | ||
}, | ||
setup(props, { attrs }) { | ||
return () => h(Avatar, { ...attrs }, [h(AvatarFallback)], | ||
) | ||
}, | ||
}) | ||
|
||
const component = mount(testComponent) | ||
expect(component.html()).toContain('<div>Merhaba asdasda</div>') | ||
}) | ||
|
||
test('createProvideScope', async () => { | ||
const AVATAR_NAME = 'OkuAvatar' | ||
const [createAvatarProvide, _createAvatarScope] = createProvideScope(AVATAR_NAME) | ||
|
||
type AvatarProvideValue = { | ||
imageLoadingStatus: 'loading' | 'loaded' | 'error' | ||
} | ||
|
||
const [avatarProvider, useAvatarInject] = createAvatarProvide<AvatarProvideValue>(AVATAR_NAME) | ||
|
||
const Avatar = defineComponent({ | ||
props: { | ||
scopeOkuAvatar: { | ||
...ScopePropObject, | ||
}, | ||
}, | ||
setup(props, { attrs, slots }) { | ||
avatarProvider({ | ||
imageLoadingStatus: 'loading', | ||
scope: props.scopeOkuAvatar, | ||
}) | ||
return () => h('div', { ...attrs }, slots) | ||
}, | ||
}) | ||
|
||
const AvatarFallback = { | ||
components: { | ||
Avatar, | ||
}, | ||
props: { | ||
scopeOkuAvatar: { | ||
...ScopePropObject, | ||
}, | ||
}, | ||
setup(props) { | ||
const inject = useAvatarInject('AvatarFallback', props.scopeOkuAvatar) | ||
|
||
return () => (inject as AvatarProvideValue).imageLoadingStatus | ||
}, | ||
} as Component | ||
|
||
const testComponent = defineComponent({ | ||
components: { | ||
AvatarFallback, | ||
Avatar, | ||
}, | ||
setup(props, { attrs }) { | ||
return () => h(Avatar, { ...attrs }, [h(AvatarFallback)], | ||
) | ||
}, | ||
}) | ||
|
||
const component = mount(testComponent) | ||
expect(component.html()).toContain('<div>loading</div>') | ||
}) | ||
|
||
test('createProvideScope createScope empty', async () => { | ||
const [_createCollectionProvide, createCollectionScope] = createProvideScope('TestCollectionProvider') | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(createCollectionScope()()).toEqual({ | ||
scopeTestCollectionProvider: { TestCollectionProvider: [] }, | ||
}) | ||
}) | ||
|
||
test('createProvideScope createScope component', async () => { | ||
// Collection | ||
const PROVIDER_NAME = 'TestCollectionProvider' | ||
|
||
const [createCollectionProvide, createCollectionScope] = createProvideScope(PROVIDER_NAME) | ||
|
||
const [_collectionProvide, _useCollectionInject] = createCollectionProvide<{ | ||
collectionRef: 'test' | ||
}>( | ||
PROVIDER_NAME, | ||
) | ||
|
||
const useRovingFocusGroupScope = createCollectionScope() | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(useRovingFocusGroupScope().scopeTestCollectionProvider.TestCollectionProvider).toBeDefined() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.