This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
56 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -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> | ||
</>); | ||
} |
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 |
---|---|---|
@@ -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, | ||
]; | ||
} |
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 |
---|---|---|
@@ -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); | ||
} |
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