当我测试 Angular Material 小吃栏时,将持续时间设置为 3000 毫秒,然后在勾选 3000 后尝试使用 fakeAsync 进行测试,小吃栏仍然显示。如何让它自动关闭? 代码
您可以使用 Promise 来模拟
tick
并等待 3100ms,然后检查是否已解雇。
export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));
it('should open a snackbar with the provided message', async () => {
const message = 'Test message';
component.openSnackBar(message, 'Test Action');
const snackBar = await loader.getHarness(MatSnackBarHarness);
expect(await snackBar.getMessage()).toBe(message);
console.log(await snackBar.isDismissed());
await delay(3100);
console.log(await snackBar.isDismissed());
expect(await snackBar.isDismissed()).toBe(true);
});