From ee8ab3517aad04de15c18b85d6b710b98a4b6047 Mon Sep 17 00:00:00 2001 From: mdvanes <4253562+mdvanes@users.noreply.github.com> Date: Thu, 23 May 2024 08:14:37 +0200 Subject: [PATCH] chore: fix switches tests --- .../SwitchBarList/SwitchBarList.test.tsx | 6 ++ .../SwitchBarList_renderWith.test.tsx | 6 ++ .../src/switches/switches.controller.spec.ts | 14 +++- .../src/switches/switches.controller.ts | 78 ++++++++++--------- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList.test.tsx b/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList.test.tsx index fbfc829e..d30f41d8 100644 --- a/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList.test.tsx +++ b/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList.test.tsx @@ -21,6 +21,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, { idx: "4", @@ -29,6 +30,7 @@ const mockRootState: MockRootState = { status: "On", dimLevel: null, readOnly: false, + origin: "domoticz", children: [ { idx: "5", @@ -38,6 +40,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, ], }, @@ -49,6 +52,7 @@ const mockRootState: MockRootState = { dimLevel: 30, readOnly: true, children: false, + origin: "domoticz", }, { idx: "7", @@ -58,6 +62,7 @@ const mockRootState: MockRootState = { dimLevel: 30, readOnly: false, children: false, + origin: "domoticz", }, { idx: "8", @@ -67,6 +72,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, ], expanded: [], diff --git a/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList_renderWith.test.tsx b/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList_renderWith.test.tsx index b159b809..7ee62cd5 100644 --- a/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList_renderWith.test.tsx +++ b/apps/client/src/Components/Molecules/SwitchBarList/SwitchBarList_renderWith.test.tsx @@ -20,6 +20,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, { idx: "4", @@ -28,6 +29,7 @@ const mockRootState: MockRootState = { status: "On", dimLevel: null, readOnly: false, + origin: "domoticz", children: [ { idx: "5", @@ -37,6 +39,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, ], }, @@ -48,6 +51,7 @@ const mockRootState: MockRootState = { dimLevel: 30, readOnly: true, children: false, + origin: "domoticz", }, { idx: "7", @@ -57,6 +61,7 @@ const mockRootState: MockRootState = { dimLevel: 30, readOnly: false, children: false, + origin: "domoticz", }, { idx: "8", @@ -66,6 +71,7 @@ const mockRootState: MockRootState = { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, ], expanded: [], diff --git a/apps/server/src/switches/switches.controller.spec.ts b/apps/server/src/switches/switches.controller.spec.ts index a58dc072..6cb2d7a7 100644 --- a/apps/server/src/switches/switches.controller.spec.ts +++ b/apps/server/src/switches/switches.controller.spec.ts @@ -2,11 +2,16 @@ import { DomoticzTypeOptions } from "@homeremote/types"; import { ConfigService } from "@nestjs/config"; import { Test, TestingModule } from "@nestjs/testing"; import * as Got from "got"; +import { AuthenticatedRequest } from "../login/LoginRequest.types"; import { DomoticzSwitch, SwitchesController } from "./switches.controller"; jest.mock("got"); const gotSpy = jest.spyOn(Got, "default"); +const mockAuthenticatedRequest = { + user: { name: "someuser", id: 1 }, +} as AuthenticatedRequest; + describe("Switches Controller", () => { let controller: SwitchesController; let configService: ConfigService; @@ -42,7 +47,7 @@ describe("Switches Controller", () => { gotSpy.mockResolvedValue({ body: JSON.stringify({ status: "OK", result: [mockSwitch] }), }); - expect(await controller.getSwitches()).toEqual({ + expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({ status: "received", switches: [ { @@ -53,6 +58,7 @@ describe("Switches Controller", () => { dimLevel: null, readOnly: false, children: false, + origin: "domoticz", }, ], }); @@ -60,7 +66,7 @@ describe("Switches Controller", () => { it("returns error status on failed /GET ", async () => { gotSpy.mockRejectedValue("Mock Server Down"); - expect(await controller.getSwitches()).toEqual({ + expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({ status: "error", }); }); @@ -69,7 +75,7 @@ describe("Switches Controller", () => { gotSpy.mockResolvedValue({ body: JSON.stringify({ status: "error" }), }); - expect(await controller.getSwitches()).toEqual({ + expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({ status: "error", }); }); @@ -78,7 +84,7 @@ describe("Switches Controller", () => { gotSpy.mockResolvedValue({ body: "not json", }); - expect(await controller.getSwitches()).toEqual({ + expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({ status: "error", }); }); diff --git a/apps/server/src/switches/switches.controller.ts b/apps/server/src/switches/switches.controller.ts index 3f2302f4..e3e6a706 100644 --- a/apps/server/src/switches/switches.controller.ts +++ b/apps/server/src/switches/switches.controller.ts @@ -77,6 +77,7 @@ type GetChildren = ( SceneType: DomoticzType, SceneStatus: DomoticzStatus ) => Promise; + const getChildren: GetChildren = async ( domoticzuri, SceneIdx, @@ -149,45 +150,50 @@ export class SwitchesController { } getHaLightFavoritesAsSwitches = async (): Promise => { - const haFavoritesResponse = await fetch( - `${this.haApiConfig.baseUrl}/api/states/light.favorites`, - { - headers: { - Authorization: `Bearer ${this.haApiConfig.token}`, - }, - } - ); - const haFavoriteIds = - (await haFavoritesResponse.json()) as GetHaStatesResponse; + try { + const haFavoritesResponse = await fetch( + `${this.haApiConfig.baseUrl}/api/states/light.favorites`, + { + headers: { + Authorization: `Bearer ${this.haApiConfig.token}`, + }, + } + ); + const haFavoriteIds = + (await haFavoritesResponse.json()) as GetHaStatesResponse; - const haStatesPromises = haFavoriteIds.attributes.entity_id.map( - async (entity) => { - const haStateResponse = await fetch( - `${this.haApiConfig.baseUrl}/api/states/${entity}`, - { - headers: { - Authorization: `Bearer ${this.haApiConfig.token}`, - }, - } - ); - return (await haStateResponse.json()) as GetHaStatesResponse; - } - ); - const haStates = await Promise.all(haStatesPromises); + const haStatesPromises = haFavoriteIds.attributes.entity_id.map( + async (entity) => { + const haStateResponse = await fetch( + `${this.haApiConfig.baseUrl}/api/states/${entity}`, + { + headers: { + Authorization: `Bearer ${this.haApiConfig.token}`, + }, + } + ); + return (await haStateResponse.json()) as GetHaStatesResponse; + } + ); + const haStates = await Promise.all(haStatesPromises); - const haSwitches: HomeRemoteHaSwitch[] = - haStates.map((entity) => ({ - origin: "home-assistant", - dimLevel: null, - idx: entity.entity_id, - name: entity.attributes.friendly_name, - readOnly: false, - status: entity.state === "off" ? "Off" : "On", - type: "Light/Switch", - children: false, - })); + const haSwitches: HomeRemoteHaSwitch[] = + haStates.map((entity) => ({ + origin: "home-assistant", + dimLevel: null, + idx: entity.entity_id, + name: entity.attributes.friendly_name, + readOnly: false, + status: entity.state === "off" ? "Off" : "On", + type: "Light/Switch", + children: false, + })); - return haSwitches; + return haSwitches; + } catch (err) { + this.logger.error(`Can't get HaLightFavorites ${err}`); + return []; + } }; @UseGuards(JwtAuthGuard)