我有一个组件。它有一个按钮。按下按钮后,我使用 setState 函数更改按钮文本(颜色)的样式。当我测试更改的组件时,测试失败,因为更改是异步发生的。我想做一些这里给出的事情(https://testing-library.com/docs/dom-testing-library/api-async/)
const button = screen.getByRole('button', { name: 'Click Me' })
fireEvent.click(button)
await screen.findByText('Clicked once')
fireEvent.click(button)
await screen.findByText('Clicked twice')
Run Code Online (Sandbox Code Playgroud)
但不是等待文本更改。我想等待文字颜色改变。谢谢
这是我的按钮的代码
<Button onPress = {() => {this.setState({state : 1});}}>
<Text style = {style}>Button Text</Text>
</Button>
Run Code Online (Sandbox Code Playgroud)
所以当这个按钮被按下时。state 设置为 1。并且在 render 中:
if(this.state.state === 1) style = style1
else style = style2;
Run Code Online (Sandbox Code Playgroud)
但从日志中可以看出,render是在测试检查样式后调用的。那么如何等待渲染完成后再检查字体颜色是否已更改?
这是测试代码
test('The button text style changes after press', () => {
const {getByText} = render(<Component/>);
fireEvent.press(getByText('button'));
expect(getByText('button')).toHaveStyle({
color : '#ffffff'
});
})
Run Code Online (Sandbox Code Playgroud) 当我从标准输入读取时,只有备用位置ArrayList占用数据.输入数据是:
1
2
3
4
5
6
7
8
9
0
Run Code Online (Sandbox Code Playgroud)
我在多个地方尝试了这个,但我仍然得到相同的输出.
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> strings= new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 10; i++) {
strings.add(null);
}
for (int i = 0; i < 10; i++) {
strings.add(9-i, br.readLine());
}
for (int i = 0; i < 10; i++) {
System.out.println(strings.get(i));
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果我期待:
0
9
8
7 …Run Code Online (Sandbox Code Playgroud) 这是代码,
public class Solution {
public static void main(String[] args) {
compare(5);
}
public static void compare(int a) {
if(a==5)
System.out.println("The number is equal to 5");
if(a<5)
System.out.println("The number is less than 5");
else
System.out.println("The number is greater than 5");
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出,
The number is equal to 5
The number is greater than 5
Run Code Online (Sandbox Code Playgroud)
我刚刚调用了compare方法一次,为什么它执行两次?