Skip to content

Commit

Permalink
feat(fallback): add fallback logic and error hint for invalid model (#…
Browse files Browse the repository at this point in the history
…144)

* feat(fallback): add fallback logic and error hint for invalid model

* test(coverage): add testcase to improve test coverage
  • Loading branch information
ArrayZoneYour authored Mar 10, 2020
1 parent f347240 commit a217007
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
34 changes: 34 additions & 0 deletions __test__/Model/error.multi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference path="../index.d.ts" />
import { renderHook } from '@testing-library/react-hooks'
// @ts-ignore
import { ErrorModel as EM } from './errorModel'
import { Model } from '../../src'

describe('useStore', () => {
test('create by single model error definition', async () => {
let state: any
let actions: any
let count = 0
const ErrorModel = Model(EM)
// @ts-ignore
const { useStore, subscribe, unsubscribe } = Model({ ErrorModel })
renderHook(() => {
;[state, actions] = useStore('ErrorModel')
})
expect(actions).toEqual({})
expect(actions.increment).toBe(undefined)
// await actions.increment(3)
expect(state).toEqual({})
// test subscribe
// @ts-ignore
subscribe('increment', () => (count += 1))
expect(count).toBe(0)
expect(state.count).toBe(undefined)
// test unsubscribe
// @ts-ignore
unsubscribe('increment')
expect(actions).toEqual({})
expect(state.count).toBe(undefined)
expect(count).toBe(0)
})
})
32 changes: 32 additions & 0 deletions __test__/Model/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="../index.d.ts" />
import { renderHook } from '@testing-library/react-hooks'
// @ts-ignore
import { ErrorModel } from './errorModel'
import { Model } from '../../src'

describe('useStore', () => {
test('create by single model definition', async () => {
let state: any
let actions: any
let count = 0
// @ts-ignore
const { useStore, subscribe, unsubscribe } = Model(ErrorModel)
renderHook(() => {
;[state, actions] = useStore()
})
expect(state).toEqual({})
expect(actions.increment).toBe(undefined)
// await actions.increment(3)
expect(state).toEqual({})
// test subscribe
subscribe('increment', () => (count += 1))
expect(actions).toEqual({})
expect(count).toBe(0)
expect(state.count).toBe(undefined)
// test unsubscribe
unsubscribe('increment')
expect(actions).toEqual({})
expect(state.count).toBe(undefined)
expect(count).toBe(0)
})
})
23 changes: 23 additions & 0 deletions __test__/Model/errorModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Use to simulate a error model.js file
export const ErrorModel: any = {
actions: {
// @ts-ignore
add: (params, { state }) => {
return {
count: state.count + params
}
},
// @ts-ignore
addCaller: (_, { actions }) => {
actions.add(5)
},
// @ts-ignore
increment: params => {
// @ts-ignore
return state => {
state.count += params
}
},
state: { count: 0 }
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-model",
"version": "3.0.3",
"version": "3.0.4",
"description": "The State management library for React",
"main": "./dist/react-model.js",
"umd:main": "./dist/react-model.umd.js",
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Models<State = any, ActionKeys = any, ExtContext extends {} = {}> {

interface API<MT extends ModelType = ModelType<any, any, {}>> {
__id: string
__ERROR__?: boolean
useStore: (
depActions?: Array<keyof MT['actions']>
) => [Get<MT, 'state'>, getConsumerActionsType<Get<MT, 'actions'>>]
Expand Down Expand Up @@ -167,6 +168,7 @@ type ModelType<
ActionKeys = any,
ExtContext extends {} = {}
> = {
__ERROR__?: boolean
actions: {
[P in keyof ActionKeys]: Action<
InitStateType,
Expand Down
24 changes: 24 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,35 @@ function Model<M extends Models, MT extends ModelType, E>(
useStore(hash, depActions as (string[] | undefined))
}
} else {
if (models.actions) {
console.error('invalid model(s) schema: ', models)
const errorFn = (fakeReturnVal?: unknown) => (..._: unknown[]) => {
return fakeReturnVal
}
// Fallback Functions
return {
__ERROR__: true,
actions: errorFn({}),
getActions: errorFn({}),
getInitialState: errorFn({}),
getState: errorFn({}),
subscribe: errorFn(),
unsubscribe: errorFn(),
useStore: errorFn([{}, {}])
} as any
}
if (initialState) {
Global.State = initialState || {}
}
extContext && (Global.Context['__global'] = extContext)
Object.entries(models).forEach(([name, model]) => {
if (model.__ERROR__) {
// Fallback State and Actions when model schema is invalid
console.error(name + " model's schema is invalid")
Global.State[name] = {}
Global.Actions[name] = {}
return
}
if (!isAPI(model)) {
if (!Global.State[name]) {
Global.State[name] = model.state
Expand Down

0 comments on commit a217007

Please sign in to comment.