Skip to content

Commit 6f31cf6

Browse files
committed
test(context): provideStore + injectStore
1 parent fffb146 commit 6f31cf6

File tree

6 files changed

+373
-48
lines changed

6 files changed

+373
-48
lines changed

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33
"version": "0.3.1",
44
"author": "Nicolai Moraru <[email protected]>",
55
"license": "MIT",
6+
"type": "module",
67
"scripts": {
78
"dev:todolist-ts-example": "cd packages/examples/todolist-ts && pnpm run dev",
89
"dev:vue-tinybase": "cd packages/public/vue-tinybase && pnpm run dev",
910
"dev": "run-p dev:*",
10-
"docs:build": "cd packages/private/docs && pnpm run build"
11+
"docs:build": "cd packages/private/docs && pnpm run build",
12+
"test": "vitest",
13+
"coverage": "vitest run --coverage"
1114
},
1215
"devDependencies": {
16+
"@vitest/coverage-v8": "^1.6.0",
17+
"@vue/test-utils": "^2.4.6",
18+
"happy-dom": "^14.12.3",
1319
"npm-run-all2": "^6.2.2",
20+
"tinybase": "^5.0.0",
1421
"vite": "^5.3.3",
15-
"vitest": "^1.6.0"
22+
"vitest": "^1.6.0",
23+
"vue": "^3.4.31"
1624
}
1725
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { InjectionKey, defineComponent, h } from 'vue'
2+
import { expect, test } from 'vitest'
3+
import { createStore } from 'tinybase'
4+
import { mount } from '@vue/test-utils'
5+
import { provideStore } from './provideStore.js'
6+
import { injectStore } from './injectStore.js'
7+
8+
test('[default-store/context] (provideStore + injectStore) happy-path', () => {
9+
const store = createStore().setValue('foo', 'bar')
10+
11+
const ChildComponent = defineComponent({
12+
setup() {
13+
const store = injectStore()
14+
15+
const value = store.getValue('foo')
16+
17+
return () => h('div', null, `${value}`)
18+
},
19+
})
20+
21+
const ParentComponent = defineComponent({
22+
setup() {
23+
provideStore(store as any)
24+
25+
return () => h(ChildComponent)
26+
},
27+
})
28+
29+
const wrapper = mount(ParentComponent)
30+
31+
expect(wrapper.text()).toBe('bar')
32+
})
33+
34+
test('[default-store/context] (injectStore) missing store in context', () => {
35+
const ChildComponent = defineComponent({
36+
setup() {
37+
injectStore()
38+
return () => h('div')
39+
},
40+
})
41+
42+
const fn = () => mount(ChildComponent)
43+
44+
expect(fn).toThrowError('[tinybase-vue] (injectStore): Could not find store with key Symbol(DefaultStoreKey)')
45+
})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { InjectionKey, defineComponent, h } from 'vue'
2+
import { expect, test } from 'vitest'
3+
import { createStore } from 'tinybase'
4+
import { mount } from '@vue/test-utils'
5+
import { provideStore } from './provideStore.js'
6+
import { injectStore } from './injectStore.js'
7+
8+
test('[custom-store/context] (provideStore + injectStore) happy-path', () => {
9+
const store1 = createStore().setValue('foo', 'bar')
10+
const store2 = createStore().setValue('foo', 'baz')
11+
const store1Key = Symbol('store1') as InjectionKey<typeof store1>
12+
const store2Key = Symbol('store2') as InjectionKey<typeof store2>
13+
14+
const ChildComponent = defineComponent({
15+
setup() {
16+
const store1 = injectStore(store1Key)
17+
const store2 = injectStore(store2Key)
18+
19+
const value1 = store1.getValue('foo')
20+
const value2 = store2.getValue('foo')
21+
22+
return () => h('div', null, `${value1} ${value2}`)
23+
},
24+
})
25+
26+
const ParentComponent = defineComponent({
27+
setup() {
28+
provideStore(store1Key, store1)
29+
provideStore(store2Key, store2)
30+
31+
return () => h(ChildComponent)
32+
},
33+
})
34+
35+
const wrapper = mount(ParentComponent)
36+
37+
expect(wrapper.text()).toBe('bar baz')
38+
})
39+
40+
test('[custom-store/context] (injectStore) missing store in context', () => {
41+
const storeKey = Symbol('store1')
42+
43+
const ChildComponent = defineComponent({
44+
setup() {
45+
injectStore(storeKey)
46+
return () => h('div')
47+
},
48+
})
49+
50+
const fn = () => mount(ChildComponent)
51+
52+
expect(fn).toThrowError('[tinybase-vue] (injectStore): Could not find store with key Symbol(store1)')
53+
})

packages/public/vue-tinybase/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@tsconfig/node20/tsconfig.json",
33
"include": ["src/**/*"],
4+
"exclude": ["src/**/*.test.ts"],
45
"compilerOptions": {
56
"composite": true,
67
"rootDir": "src",

0 commit comments

Comments
 (0)