Skip to content

Commit ffc3815

Browse files
committed
test(mock): add a mock store to base tests on
1 parent d95bdee commit ffc3815

File tree

7 files changed

+218
-32
lines changed

7 files changed

+218
-32
lines changed

packages/public/vue-tinybase/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"scripts": {
2727
"lint:path": "eslint --ext .ts --fix",
2828
"lint": "pnpm run lint:path .",
29-
"dev": "pkgroll --tsconfig=tsconfig.package.json --watch",
30-
"build": "pkgroll --tsconfig=tsconfig.package.json"
29+
"dev": "pkgroll --tsconfig=tsconfig.lib.json --watch",
30+
"build": "pkgroll --tsconfig=tsconfig.lib.json"
3131
},
3232
"author": "Nicolai Moraru <[email protected]>",
3333
"license": "MIT",

packages/public/vue-tinybase/src/context/context.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,30 @@ import { createStore } from 'tinybase'
44
import { mount } from '@vue/test-utils'
55
import { provideStore } from './provideStore.js'
66
import { injectStore } from './injectStore.js'
7+
import { store } from '../test-store/store.js'
78

89
test('[default-store/context] (provideStore + injectStore) happy-path', () => {
9-
const store = createStore().setValue('foo', 'bar')
10-
1110
const ChildComponent = defineComponent({
1211
setup() {
1312
const store = injectStore()
1413

15-
const value = store.getValue('foo')
14+
const value = store.getValue('theme')
1615

1716
return () => h('div', null, `${value}`)
1817
},
1918
})
2019

2120
const ParentComponent = defineComponent({
2221
setup() {
23-
provideStore(store as any)
22+
provideStore(store)
2423

2524
return () => h(ChildComponent)
2625
},
2726
})
2827

2928
const wrapper = mount(ParentComponent)
3029

31-
expect(wrapper.text()).toBe('bar')
30+
expect(wrapper.text()).toBe('light')
3231
})
3332

3433
test('[default-store/context] (injectStore) missing store in context', () => {
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,47 @@
11
import { InjectionKey, defineComponent, h } from 'vue'
22
import { expect, test } from 'vitest'
3-
import { createStore } from 'tinybase'
43
import { mount } from '@vue/test-utils'
54
import { provideStore } from './provideStore.js'
65
import { injectStore } from './injectStore.js'
6+
import { store, Store } from '../../test-store/store.js'
77

88
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>
9+
const StoreKey = Symbol('InjectedStore') as InjectionKey<Store>
1310

1411
const ChildComponent = defineComponent({
1512
setup() {
16-
const store1 = injectStore(store1Key)
17-
const store2 = injectStore(store2Key)
13+
const store = injectStore(StoreKey)
1814

19-
const value1 = store1.getValue('foo')
20-
const value2 = store2.getValue('foo')
15+
const theme = store.getValue('theme')
2116

22-
return () => h('div', null, `${value1} ${value2}`)
17+
return () => h('div', null, `${theme}`)
2318
},
2419
})
2520

2621
const ParentComponent = defineComponent({
2722
setup() {
28-
provideStore(store1Key, store1)
29-
provideStore(store2Key, store2)
23+
provideStore(StoreKey, store)
3024

3125
return () => h(ChildComponent)
3226
},
3327
})
3428

3529
const wrapper = mount(ParentComponent)
3630

37-
expect(wrapper.text()).toBe('bar baz')
31+
expect(wrapper.text()).toBe('light')
3832
})
3933

4034
test('[custom-store/context] (injectStore) missing store in context', () => {
41-
const storeKey = Symbol('store1')
35+
const StoreKey = Symbol('NonExistentStore')
4236

4337
const ChildComponent = defineComponent({
4438
setup() {
45-
injectStore(storeKey)
39+
injectStore(StoreKey)
4640
return () => h('div')
4741
},
4842
})
4943

5044
const fn = () => mount(ChildComponent)
5145

52-
expect(fn).toThrowError('[tinybase-vue] (injectStore): Could not find store with key Symbol(store1)')
46+
expect(fn).toThrowError('[tinybase-vue] (injectStore): Could not find store with key Symbol(NonExistentStore)')
5347
})
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { createStore } from 'tinybase/with-schemas'
2+
3+
export const store = createStore()
4+
.setTablesSchema({
5+
projects: {
6+
id: { type: 'string' },
7+
name: { type: 'string' },
8+
description: { type: 'string' },
9+
startDate: { type: 'string' },
10+
endDate: { type: 'string' },
11+
},
12+
tasks: {
13+
id: { type: 'string' },
14+
projectId: { type: 'string' },
15+
title: { type: 'string' },
16+
description: { type: 'string' },
17+
status: { type: 'string' },
18+
dueDate: { type: 'string' },
19+
},
20+
users: {
21+
id: { type: 'string' },
22+
name: { type: 'string' },
23+
email: { type: 'string' },
24+
role: { type: 'string' },
25+
},
26+
})
27+
.setValuesSchema({
28+
currentUserId: { type: 'string' },
29+
isAuthenticated: { type: 'boolean' },
30+
theme: { type: 'string', default: 'light' },
31+
notificationsEnabled: { type: 'boolean', default: true },
32+
lastSyncDate: { type: 'string' },
33+
})
34+
.setTables({
35+
projects: {
36+
p1: {
37+
id: 'p1',
38+
name: 'Project Alpha',
39+
description: 'First project description',
40+
startDate: '2023-01-01',
41+
endDate: '2023-06-01',
42+
},
43+
p2: {
44+
id: 'p2',
45+
name: 'Project Beta',
46+
description: 'Second project description',
47+
startDate: '2023-02-01',
48+
endDate: '2023-07-01',
49+
},
50+
p3: {
51+
id: 'p3',
52+
name: 'Project Gamma',
53+
description: 'Third project description',
54+
startDate: '2023-03-01',
55+
endDate: '2023-08-01',
56+
},
57+
},
58+
tasks: {
59+
t1: {
60+
id: 't1',
61+
projectId: 'p1',
62+
title: 'Task 1',
63+
description: 'Description for task 1',
64+
status: 'To Do',
65+
dueDate: '2023-01-10',
66+
},
67+
t2: {
68+
id: 't2',
69+
projectId: 'p1',
70+
title: 'Task 2',
71+
description: 'Description for task 2',
72+
status: 'In Progress',
73+
dueDate: '2023-01-15',
74+
},
75+
t3: {
76+
id: 't3',
77+
projectId: 'p1',
78+
title: 'Task 3',
79+
description: 'Description for task 3',
80+
status: 'Done',
81+
dueDate: '2023-01-20',
82+
},
83+
t4: {
84+
id: 't4',
85+
projectId: 'p2',
86+
title: 'Task 4',
87+
description: 'Description for task 4',
88+
status: 'To Do',
89+
dueDate: '2023-02-10',
90+
},
91+
t5: {
92+
id: 't5',
93+
projectId: 'p2',
94+
title: 'Task 5',
95+
description: 'Description for task 5',
96+
status: 'In Progress',
97+
dueDate: '2023-02-15',
98+
},
99+
t6: {
100+
id: 't6',
101+
projectId: 'p2',
102+
title: 'Task 6',
103+
description: 'Description for task 6',
104+
status: 'Done',
105+
dueDate: '2023-02-20',
106+
},
107+
t7: {
108+
id: 't7',
109+
projectId: 'p3',
110+
title: 'Task 7',
111+
description: 'Description for task 7',
112+
status: 'To Do',
113+
dueDate: '2023-03-10',
114+
},
115+
t8: {
116+
id: 't8',
117+
projectId: 'p3',
118+
title: 'Task 8',
119+
description: 'Description for task 8',
120+
status: 'In Progress',
121+
dueDate: '2023-03-15',
122+
},
123+
t9: {
124+
id: 't9',
125+
projectId: 'p3',
126+
title: 'Task 9',
127+
description: 'Description for task 9',
128+
},
129+
t10: {
130+
id: 't10',
131+
projectId: 'p3',
132+
title: 'Task 10',
133+
description: 'Description for task 10',
134+
status: 'To Do',
135+
dueDate: '2023-03-25',
136+
},
137+
},
138+
users: {
139+
u1: {
140+
id: 'u1',
141+
name: 'Alice Johnson',
142+
143+
role: 'Admin',
144+
},
145+
u2: {
146+
id: 'u2',
147+
name: 'Bob Smith',
148+
149+
role: 'Manager',
150+
},
151+
u3: {
152+
id: 'u3',
153+
name: 'Charlie Brown',
154+
155+
role: 'Developer',
156+
},
157+
},
158+
})
159+
.setValues({
160+
currentUserId: 'u1',
161+
isAuthenticated: true,
162+
theme: 'light',
163+
notificationsEnabled: true,
164+
lastSyncDate: '2023-01-01',
165+
})
166+
167+
export type Store = typeof store
168+
169+
declare module '../types.js' {
170+
export interface VueTinybaseContext {
171+
store: Store
172+
}
173+
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
2-
"extends": "@tsconfig/node20/tsconfig.json",
3-
"include": ["src/**/*"],
4-
"exclude": ["src/**/*.test.ts"],
5-
"compilerOptions": {
6-
"composite": true,
7-
"rootDir": "src",
8-
"outDir": "dist",
9-
"allowJs": true
10-
}
2+
"references": [
3+
{
4+
"path": "./tsconfig.lib.json"
5+
},
6+
{
7+
"path": "./tsconfig.test.json"
8+
}
9+
]
1110
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@tsconfig/node20/tsconfig.json",
3+
"include": ["src/**/*"],
4+
"exclude": ["src/**/*.test.ts"],
5+
"compilerOptions": {
6+
"composite": true,
7+
"rootDir": "src",
8+
"outDir": "dist",
9+
"allowJs": true
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@tsconfig/node20/tsconfig.json",
3+
"include": ["src/**/*"],
4+
"compilerOptions": {
5+
"composite": true,
6+
"rootDir": "src",
7+
"outDir": "dist",
8+
"allowJs": true
9+
}
10+
}

0 commit comments

Comments
 (0)