我有一个输入组件。它可以以6种不同的方式出现和表现:
输入为:
我不想使用每个测试重复6次,而是使用describe.each()而不是简单的describe()。
输入也会出现,并且根据用户与输入的交互行为而有所不同。
例如,所有用户将以以下流程开始:
然后,用户可以遵循以下流程:
或其他流程:
或其他流程:
与其单独测试每个步骤,不如不进行清理来设置测试,以便我可以为流程的每个阶段嵌套测试,这会更好吗?
例如:
describe.each`...`("Initial render", ({...}) => {
it("renders as snapshot", () => ....)
describe("Then users focuses in", () => {
fireEvent.focusIn(input)
it("renders as snapshot", () => ....)
describe("Then user enters valid input", () => {
fireEvent.change(input, { target: { value: "Valid input" }})
it("renders as snapshot", () => ....)
describe("Then user focuses out", () => {
fireEvent.focusOut(input)
it("renders as snapshot", () => ....)
describe("Then user enters an invalid or no input", () => {
fireEvent.focusIn(input)
fireEvent.change(input, { target: { value: "" }})
it("renders as snapshot", () => ....)
})
})
})
describe("Then user enters an invalid input", () => {
fireEvent.change(input, { target: { value: "$%^@!" }})
it("renders as snapshot", () => ....)
describe("Then user focuses out", () => {
fireEvent.focusOut(input)
it("renders as snapshot", () => ....)
describe("Then user enters an valid input", () => {
fireEvent.focusIn(input)
fireEvent.change(input, { target: { value: "Valid input" }})
it("renders as snapshot", () => ....)
})
})
})
describe("Then user doesn't enter any input and focuses out", () => {
fireEvent.focusOut(input)
it("renders as snapshot", () => ....)
})
})
}
如果我这样做,如何正确设置和拆卸每个测试,即使用beforeEach(cleanup)
,afterEach(cleanup)
,beforeAll(cleanup)
和/或afterAll(cleanup)
,它与describe.each()一起使用?