Skip to content

Commit

Permalink
refactor: use as esm module to allow latest version of node-fetch to …
Browse files Browse the repository at this point in the history
…be used
  • Loading branch information
jvandenaardweg committed Dec 5, 2022
1 parent 371d429 commit 7496aca
Show file tree
Hide file tree
Showing 11 changed files with 1,242 additions and 103 deletions.
1,185 changes: 1,153 additions & 32 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"displayName": "Homebridge HomeWizard Energy Socket",
"name": "homebridge-homewizard-energy-socket",
"type": "module",
"version": "1.0.9",
"description": "This Homebridge plugin exposes your HomeWizard Energy Sockets to Apple HomeKit. So you can use the Home App to switch your Energy Sockets on or off and integrate the Energy Sockets into your Home Automations.",
"license": "MIT",
Expand All @@ -16,15 +17,16 @@
"homebridge": ">=1.3.5"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint src/**.ts --max-warnings=0",
"watch": "npm run build && npm link && nodemon",
"build": "rimraf ./dist && tsc && resolve-tspaths",
"prepublishOnly": "npm run lint && npm run build",
"homebridge": "homebridge -D",
"update": "npx npm-check-updates --interactive",
"test": "jest",
"test:watch": "jest --watch",
"test": "vitest run",
"test:watch": "vitest",
"release": "dotenv npx release-it",
"type-check": "tsc --noEmit",
"prepare": "is-ci || husky install",
Expand Down Expand Up @@ -61,11 +63,12 @@
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"typescript": "^4.9.3",
"vitest": "^0.25.3"
},
"dependencies": {
"bonjour-service": "^1.0.14",
"node-fetch": "^2.6.7"
"node-fetch": "^3.1.0"
},
"release-it": {
"git": {
Expand Down
46 changes: 11 additions & 35 deletions src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,33 @@ import { mockApiUrl } from "./mocks/api-url";

const mockLoggerPrefix = "test logger prefix";

const newApi = () => {
return new HomeWizardApi(mockApiUrl, mockLoggerPrefix, loggerMock);
};

describe("HomeWizardApi", () => {
it("should be able to create a new instance", () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);
const homeWizardApi = newApi();

expect(homeWizardApi).toBeTruthy();
});

it('should GET the "basic" endpoint', async () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);

const homeWizardApi = newApi();
const basicInformation = await homeWizardApi.getBasicInformation();

expect(basicInformation).toStrictEqual(mockBasicInformationResponse);
});

it('should GET the "state" endpoint', async () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);

const homeWizardApi = newApi();
const state = await homeWizardApi.getState();

expect(state).toStrictEqual(mockStateResponse);
});

it('should PUT the "state" endpoint', async () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);

const homeWizardApi = newApi();
const updatedPowerOn = true;

const state = await homeWizardApi.putState({
Expand All @@ -58,12 +44,7 @@ describe("HomeWizardApi", () => {
});

it('should PUT the "identify" endpoint', async () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);

const homeWizardApi = newApi();
const firmwareVersion = 3;

const identify = await homeWizardApi.putIdentify(firmwareVersion);
Expand All @@ -72,12 +53,7 @@ describe("HomeWizardApi", () => {
});

it('should error when firmware version on PUT "identify" endpoint is too low', async () => {
const homeWizardApi = new HomeWizardApi(
mockApiUrl,
mockLoggerPrefix,
loggerMock
);

const homeWizardApi = newApi();
const firmwareVersion = 2;

const identifyFn = async () => homeWizardApi.putIdentify(firmwareVersion);
Expand Down
1 change: 0 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Logger } from "homebridge";
import fetch, { Response } from "node-fetch";
import {
HomeWizardApiBasicInformationResponse,
HomeWizardApiErrorResponse,
HomeWizardApiIdentifyResponse,
HomeWizardApiStatePutParams,
HomeWizardApiStatePutResponse,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { API } from "homebridge";
import { PLATFORM_NAME, PLUGIN_NAME } from "@/settings";
import { HomebridgeHomeWizardEnergySocket } from "@/platform";

export = (api: API) => {
export default (api: API) => {
api.registerPlatform(
PLUGIN_NAME,
PLATFORM_NAME,
Expand Down
2 changes: 1 addition & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Characteristic,
APIEvent,
} from "homebridge";
import Bonjour, { Service as BonjourService } from "bonjour-service";
import { Bonjour, Service as BonjourService } from "bonjour-service";

import { PLATFORM_NAME, PLUGIN_NAME } from "@/settings";
import { EnergySocketAccessory } from "@/energySocketAccessory";
Expand Down
11 changes: 6 additions & 5 deletions src/tests/mocks/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Logger } from "homebridge";
import { vi } from "vitest";

export const loggerMock = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
log: jest.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
log: vi.fn(),
} satisfies Logger;
43 changes: 22 additions & 21 deletions src/tests/platform.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import { HomebridgeAPI } from "homebridge/lib/api";
import { HomebridgeHomeWizardEnergySocket } from "../platform";
import { PLATFORM_NAME } from "../settings";
import { TxtRecord } from "../api/types";
import { loggerMock } from "./mocks/logger";
import { vi } from "vitest";

jest.mock("bonjour-service");

// mock the config parameter
const configMock = {
platform: PLATFORM_NAME,
};

const apiMock = new HomebridgeAPI();

const platform = new HomebridgeHomeWizardEnergySocket(
loggerMock,
configMock,
apiMock
);
vi.mock("bonjour-service");

describe("platform", () => {
it('should return true if api_enabled is "1"', () => {
const txtRecordMock = {
api_enabled: "1",
} as TxtRecord;

expect(platform.isDeviceApiEnabled(txtRecordMock)).toBe(true);
expect(
HomebridgeHomeWizardEnergySocket.prototype.isDeviceApiEnabled(
txtRecordMock
)
).toBe(true);
});

it('should return false if api_enabled is not "1"', () => {
const txtRecordMock = {
api_enabled: "0",
} as TxtRecord;

expect(platform.isDeviceApiEnabled(txtRecordMock)).toBe(false);
expect(
HomebridgeHomeWizardEnergySocket.prototype.isDeviceApiEnabled(
txtRecordMock
)
).toBe(false);
});

it('should return true if product_type is "HWE-SKT"', () => {
const txtRecordMock = {
product_type: "HWE-SKT",
} as TxtRecord;

expect(platform.isDeviceProductTypeSupported(txtRecordMock)).toBe(true);
expect(
HomebridgeHomeWizardEnergySocket.prototype.isDeviceProductTypeSupported(
txtRecordMock
)
).toBe(true);
});

it('should return false if product_type is not "HWE-SKT"', () => {
const txtRecordMock = {
product_type: "something-else",
} as TxtRecord;

expect(platform.isDeviceProductTypeSupported(txtRecordMock)).toBe(false);
expect(
HomebridgeHomeWizardEnergySocket.prototype.isDeviceProductTypeSupported(
txtRecordMock
)
).toBe(false);
});
});
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "ES2018", // ~node10
"module": "commonjs",
"target": "ES6",
"module": "ES6",
"lib": ["es2015", "es2016", "es2017", "es2018"],
"declaration": true,
"declarationMap": true,
Expand All @@ -28,6 +28,6 @@
"@/*": ["src/*"]
}
},
"include": ["src/", "jest.setup.js"],
"include": ["src/", "jest.setup.js", "vitest.setup.ts"],
"exclude": ["src/**/*.test.ts", "src/tests/**/*"]
}
18 changes: 18 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from "vitest/config";
import path from "path";

export default defineConfig({
test: {
globals: true,
environment: "node",
setupFiles: ["./vitest.setup.ts"],
},
resolve: {
alias: [
{
find: "@",
replacement: path.resolve(__dirname, "src"),
},
],
},
});
20 changes: 20 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { server } from "src/api/mocks/server";
import { vi } from "vitest";

beforeAll(() => {
// Establish API mocking before all tests.
server.listen();
});

afterEach(() => {
// Reset any MSW request handlers that we may add during the tests,
// so they don't affect other tests.
server.resetHandlers();

vi.clearAllMocks();
});

afterAll(() => {
// Clean up after the tests are finished.
server.close();
});

0 comments on commit 7496aca

Please sign in to comment.