diff --git a/README.md b/README.md index 74c80dc..c2e3195 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Tests](https://github.com/AndreMiras/edilkamin.js/workflows/Tests/badge.svg)](https://github.com/AndreMiras/edilkamin.js/actions/workflows/tests.yml) [![CLI Tests](https://github.com/AndreMiras/edilkamin.js/actions/workflows/cli-tests.yml/badge.svg)](https://github.com/AndreMiras/edilkamin.js/actions/workflows/cli-tests.yml) -[![codecov](https://codecov.io/gh/AndreMiras/edilkamin.js/graph/badge.svg?token=YG3LKXNZWU)](https://codecov.io/gh/AndreMiras/edilkamin.js) +[![codecov](https://codecov.io/gh/AndreMiras/edilkamin.js/graph/badge.svg?token=YG3LKXNZWU)](https://app.codecov.io/gh/AndreMiras/edilkamin.js/tree/main) [![Documentation](https://github.com/AndreMiras/edilkamin.js/workflows/Documentation/badge.svg)](https://github.com/AndreMiras/edilkamin.js/actions/workflows/documentation.yml) [![npm version](https://badge.fury.io/js/edilkamin.svg)](https://badge.fury.io/js/edilkamin) diff --git a/src/library.test.ts b/src/library.test.ts index 6a7e8a3..549be9c 100644 --- a/src/library.test.ts +++ b/src/library.test.ts @@ -81,8 +81,13 @@ describe("library", () => { it("should create API methods with the correct baseURL", () => { const baseURL = "https://example.com/api"; const api = configure(baseURL); - assert.ok(axiosStub.calledOnce); - assert.deepEqual(axiosStub.firstCall.args[0], { baseURL }); + assert.deepEqual(axiosStub.args, [ + [ + { + baseURL, + }, + ], + ]); assert.deepEqual(Object.keys(api), [ "deviceInfo", "setPower", @@ -94,44 +99,62 @@ describe("library", () => { describe("API Methods", () => { it("should call axios for deviceInfo", async () => { + const expectedDevice = { id: "123", name: "Mock Device" }; + const expectedToken = "mockToken"; const mockAxios = { - get: sinon - .stub() - .resolves({ data: { id: "123", name: "Mock Device" } }), + get: sinon.stub().resolves({ data: expectedDevice }), }; - axiosStub.returns(mockAxios); const api = configure("https://example.com/api"); - const result = await api.deviceInfo("mockToken", "mockMacAddress"); - assert.ok(mockAxios.get.calledOnce); - assert.equal( - mockAxios.get.firstCall.args[0], - "device/mockMacAddress/info" - ); - assert.deepEqual(mockAxios.get.firstCall.args[1], { - headers: { Authorization: "Bearer mockToken" }, - }); - assert.deepEqual(result.data, { id: "123", name: "Mock Device" }); + const result = await api.deviceInfo(expectedToken, "mockMacAddress"); + assert.deepEqual(mockAxios.get.args, [ + [ + "device/mockMacAddress/info", + { headers: { Authorization: `Bearer ${expectedToken}` } }, + ], + ]); + assert.deepEqual(result.data, expectedDevice); }); - it("should call axios for setPowerOn", async () => { - const mockAxios = { - put: sinon.stub().resolves({ status: 200 }), - }; - axiosStub.returns(mockAxios); - const api = configure("https://example.com/api"); - const result = await api.setPowerOn("mockToken", "mockMacAddress"); - assert.ok(mockAxios.put.calledOnce); - assert.equal(mockAxios.put.firstCall.args[0], "mqtt/command"); - assert.deepEqual(mockAxios.put.firstCall.args[1], { - mac_address: "mockMacAddress", - name: "power", - value: 1, - }); - assert.deepEqual(mockAxios.put.firstCall.args[2], { - headers: { Authorization: "Bearer mockToken" }, + // Tests for setPowerOn and setPowerOff + [ + { + method: "setPowerOn", + call: (api: ReturnType) => + api.setPowerOn("mockToken", "mockMacAddress"), + expectedValue: 1, + }, + { + method: "setPowerOff", + call: (api: ReturnType) => + api.setPowerOff("mockToken", "mockMacAddress"), + expectedValue: 0, + }, + ].forEach(({ method, call, expectedValue }) => { + it(`should call axios for ${method}`, async () => { + const mockAxios = { + put: sinon.stub().resolves({ status: 200 }), + }; + axiosStub.returns(mockAxios); + const api = configure("https://example.com/api"); + + // Invoke the method using the mapped call function + const result = await call(api); + assert.deepEqual(mockAxios.put.args, [ + [ + "mqtt/command", + { + mac_address: "mockMacAddress", + name: "power", + value: expectedValue, + }, + { + headers: { Authorization: "Bearer mockToken" }, + }, + ], + ]); + assert.equal(result.status, 200); }); - assert.equal(result.status, 200); }); }); });