diff --git a/test/fixtures/modelDNE/index.tsx b/test/fixtures/modelDoNotExist/index.tsx similarity index 100% rename from test/fixtures/modelDNE/index.tsx rename to test/fixtures/modelDoNotExist/index.tsx diff --git a/test/fixtures/modelDNE/test.js b/test/fixtures/modelDoNotExist/test.js similarity index 100% rename from test/fixtures/modelDNE/test.js rename to test/fixtures/modelDoNotExist/test.js diff --git a/test/fixtures/modelWithSelector/index.tsx b/test/fixtures/modelWithSelector/index.tsx index f8f6162..4b30eab 100644 --- a/test/fixtures/modelWithSelector/index.tsx +++ b/test/fixtures/modelWithSelector/index.tsx @@ -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 (<>

{name} is {isAdult ? 'an adult' : 'a teen'}

+ + ); } diff --git a/test/fixtures/modelWithSelector/models/user.js b/test/fixtures/modelWithSelector/models/user.js index bcddc54..5be71c7 100644 --- a/test/fixtures/modelWithSelector/models/user.js +++ b/test/fixtures/modelWithSelector/models/user.js @@ -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: 'troy.lty@alipay.com', gender: 'male', height: '6\'2', age: 24, - } + }); + return [ + data, + dispatch, + ]; } diff --git a/test/fixtures/modelWithSelector/test.js b/test/fixtures/modelWithSelector/test.js index 7341b5f..7477475 100644 --- a/test/fixtures/modelWithSelector/test.js +++ b/test/fixtures/modelWithSelector/test.js @@ -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); } diff --git a/test/index.test.tsx b/test/index.test.tsx index 46dbbd3..6afd53f 100644 --- a/test/index.test.tsx +++ b/test/index.test.tsx @@ -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( - + { + context.updateCount += 1; + }} /> ); await require(join(fixture, 'test.js')).default({ ...renderRet, fireEvent, delay, + context, }); }); });