我已经通过css表单将样式应用于按钮。当应用程序启动时,按钮只有一个.button类。当按钮被点击时
.clicked
类,然后修改按钮的样式。按钮文本也从“button”更改为“button clicked”。
我希望RTL寻找这些样式的变化(在运行应用程序时这些变化是显而易见的)。
按钮.css:
.Button {
background-color: blueviolet;
}
.clicked {
transform: scale(8.0);
background-color: red;
}
测试代码:
import React from 'react';
import { cleanup, render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import App from './App';
describe('clicking on the button', () => {
afterAll(cleanup);
let upDatedButton: HTMLElement;
beforeAll(async () => {
const { getByText, findByText } = render(<App />);
const button = getByText('button');
fireEvent(
button,
new MouseEvent('click', {
bubbles: true,
cancelable: true
})
)
upDatedButton = await findByText('button clicked');
});
// the button has been click: it's text is now different
it('button text changes', async () => {
expect(upDatedButton).toBeInTheDocument();
expect(upDatedButton).toBeInstanceOf(HTMLButtonElement);
});
it('style changes after click', async () => {
expect(upDatedButton).toHaveClass('Button clicked'); //clicked class is picked up
// fails here: only style is background-color: ButtonFace
expect(upDatedButton).toHaveStyle(`
transform: scale(8.0);
background-color: red;
`);
});
});
toHaveStyle()
不是选择这些新的风格,而是完全不同的东西,一个背景色的“按钮面”:
expect(element).toHaveStyle()
- Expected
- background-color: red;
- transform: scale(8.0);
+ background-color: ButtonFace;
如何测试用户应该看到的样式?干杯!