我想将键和值动态映射list到obj. 但是,TS 给了我一条错误消息:
Type 'string | number' is not assignable to type 'never'
我不知道出了什么问题。下面是代码片段:
interface T {
// uncomment the next line makes the error go away
// [k: string]: any
a: string;
b?: string;
c?: number;
}
const obj: T = {
a: 'something',
};
const list: Array<{
foo: keyof T;
bar: string | number;
}> = [
{ foo: 'b', bar: 'str' },
{ foo: 'c', bar: 1 },
];
list.forEach(item => {
const …Run Code Online (Sandbox Code Playgroud) 我正在尝试为使用 Formik 构建的 React Native 组件编写一些测试。这是一个简单的表单,要求输入用户名和密码,我想使用使用 Yup 构建的验证模式。
当我使用模拟器并手动测试表单时,表单的行为符合预期,仅当输入值无效时才会显示错误消息。
但是,当我尝试使用 编写一些自动化测试时@testing-library/react-native,其行为并不是我所期望的。即使提供的值有效,错误消息也会在测试中显示。下面是代码:
// App.test.js
import React from 'react';
import { render, act, fireEvent } from '@testing-library/react-native';
import App from '../App';
it('does not show error messages when input values are valid', async () => {
const {
findByPlaceholderText,
getByPlaceholderText,
getByText,
queryAllByText,
} = render(<App />);
const usernameInput = await findByPlaceholderText('Username');
const passwordInput = getByPlaceholderText('Password');
const submitButton = getByText('Submit');
await act(async () => {
fireEvent.changeText(usernameInput, 'testUser');
fireEvent.changeText(passwordInput, 'password');
fireEvent.press(submitButton);
});
expect(queryAllByText('This …Run Code Online (Sandbox Code Playgroud) reactjs react-native yup formik react-native-testing-library
formik ×1
javascript ×1
react-native ×1
react-native-testing-library ×1
reactjs ×1
typescript ×1
yup ×1