-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduled-actions.spec.ts
54 lines (48 loc) · 1.55 KB
/
scheduled-actions.spec.ts
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
import { CreateTestRunner } from "@digital-alchemy/hass/mock-assistant";
import dayjs from "dayjs";
import { UNIT_TESTING_APP } from "../application";
describe("Scheduled Actions", () => {
const runner = CreateTestRunner(UNIT_TESTING_APP);
afterEach(async () => {
await UNIT_TESTING_APP.teardown();
jest.restoreAllMocks();
jest.useRealTimers();
});
describe("Cron", () => {
beforeEach(() => {
jest.useFakeTimers();
});
it("turns on porch light at 8PM", async () => {
expect.assertions(1);
await runner(
() => {
// 7:59:59 PM
jest.setSystemTime(dayjs("2024-01-01 19:59:59").toDate());
jest.runOnlyPendingTimersAsync();
},
async ({ hass }) => {
const turnOn = jest.spyOn(hass.call.switch, "turn_on");
// move clock forward by 2 seconds
jest.advanceTimersByTime(2000);
expect(turnOn).toHaveBeenCalledWith({ entity_id: "switch.porch_light" });
},
);
});
it("turns off porch light at 5AM", async () => {
expect.assertions(1);
await runner(
() => {
// 4:59:59 AM
jest.setSystemTime(dayjs("2024-01-01 04:59:59").toDate());
jest.runOnlyPendingTimersAsync();
},
async ({ hass }) => {
const turnOn = jest.spyOn(hass.call.switch, "turn_off");
// move clock forward by 2 seconds
jest.advanceTimersByTime(2000);
expect(turnOn).toHaveBeenCalledWith({ entity_id: "switch.porch_light" });
},
);
});
});
});