在React Native中,除了使用快照测试,还可以使用其他方法来测试和测试组件
import React from 'react';
import TestRenderer from 'react-test-renderer';
import MyComponent from './MyComponent';
test('renders correctly', () => {
const testRenderer = TestRenderer.create(<MyComponent />);
const json = testRenderer.toJSON();
expect(json).toMatchSnapshot();
});
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyComponent from './MyComponent';
test('renders correctly', () => {
const { getByTestId } = render(<MyComponent />);
const button = getByTestId('my-button');
expect(button).toBeTruthy();
});
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyComponent from './MyComponent';
test('renders correctly', () => {
const { getByTestId } = render(<MyComponent />);
const button = getByTestId('my-button');
expect(button).toBeTruthy();
});
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
test('renders correctly', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.my-button')).toHaveLength(1);
});
这些方法都可以在不使用快照的情况下测试和测试React Native组件。您可以根据项目需求和团队熟悉程度选择最适合您的方法。
领取专属 10元无门槛券
手把手带您无忧上云