-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseInterval.cy.tsx
More file actions
109 lines (92 loc) · 3.73 KB
/
useInterval.cy.tsx
File metadata and controls
109 lines (92 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React from "react";
import { useInterval } from "../../src/lib/hooks/useInterval";
interface TestComponentProps {
callbackFunction: () => void | Promise<void>;
intervalValue: number;
autoStart: boolean;
}
function TestComponent({ callbackFunction, intervalValue, autoStart }: TestComponentProps) {
const result = useInterval({ callback: callbackFunction, interval: intervalValue, autoStart: autoStart });
return (
<>
<div data-testid="test-component">
Component using useInterval hook
<button onClick={result.startInterval}>Start interval</button>
<button onClick={result.stopInterval}>Stop interval</button>
</div>
</>
);
}
describe("useInterval Hook - Cypress Component Tests", () => {
it("Component mounts", () => {
const callbackSpy = cy
.spy(() => {
console.log("Hello World!");
})
.as("componentMountSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={false} />);
cy.get('[data-testid="test-component"]').should("be.visible");
cy.get('[data-testid="test-component"]').should("contain.text", "Component using useInterval hook");
});
it("should not start the interval when autostart is set on false", () => {
const callbackSpy = cy
.spy(() => {
console.log("Does not start the interval automaticly");
})
.as("callbackSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={false} />);
cy.wait(3000);
cy.get("@callbackSpy").should("not.have.been.called");
});
it("should remain stopped until start is called when autostart is false", () => {
const callbackSpy = cy
.spy(() => {
console.log("Does not start the interval automaticly");
})
.as("callbackSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={false} />);
cy.wait(3000);
cy.get("@callbackSpy").should("not.have.been.called");
cy.contains("button", "Start interval").click();
cy.wait(1000);
cy.contains("button", "Stop interval").click();
cy.wait(3000);
cy.get("@callbackSpy").should("have.been.calledOnce");
});
it("should start the interval automatically when autostart is true", () => {
const callbackSpy = cy
.spy(() => {
console.log("The interval runs automatically when autostart is true.");
})
.as("callbackSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={true} />);
cy.wait(3000);
cy.get("@callbackSpy").should("have.been.calledThrice");
});
it("should be running continuously until stopped when autostart is true", () => {
const callbackSpy = cy
.spy(() => {
console.log("The interval runs automatically until it gets stopped.");
})
.as("callbackSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={true} />);
cy.wait(1000);
cy.get("@callbackSpy").should("have.been.calledOnce");
cy.contains("button", "Stop interval").click();
cy.get("@callbackSpy").should("have.been.calledOnce");
});
it("should only be possible to start the interval once", () => {
const callbackSpy = cy
.spy(() => {
console.log("The interval can only be started once.");
})
.as("callbackSpy");
cy.mount(<TestComponent callbackFunction={callbackSpy} intervalValue={1000} autoStart={false} />);
cy.contains("button", "Start interval").click();
cy.contains("button", "Start interval").click();
cy.get("@callbackSpy").should("have.been.calledOnce");
cy.wait(1000);
cy.contains("button", "Stop interval").click();
cy.get("@callbackSpy").should("have.been.calledTwice");
});
});