Skip to content

Commit 4a3f58f

Browse files
author
Ben Crowl
committed
WIP on tests
1 parent 15c0f90 commit 4a3f58f

File tree

11 files changed

+121
-60
lines changed

11 files changed

+121
-60
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"typescript.format.placeOpenBraceOnNewLineForFunctions": true,
44
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
55
"tsimporter.emitSemicolon": false,
6+
"tsimporter.noStatusBar": false,
67
"tsimporter.filesToExclude": [
7-
"./dist/**/*"
8+
"./dist/**"
89
]
910
}

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Vuex-Typescript-Builder
1+
# Vuex-Typex
22

3-
[https://github.com/mrcrowl/vuex-ts-builder/](https://github.com/mrcrowl/vuex-ts-builder/)
3+
[https://github.com/mrcrowl/vuex-typex/](https://github.com/mrcrowl/vuex-typex/)
44

5-
A shameless extension of the ideas presented by [@istrib](https://github.com/istrib) in the [vuex-typescript](https://github.com/istrib/vuex-typescript) package.
5+
## A TypeScript pattern for strongly-typed access to Vuex Store modules
6+
7+
\*A shameless extension of the ideas presented by [@istrib](https://github.com/istrib) in the [vuex-typescript](https://github.com/istrib/vuex-typescript) package.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "vuex-ts-builder",
2+
"name": "vuex-typex",
33
"version": "1.0.0",
4-
"description": "Typescript builder for strongly-typed access to Vuex Store modules",
4+
"description": "A TypeScript pattern for strongly-typed access to Vuex Store modules",
55
"files": [
66
"dist/index.js",
77
"dist/index.d.ts"
@@ -24,7 +24,7 @@
2424
},
2525
"repository": {
2626
"type": "git",
27-
"url": "git+https://github.com/mrcrowl/vuex-ts-builder.git"
27+
"url": "git+https://github.com/mrcrowl/vuex-typex.git"
2828
},
2929
"keywords": [
3030
"vuex",
@@ -34,9 +34,9 @@
3434
"author": "mrcrowl",
3535
"license": "MIT",
3636
"bugs": {
37-
"url": "https://github.com/mrcrowl/vuex-ts-builder/issues"
37+
"url": "https://github.com/mrcrowl/vuex-typex/issues"
3838
},
39-
"homepage": "https://github.com/mrcrowl/vuex-ts-builder#readme",
39+
"homepage": "https://github.com/mrcrowl/vuex-typex#readme",
4040
"scripts": {
4141
"clean": "rimraf dist && rimraf coverage",
4242
"compile": "tsc --pretty",

src/bah.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
type a = { a: string }
3+
type b = { b: number }
4+
type c = { c: Date }
5+
type d<T> = { d: (payload: { s: T }) => T }
6+
7+
type e = a & b & c & d<Symbol> & { z: StringConstructor }
8+
9+
type f = {[K in keyof e]: e[K]}
10+
11+
var abcd: e;

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ActionContext, ActionTree, GetterTree, Module, MutationTree, Store } fr
44
const useRootNamespace = { root: true }
55

66
export type MutationHandler<S, P> = (state: S, payload: P) => void
7-
export type ActionHandler<S, R, P, T> = (context: BareActionContext<S, R>, payload: P) => Promise<T>
7+
export type ActionHandler<S, R, P, T> = (context: BareActionContext<S, R>, payload: P) => Promise<T> | T
88
export type GetterHandler<S, R, T> = (state: S, rootState: R) => T
99

1010
export interface BareActionContext<S, R>

src/tests/store/auth/auth.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11

22
import { ModuleBuilder } from "../../.."
3-
import { BirthdayState } from "./state"
3+
import { AuthState } from "./state"
4+
import { RootState } from "../index"
5+
import { Module } from "vuex"
46

7+
const initialState: AuthState = {
8+
userID: "b6c8185c6d0af2f5d968",
9+
isLoggedIn: true
10+
}
511

6-
const birthday = new ModuleBuilder<AuthState>
12+
const m = new ModuleBuilder<AuthState, RootState>("auth", initialState)
13+
14+
const auth = {
15+
commitSetUserID: m.commit((state, payload: { userID: string }) => state.userID = payload.userID),
16+
commitSetIsLoggedIn: m.commit((state, payload: { isLoggedIn: boolean }) => state.isLoggedIn = payload.isLoggedIn),
17+
dispatchLogin: m.dispatch((context) =>
18+
{
19+
return
20+
})
21+
}
22+
23+
export default auth
24+
export const birthdayModule: Module<AuthState, RootState> = m.toVuexModule()

src/tests/store/auth/state.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
export interface AuthState
33
{
44
userID: string
5+
isLoggedIn: boolean
56
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import birthday from "../birthday"
2+
import { BareActionContext } from "../../../../index"
3+
import { BirthdayState } from "../state"
4+
import { RootState } from "../../root"
5+
6+
export default async function removeFirstAfter(context: BareActionContext<BirthdayState, RootState>, delay: number)
7+
{
8+
if (context.state.birthdays.length > 2)
9+
{
10+
await new Promise((resolve, _) => setTimeout(resolve, 1000)); // second delay
11+
birthday.commitRemoveFirstBirthday()
12+
}
13+
}

src/tests/store/birthday/birthday.ts

+44-39
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,54 @@ import { ModuleBuilder } from "../../.."
33
import { BirthdayState, Birthday } from "./state"
44
import { Module } from "vuex"
55
import { RootState } from "../root"
6+
import * as Vuex from "vuex"
7+
import removeFirstAfter from "../../../../dist/tests/store/birthday/actions/removeFirstAfter"
68

79
const initialState: BirthdayState = {
810
birthdays: [
911
{
10-
name: "Jacob",
11-
dob: new Date(2006, 10, 11)
12+
name: "Richard",
13+
dob: new Date(1995, 10, 11)
1214
},
1315
{
14-
name: "Danny",
15-
dob: new Date(2009, 11, 30)
16+
name: "Erlich",
17+
dob: new Date(1983, 1, 17)
18+
},
19+
{
20+
name: "Nelson",
21+
dob: new Date(1996, 3, 28)
22+
},
23+
{
24+
name: "Dinesh",
25+
dob: new Date(1989, 1, 7)
26+
},
27+
{
28+
name: "Bertram",
29+
dob: new Date(1985, 7, 14)
30+
},
31+
{
32+
name: "Donald",
33+
dob: new Date(1994, 5, 31)
34+
},
35+
{
36+
name: "Monica",
37+
dob: new Date(1996, 8, 26)
1638
}
1739
]
1840
}
1941

20-
const builder = new ModuleBuilder<BirthdayState, RootState>("birthday", initialState)
42+
const m = new ModuleBuilder<BirthdayState, RootState>("birthday", initialState)
2143

22-
const commitAddBirthday = builder.commit((state, payload: { birthday: Birthday }) =>
23-
{
24-
state.birthdays.push(payload.birthday)
25-
})
44+
const addBirthdayMut = (state: BirthdayState, payload: { birthday: Birthday }) => state.birthdays.push(payload.birthday)
45+
const removeFirstBirthdayMut = (state: BirthdayState) => state.birthdays.shift()
2646

27-
const commitRemoveFirstBirthday = builder.commit((state) =>
28-
{
29-
state.birthdays.shift()
30-
})
31-
32-
const getOldestName = builder.read((state): Birthday | undefined =>
47+
const oldestNameGetter = m.read((state): Birthday | undefined =>
3348
{
3449
const sortedBirthdays = (<Birthday[]>[]).sort((a, b) => a.dob.getTime() - b.dob.getTime())
3550
return sortedBirthdays[0]
36-
})
51+
}, "oldestName")
3752

38-
const getDOBforName = builder.read((state) => (name: string) =>
53+
const dateOfBirthForMethod = m.read((state) => (name: string) =>
3954
{
4055
const matches = state.birthdays.filter(b => b.name === name)
4156
if (matches.length)
@@ -46,33 +61,23 @@ const getDOBforName = builder.read((state) => (name: string) =>
4661
return
4762
}, "dob")
4863

49-
const dispatchRemoveFirstAfter = builder.dispatch(async (context, delay: number) =>
50-
{
51-
if (context.state.birthdays.length > 2)
52-
{
53-
await new Promise((resolve, _) => setTimeout(resolve, 1000)); // second delay
54-
commitRemoveFirstBirthday()
55-
}
56-
})
57-
58-
const module = {
64+
const birthday = {
5965
// getters + methods
60-
get oldestName() { return getOldestName() },
61-
dateOfBirthFor(name: string) { return getDOBforName()(name) },
66+
get oldestName() { return oldestNameGetter() },
67+
dateOfBirthFor(name: string) { return dateOfBirthForMethod()(name) },
6268

6369
// mutations
64-
commitAddBirthday,
65-
commitRemoveFirstBirthday,
70+
commitAddBirthday: m.commit(addBirthdayMut),
71+
commitRemoveFirstBirthday: m.commit(removeFirstBirthdayMut),
6672

6773
// actions
68-
dispatchRemoveFirstAfter,
74+
dispatchRemoveFirstAfter: m.dispatch(removeFirstAfter)
6975
}
7076

71-
module.commitAddBirthday({ birthday: { dob: new Date(1980, 2, 3), name: "Louise" } })
72-
module.commitRemoveFirstBirthday()
73-
module.dateOfBirthFor("Louise")
74-
module.dispatchRemoveFirstAfter(1000)
75-
76-
export default module
77+
// birthday.commitAddBirthday({ birthday: { dob: new Date(1980, 2, 3), name: "Louise" } })
78+
// birthday.commitRemoveFirstBirthday()
79+
// birthday.dateOfBirthFor("Louise")
80+
// birthday.dispatchRemoveFirstAfter(1000)
7781

78-
export const vuexModule: Module<BirthdayState, RootState> = builder.toVuexModule()
82+
export default birthday
83+
export const birthdayModule: Module<BirthdayState, RootState> = m.toVuexModule()

src/tests/store/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import { AuthState } from "./auth/state";
3+
import { BirthdayState } from "../store/birthday/state"
4+
import { Store } from "vuex"
5+
import { authModule } from './auth/auth'
6+
import { birthdayModule } from './birthday/birthday'
7+
8+
export interface RootState
9+
{
10+
auth: AuthState
11+
birthday: BirthdayState
12+
}
13+
14+
export const createStore = new Store({
15+
modules: {
16+
auth: authModule,
17+
birthday: birthdayModule
18+
}
19+
})

src/tests/store/root.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)