-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(coverage): Improve Test Coverage from zero.
- Loading branch information
1 parent
10b0204
commit 2dc0e2f
Showing
20 changed files
with
241 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,7 @@ dist/ | |
|
||
# yarn | ||
yarn.lock | ||
package-lock.json | ||
package-lock.json | ||
|
||
# Jest | ||
coverage/ |
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ package-lock.json | |
# Test | ||
__test__/ | ||
jest.config.js | ||
coverage/ | ||
|
||
# microbundle | ||
.rts2_cache_cjs/ | ||
|
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,12 @@ | ||
/// <reference path="./index.d.ts" /> | ||
import { Model } from '../src' | ||
import { AsyncCounter } from '.' | ||
|
||
describe('asyncState', () => { | ||
test('return default initial state from asyncState', async () => { | ||
const { getState, getInitialState } = Model({ AsyncCounter }) | ||
await getInitialState() | ||
const state = getState('AsyncCounter') | ||
expect(state.count).toBe(1) | ||
}) | ||
}) |
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,13 @@ | ||
/// <reference path="./index.d.ts" /> | ||
import 'react-testing-library/cleanup-after-each' | ||
import { Model } from '../src' | ||
import { Counter } from '.' | ||
|
||
describe('getState', () => { | ||
test('return default initial state', () => { | ||
let state | ||
const { getState } = Model({ Counter }) | ||
state = getState('Counter') | ||
expect(state.count).toBe(0) | ||
}) | ||
}) |
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,11 @@ | ||
type CounterState = { | ||
count: number | ||
} | ||
|
||
type CounterActionParams = { | ||
increment: number | ||
} | ||
|
||
type ExtraActionParams = { | ||
add: number | ||
} |
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
/// <reference path="./index.d.ts" /> | ||
import { timeout } from '../src/helper' | ||
|
||
export const Counter: ModelType< | ||
CounterState, | ||
CounterActionParams & ExtraActionParams | ||
> = { | ||
state: { count: 0 }, | ||
actions: { | ||
increment: (_, __, params) => { | ||
return state => { | ||
state.count += params | ||
} | ||
}, | ||
add: (state, __, params) => { | ||
return { | ||
count: state.count += params | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const AsyncCounter: ModelType<CounterState, CounterActionParams> = { | ||
state: { count: 0 }, | ||
asyncState: async () => ({ | ||
count: 1 | ||
}), | ||
actions: { | ||
increment: (_, __, params) => { | ||
return state => { | ||
state.count += params | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const TimeoutCounter: ModelType<CounterState, CounterActionParams> = { | ||
state: { count: 0 }, | ||
asyncState: async () => ({ | ||
count: 1 | ||
}), | ||
actions: { | ||
increment: async (_, __, params) => { | ||
await timeout(4000, {}) | ||
return (state: typeof _) => { | ||
state.count += params | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const ErrorCounter: ModelType<CounterState, CounterActionParams> = { | ||
state: { count: 0 }, | ||
actions: { | ||
increment: async () => { | ||
throw 'error' | ||
return {} | ||
} | ||
} | ||
} |
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,27 @@ | ||
/// <reference path="../index.d.ts" /> | ||
import 'react-testing-library/cleanup-after-each' | ||
import { testHook } from 'react-testing-library' | ||
import { Model } from '../../src' | ||
import { Counter } from '..' | ||
|
||
describe('', () => { | ||
test('communicator', async () => { | ||
let stateFirst: any, stateSecond: any | ||
let actionsFirst: any, actionsSecond: any | ||
const { useStore } = Model({ Counter }) | ||
testHook(() => { | ||
;[stateFirst, actionsFirst] = useStore('Counter') | ||
}) | ||
testHook(() => { | ||
;[stateSecond, actionsSecond] = useStore('Counter') | ||
}) | ||
expect(stateFirst.count).toBe(0) | ||
expect(stateSecond.count).toBe(0) | ||
await actionsFirst.increment(3) | ||
expect(stateFirst.count).toBe(3) | ||
expect(stateSecond.count).toBe(3) | ||
await actionsSecond.increment(4) | ||
expect(stateFirst.count).toBe(7) | ||
expect(stateSecond.count).toBe(7) | ||
}) | ||
}) |
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,23 @@ | ||
/// <reference path="../index.d.ts" /> | ||
import 'react-testing-library/cleanup-after-each' | ||
;(window as any).__REDUX_DEVTOOLS_EXTENSION__ = { | ||
connect: () => {}, | ||
send: () => {} | ||
} | ||
import { testHook } from 'react-testing-library' | ||
import { Model } from '../../src' | ||
import { Counter } from '..' | ||
|
||
describe('withDevTools', () => { | ||
test("won't break the behavior without DevTools", async () => { | ||
let state: any | ||
let actions: any | ||
const { useStore } = Model({ Counter }) | ||
testHook(() => { | ||
;[state, actions] = useStore('Counter') | ||
}) | ||
expect(state).toEqual({ count: 0 }) | ||
await actions.increment(3) | ||
expect(state.count).toBe(3) | ||
}) | ||
}) |
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,18 @@ | ||
/// <reference path="../index.d.ts" /> | ||
import { Model, middlewares } from '../../src' | ||
import { testHook } from 'react-testing-library' | ||
import { actionMiddlewares } from '../../src' | ||
import { TimeoutCounter } from '..' | ||
|
||
describe('getNewStateWithCache: ', () => { | ||
test('cache when timeout', async () => { | ||
let state: any, actions: any | ||
actionMiddlewares[0] = middlewares.getNewStateWithCache(3000) | ||
const { useStore } = Model({ TimeoutCounter }) | ||
testHook(() => { | ||
;[state, actions] = useStore('TimeoutCounter') | ||
}) | ||
await actions.increment(3) | ||
expect(state).toEqual({ count: 0 }) | ||
}) | ||
}) |
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,22 @@ | ||
/// <reference path="../index.d.ts" /> | ||
import 'react-testing-library/cleanup-after-each' | ||
process.env.NODE_ENV = 'production' | ||
console.log(process.env) | ||
import { Model } from '../../src' | ||
import { ErrorCounter } from '..' | ||
import { testHook } from 'react-testing-library' | ||
|
||
describe('tryCatch', () => { | ||
test("catch actions' error in production", async () => { | ||
let actions: any | ||
let errNum = 0 | ||
const { useStore } = Model({ ErrorCounter }) | ||
testHook(() => { | ||
;[, actions] = useStore('ErrorCounter') | ||
}) | ||
await actions.increment().catch(() => { | ||
errNum += 1 | ||
}) | ||
expect(errNum).toBe(0) | ||
}) | ||
}) |
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,37 @@ | ||
/// <reference path="./index.d.ts" /> | ||
import { testHook } from 'react-testing-library' | ||
import { Model } from '../src' | ||
import { Counter } from '.' | ||
|
||
describe('useStore', () => { | ||
test('return default initial values', () => { | ||
let state | ||
const { useStore } = Model({ Counter }) | ||
testHook(() => { | ||
;[state] = useStore('Counter') | ||
}) | ||
expect(state).toEqual({ count: 0 }) | ||
}) | ||
test('consumer actions return function', async () => { | ||
let state: any | ||
let actions: any | ||
const { useStore } = Model({ Counter }) | ||
testHook(() => { | ||
;[state, actions] = useStore('Counter') | ||
}) | ||
await actions.increment(3) | ||
expect(state).toEqual({ count: 3 }) | ||
await actions.increment(4) | ||
expect(state.count).toBe(7) | ||
}) | ||
test('consumer actions return Partial<State>', async () => { | ||
let state: any | ||
let actions: any | ||
const { useStore } = Model({ Counter }) | ||
testHook(() => { | ||
;[state, actions] = useStore('Counter') | ||
}) | ||
await actions.add(3) | ||
expect(state).toEqual({ count: 3 }) | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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