javascript - 异步/等待,没有达到下一个功能
问题描述
我有这个代码:
const handleButton = React.useCallback(async () => {
try {
await client.clearStore();
navigation.push('screen');
} catch (e) {
logger.error('error', e);
}
}, []);
我正在测试nagivation
to screen
,但await
我无法执行或到达navigation.push()
. 如果我删除await
onclient.clearStore
一切正常,但行为可能与预期不同(我需要先 clearStore 然后导航)。
任何想法?
编辑:
我的测试是这样的:
import { useApolloClient } from '@apollo/react-hooks';
// After imports
jest.mock('@apollo/react-hooks');
// In describe:
describe('text', () => {
beforeEach(async () => {
// @ts-ignore
moment.mockClear();
useApolloClient.mockClear();
});
// My test:
it.only('should redirects to MyComponent when button is pressed', async () => {
const apolloClient = {
clearStore: jest.fn()
};
useApolloClient.mockImplementation(() => apolloClient);
apolloClient.clearStore
.mockReturnValueOnce(
Promise.resolve('foo')
);
moment.mockImplementation(
(args: moment.MomentInput) =>
jest.requireActual('moment')(args || '2020-07-30T20:00:00.000Z')
);
const navigation = {
push: jest.fn()
};
const rendered = render(
<MyComponent
paymentGatewayCode='khipu'
allowToPay={true}
expiresDate={'2020-07-30T20:00:00.000Z'}
navigation={navigation}
/>
);
const button = rendered.getByTestId('button');
fireEvent.press(button);
console.log('navigation.push.mock.calls', navigation.push.mock.calls);
expect(navigation.push.mock.calls.length).toBe(1);
expect(navigation.push.mock.calls[0]).toEqual(['MyComponent']);
});
我在运行测试时得到了这个:
解决方案
推荐阅读
- vaadin - Vaadin:如何更新图表的数据?
- mysql - 如何将数据插入从单个表单接收的多个数据库表中?Django/MySql
- node.js - mongoose:对 $project 中的字段进行排序和分页
- javascript - 使用jQuery更改输入数字字段时防止提交表单
- javascript - 简单的 html 复制和粘贴问题 - 也复制按钮
- sql - plsql 中 IF 语句的替代选择 Into
- python - 多个熊猫数据框中的元素数学运算
- linux-device-driver - 在 Netdevice 中切换 GPIO
- google-cloud-platform - RPC API 创建服务:未找到请求的实体错误
- sql - SQL join 查找相关代码和地址