Skip to content

Commit

Permalink
test(coverage): Improve Test Coverage from zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayZoneYour committed Feb 18, 2019
1 parent 10b0204 commit 2dc0e2f
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 47 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ dist/

# yarn
yarn.lock
package-lock.json
package-lock.json

# Jest
coverage/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package-lock.json
# Test
__test__/
jest.config.js
coverage/

# microbundle
.rts2_cache_cjs/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# react-model · ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![npm version](https://img.shields.io/npm/v/react-model.svg?style=flat)](https://www.npmjs.com/package/react-model) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js) [![downloads](https://img.shields.io/npm/dt/react-model.svg)](https://www.npmjs.com/package/react-model) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
# react-model · ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![npm version](https://img.shields.io/npm/v/react-model.svg?style=flat)](https://www.npmjs.com/package/react-model) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js) [![downloads](https://img.shields.io/npm/dt/react-model.svg)](https://www.npmjs.com/package/react-model) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg) ![statement coverage](badges/badge-statements.svg) ![lines coverage](badges/badge-lines.svg)

The State management library for React

Expand Down
12 changes: 12 additions & 0 deletions __test__/asyncState.spec.ts
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)
})
})
13 changes: 13 additions & 0 deletions __test__/getState.spec.ts
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)
})
})
11 changes: 11 additions & 0 deletions __test__/index.d.ts
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
}
34 changes: 0 additions & 34 deletions __test__/index.spec.ts

This file was deleted.

60 changes: 60 additions & 0 deletions __test__/index.ts
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 {}
}
}
}
27 changes: 27 additions & 0 deletions __test__/middlewares/commuicator.spec.ts
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)
})
})
23 changes: 23 additions & 0 deletions __test__/middlewares/devToolsListener.spec.ts
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)
})
})
18 changes: 18 additions & 0 deletions __test__/middlewares/getNewStateWithCache.spec.ts
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 })
})
})
22 changes: 22 additions & 0 deletions __test__/middlewares/tryCatch.spec.ts
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)
})
})
37 changes: 37 additions & 0 deletions __test__/useStore.spec.ts
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 })
})
})
1 change: 1 addition & 0 deletions badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 2 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@ module.exports = {
coverageDirectory: 'coverage',

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
coveragePathIgnorePatterns: ['/node_modules/', '/__test__/'],

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
coverageReporters: ['json-summary', 'text', 'lcov', 'clover'],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"types": "./src/index",
"scripts": {
"build": "microbundle -o ./dist --sourcemap false",
"test": "jest"
"test": "jest --silent",
"test:coverage": "jest --collect-coverage --silent",
"test:badges": "jest --coverage --silent && jest-coverage-badges --output './badges'"
},
"keywords": [
"react",
Expand All @@ -30,6 +32,7 @@
"@types/node": "^10.12.21",
"@types/react": "^16.8.2",
"@types/react-dom": "^16.8.0",
"dotenv": "^6.2.0",
"jest": "^24.1.0",
"react-testing-library": "^5.8.0",
"ts-jest": "^23.10.5",
Expand Down
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
setPartialState,
getInitialState
} from './helper'
import { actionMiddlewares, applyMiddlewares } from './middlewares'
import { actionMiddlewares, applyMiddlewares, middlewares } from './middlewares'

// TODO: Cross Model communication

Expand Down Expand Up @@ -178,6 +178,7 @@ const connect = (modelName: string, mapProps: Function | undefined) => (
export {
actionMiddlewares,
Model,
middlewares,
Provider,
Consumer,
connect,
Expand Down

0 comments on commit 2dc0e2f

Please sign in to comment.