Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @ice/store Unit Test #84

Merged
merged 42 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
157e1fa
chore: add dependency
luhc228 Mar 7, 2020
40901a8
test: add helper and init test
luhc228 Mar 7, 2020
2b1becf
test: add createStore test
luhc228 Mar 10, 2020
a65917e
test: add withModel withModelActions withModelEffectsState test
luhc228 Mar 11, 2020
3885972
test: add throw error when getting unexist model
luhc228 Mar 11, 2020
ee3ddad
chore: new version
alvinhui Mar 12, 2020
42c93f7
test: add global actions test
luhc228 Mar 12, 2020
279ee35
Merge branch 'master' into unit-test
luhc228 Mar 12, 2020
e2b1907
Merge branch 'master' into unit-test
luhc228 Mar 13, 2020
8cbd919
test: add createModel test
luhc228 Mar 13, 2020
ab7be49
Merge branch 'release/1.2.0' into unit-test
luhc228 Mar 13, 2020
35f581c
test: add createUseContainer test
luhc228 Mar 15, 2020
0b053ac
Merge branch 'unit-test' of github.com:luhc228/icestore into unit-test
luhc228 Mar 15, 2020
868aca5
chore: createModel
luhc228 Mar 15, 2020
5a65266
test: add createContainer test
luhc228 Mar 16, 2020
0490e8b
fix: fix conflict
luhc228 Apr 2, 2020
a5937f8
Merge branch 'master' into unit-test
luhc228 Apr 3, 2020
0ebb82f
test: update test cases
luhc228 Apr 3, 2020
c3a508f
test: add useModel test
luhc228 Apr 5, 2020
d5a4ef1
test: add class component test
luhc228 Apr 7, 2020
1a6548d
Merge branch 'master' into unit-test
luhc228 Apr 7, 2020
3e1b591
test: update class component test
luhc228 Apr 7, 2020
ea3274f
Merge branch 'master' into unit-test
luhc228 Apr 8, 2020
a379c0b
test: add createStore options test
luhc228 Apr 8, 2020
35a391e
test: add appendReducer test
luhc228 Apr 12, 2020
86e73aa
test: add appendReducer test
luhc228 Apr 12, 2020
5fcec81
chore: lint
luhc228 Apr 12, 2020
af46b44
test: add utils test
luhc228 Apr 12, 2020
dcc0b7d
fix: fix conflict
luhc228 Apr 12, 2020
d01b605
docs: add badge
luhc228 Apr 12, 2020
672a3a8
chore: remove unnecessary comment
luhc228 Apr 13, 2020
3cfc1da
test: update converter test
luhc228 Apr 13, 2020
5b6020a
Merge branch 'master' into unit-test
alvinhui Apr 14, 2020
438dca4
feat: add comment to pr
luhc228 Apr 16, 2020
3eb981f
fix: fix by the comments
luhc228 Apr 16, 2020
8f9e41f
Merge branch 'unit-test' of github.com:luhc228/icestore into unit-test
luhc228 Apr 16, 2020
5038104
chore: update codecov config
luhc228 Apr 16, 2020
08f9e61
test: update test
luhc228 Apr 16, 2020
35c06f4
test: update converter test case
luhc228 Apr 16, 2020
cab9669
test: update examples
luhc228 Apr 17, 2020
35c7caf
test: update todos example
luhc228 Apr 17, 2020
0f43793
chore: update modelEffects type
luhc228 Apr 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ English | [简体中文](./README.zh-CN.md)
[![NPM downloads](http://img.shields.io/npm/dm/@ice/store.svg?style=flat)](https://npmjs.org/package/@ice/store)
[![Known Vulnerabilities](https://snyk.io/test/npm/@ice/store/badge.svg)](https://snyk.io/test/npm/@ice/store)
[![David deps](https://img.shields.io/david/ice-lab/icestore.svg?style=flat-square)](https://david-dm.org/ice-lab/icestore)
[![codecov](https://codecov.io/gh/ice-lab/icestore/branch/master/graph/badge.svg)](https://codecov.io/gh/ice-lab/icestore)

<table>
<thead>
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[![NPM downloads](http://img.shields.io/npm/dm/@ice/store.svg?style=flat)](https://npmjs.org/package/@ice/store)
[![Known Vulnerabilities](https://snyk.io/test/npm/@ice/store/badge.svg)](https://snyk.io/test/npm/@ice/store)
[![David deps](https://img.shields.io/david/ice-lab/icestore.svg?style=flat-square)](https://david-dm.org/ice-lab/icestore)
[![codecov](https://codecov.io/gh/ice-lab/icestore/branch/master/graph/badge.svg)](https://codecov.io/gh/ice-lab/icestore)

<table>
<thead>
Expand Down
10 changes: 9 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
comment: off
comment:
layout: "reach, diff, flags, files"
behavior: default
require_changes: false
require_base: no
require_head: yes
branches:
- "master"

coverage:
status:
project:
Expand Down
5 changes: 2 additions & 3 deletions examples/todos/src/components/Todos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ const { useModel, useModelEffectsLoading } = store;

export default function Todos() {
const todos = useModel('todos');
const [ state, dispatchers ] = todos;
const [state, dispatchers] = todos;
const effectsLoading = useModelEffectsLoading('todos');

const { dataSource } = state;
const { refresh } = dispatchers;

useEffect(() => {
refresh();

// eslint-disable-next-line
// eslint-disable-next-line
}, []);

const noTaskView = <div>no task</div>;
Expand Down
5 changes: 2 additions & 3 deletions examples/todos/src/components/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import store from '../store';
const { useModel } = store;

export default function UserApp() {
const [ state, dispatchers ] = useModel('user');
const [state, dispatchers] = useModel('user');
const { dataSource, auth, todos } = state;
const { login } = dispatchers;
const { name } = dataSource;

useEffect(() => {
login();

// eslint-disable-next-line
// eslint-disable-next-line
}, []);

console.debug('UserApp rending...');
Expand Down
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@
"prepublishOnly": "npm run lint:nofix && npm run test && npm run build",
"lint": "npm run lint:nofix -- --fix",
"lint:nofix": "eslint --cache --ext .ts,.tsx ./",
"test": "NODE_ENV=unittest jest",
"test": "cross-env NODE_ENV=unittest jest",
"test:w": "jest --watch",
"coverage": "codecov"
},
"devDependencies": {
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@ice/spec": "^0.1.9",
"@testing-library/react": "^9.0.0",
"@types/jest": "^24.0.12",
"@testing-library/react-hooks": "^3.2.1",
"@types/jest": "^25.2.1",
"@types/node": "^12.0.0",
"codecov": "^3.3.0",
"cross-env": "^7.0.2",
"eslint": "^6.7.2",
"husky": "^3.0.9",
"jest": "^24.7.1",
"jest": "^25.2.1",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"ts-jest": "^24.0.2",
"react-test-renderer": "^16.13.0",
"ts-jest": "^25.2.1",
"typescript": "^3.7.4"
},
"peerDependencies": {
Expand All @@ -55,6 +59,10 @@
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"<rootDir>/tests/helpers/",
"<rootDir>/node_modules/"
],
"preset": "ts-jest"
},
"dependencies": {
Expand All @@ -64,4 +72,4 @@
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
}
}
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ export interface ModelEffects<S> {
[key: string]: (
this: { [key: string]: (payload?: any, meta?: any) => Action<any, any> },
payload: any,
rootState: S
rootState: S,
meta: any
) => void;
}

Expand Down
60 changes: 60 additions & 0 deletions tests/helpers/CounterComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { PureComponent } from 'react';
import {
ExtractIModelFromModelConfig,
ExtractIModelDispatchersFromModelConfig,
ExtractIModelEffectsStateFromModelConfig,
} from '../../src';
import counterModel from './counter';

interface CounterProps {
counter: ExtractIModelFromModelConfig<typeof counterModel>;
children: React.ReactNode;
}

export default class Counter extends PureComponent<CounterProps> {
render() {
const { counter, children } = this.props;
const [state, dispatchers] = counter;
const { count } = state;
return (
<React.Fragment>
<div data-testid="count">{count}</div>
<div data-testid="setState" onClick={() => dispatchers.setState({ count: 1 })} />
<div data-testid="increment" onClick={dispatchers.increment} />
<div data-testid="decrement" onClick={dispatchers.decrement} />
<div data-testid="decrementAsync" onClick={dispatchers.decrementAsync} />
{children}
</React.Fragment>
);
}
}

interface CounterUseDispathcersProps {
counterDispatchers: ExtractIModelDispatchersFromModelConfig<typeof counterModel>;
};
export class CounterUseDispathcers extends PureComponent<CounterUseDispathcersProps> {
render() {
const { counterDispatchers } = this.props;
return (
<div data-testid="reset" onClick={() => counterDispatchers.reset()} />
);
}
};

interface CounterUseEffectsStateProps {
counterEffectsState: ExtractIModelEffectsStateFromModelConfig<typeof counterModel>;
children: React.ReactChild;
}
export class CounterUseEffectsState extends PureComponent<CounterUseEffectsStateProps> {
render() {
const { counterEffectsState, children } = this.props;
return (
<React.Fragment>
<code data-testid="decrementAsyncEffectsState">
{JSON.stringify(counterEffectsState.decrementAsync)}
</code>
{children}
</React.Fragment>
);
}
}
64 changes: 64 additions & 0 deletions tests/helpers/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { delay } from './utils';

export interface CounterState {
count: number;
}

const counter = {
state: {
count: 0,
},
reducers: {
increment: (prevState: CounterState) => prevState.count += 1,
decrement: (prevState: CounterState) => prevState.count -= 1,
reset: () => ({ count: 0 }),
},
effects: (dispatch) => ({
async decrementAsync(_, rootState) {
if (rootState.counter.count <= 0) {
throw new Error('count should be greater than or equal to 0');
}
await delay(1000);
this.decrement();
},
}),
};

export const counterWithUnsupportEffects = {
state: {
a: 1,
},
effects: {
incrementA: (state, value) => {
return {
...state,
a: state.a + value,
};
},
},
};

export const counterWithUnsupportActions = {
state: {
a: 1,
},
actions: {
incrementA: (state, value) => {
return {
...state,
a: state.a + value,
};
},
},
};

export const counterWithNoImmer = {
state: {
count: 1,
},
reducers: {
increment: (prevState) => { return prevState.count + 1; },
},
};

export default counter;
2 changes: 2 additions & 0 deletions tests/helpers/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as todos } from './todos';
export { default as user } from './user';
45 changes: 45 additions & 0 deletions tests/helpers/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { delay } from './utils';

export interface Todo {
name: string;
done?: boolean;
}

export interface TodosState {
dataSource: Todo[];
}

const todos = {
state: {
dataSource: [
{
name: 'Init',
done: false,
},
],
},

reducers: {
addTodo(state: TodosState, todo: Todo) {
state.dataSource.push(todo);
},
removeTodo(state: TodosState, index: number) {
state.dataSource.splice(index, 1);
},
},

effects: (dispatch) => ({
add(todo, rootState, { store }) {
this.addTodo(todo);
dispatch.user.setTodos(store.getModelState('todos').dataSource.length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

effect 的第一个参数应该是 playload

},

async delete(index, rootState, { store }) {
await delay(1000);
this.removeTodo(index);
dispatch.user.setTodos(store.getModelState('todos').dataSource.length);
},
}),
};

export default todos;
21 changes: 21 additions & 0 deletions tests/helpers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface DataSourceState {
name: string;
}
class UserStateProps {
dataSource: DataSourceState = { name: 'testName' };

todos: number = 1;

auth: boolean = false;
}

const user = {
state: new UserStateProps,
reducers: {
setTodos(state: UserStateProps, todos: number) {
state.todos = todos;
},
},
};

export default user;
1 change: 1 addition & 0 deletions tests/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const delay = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));
Loading