Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
test: more testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 11, 2019
1 parent 38115cc commit cca119a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
23 changes: 20 additions & 3 deletions test/fixtures/modelWithSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useModel } from './.umi/useModel';

export default () => {
const { isAdult, name } = useModel('user', user => ({ name: user.name, isAdult: user.age > 18 }));
export default (props) => {
const { isAdult, name, dispatch } = useModel(
'user',
user => ({ name: user[0].name, isAdult: user[0].age > 18, dispatch: user[1] }),
);
useEffect(() => {
props.onUpdate();
});
return (<>
<h2 data-testid="user">{name} is {isAdult ? 'an adult' : 'a teen'}</h2>
<button onClick={() => {
dispatch({
type: 'changeGender',
});
}}>changeGender</button>
<button onClick={() => {
dispatch({
type: 'setAge',
payload: 5,
});
}}>increaseAge</button>
</>);
}
26 changes: 23 additions & 3 deletions test/fixtures/modelWithSelector/models/user.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { useState } from 'react';
import { useReducer } from 'react';

export default () => {
return {
function reducer(state, action) {
switch (action.type) {
case 'changeGender':
return {
...state,
gender: state.gender === 'male' ? 'female' : 'male',
};
case 'setAge':
return {
...state,
age: action.payload,
};
default:
break;
}
}
const [data, dispatch] = useReducer(reducer, {
name: 'Troy',
email: '[email protected]',
gender: 'male',
height: '6\'2',
age: 24,
}
});
return [
data,
dispatch,
];
}
7 changes: 6 additions & 1 deletion test/fixtures/modelWithSelector/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

export default async function ({ getByTestId }) {
export default async function ({ getByTestId, getByText, fireEvent, context }) {
expect(getByTestId('user').innerHTML).toEqual('Troy is an adult');
expect(context.updateCount).toEqual(1);
fireEvent.click(getByText(/changeGender/));
expect(context.updateCount).toEqual(1);
fireEvent.click(getByText(/increaseAge/));
expect(context.updateCount).toEqual(2);
}
8 changes: 7 additions & 1 deletion test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ readdirSync(fixtures)
test(file, async () => {
const Provider = require(providerPath).default;
const App = require(join(fixture, 'index.tsx')).default;
const context = {
updateCount: 0,
};
const renderRet = render(
<Provider><App /></Provider>
<Provider><App onUpdate={() => {
context.updateCount += 1;
}} /></Provider>
);
await require(join(fixture, 'test.js')).default({
...renderRet,
fireEvent,
delay,
context,
});
});
});

0 comments on commit cca119a

Please sign in to comment.