Skip to content

Commit

Permalink
✅ setPowerOff() test coverage
Browse files Browse the repository at this point in the history
And other minor tests refactoring
  • Loading branch information
AndreMiras committed Dec 4, 2024
1 parent 4682a25 commit d89c8ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
89 changes: 56 additions & 33 deletions src/library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<typeof configure>) =>
api.setPowerOn("mockToken", "mockMacAddress"),
expectedValue: 1,
},
{
method: "setPowerOff",
call: (api: ReturnType<typeof configure>) =>
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);
});
});
});

0 comments on commit d89c8ab

Please sign in to comment.