Skip to content

Commit

Permalink
✅ Introduce unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Dec 2, 2024
1 parent aaf5c6e commit 904657e
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 8 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha --require ts-node/register src/*.test.ts",
"lint": "prettier --check src docs .github *.md",
"format": "prettier --write src docs .github *.md",
"build": "tsc"
Expand All @@ -30,7 +30,11 @@
},
"devDependencies": {
"@aws-amplify/cli": "^7.6.21",
"@types/mocha": "^10.0.10",
"@types/sinon": "^17.0.3",
"mocha": "^10.8.2",
"prettier": "^2.5.1",
"sinon": "^19.0.2",
"ts-node": "^10.9.1",
"typedoc": "^0.22.12"
}
Expand Down
93 changes: 93 additions & 0 deletions src/library.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { strict as assert } from "assert";
import sinon from "sinon";
import { Amplify, Auth } from "aws-amplify";
import axios from "axios";
import { signIn, configure } from "../src/library";

describe("library", () => {
let axiosStub: sinon.SinonStub;

beforeEach(() => {
axiosStub = sinon.stub(axios, "create").returns({
get: sinon.stub(),
put: sinon.stub(),
} as any);
});

afterEach(() => {
sinon.restore();
});

describe("signIn", () => {
it("should return a JWT token on successful login", async () => {
const expectedJwtToken = "mockToken123";
const mockUserSession = {
getAccessToken: () => ({ jwtToken: expectedJwtToken }),
};
const mockUser = {
getSignInUserSession: () => mockUserSession,
};
const signInStub = sinon.stub(Auth, "signIn").resolves(mockUser);
const jwtToken = await signIn("mockUser", "mockPassword");
assert.ok(signInStub.calledOnce);
assert.equal(jwtToken, expectedJwtToken);
});
});

describe("configure", () => {
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(Object.keys(api), [
"deviceInfo",
"setPower",
"setPowerOff",
"setPowerOn",
]);
});
});

describe("API Methods", () => {
it("should call axios for deviceInfo", async () => {
const mockAxios = {
get: sinon
.stub()
.resolves({ data: { id: "123", name: "Mock Device" } }),
};
axiosStub.returns(mockAxios as any);
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" });
});

it("should call axios for setPowerOn", async () => {
const mockAxios = {
put: sinon.stub().resolves({ status: 200 }),
};
axiosStub.returns(mockAxios as any);
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" },
});
assert.equal(result.status, 200);
});
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"declaration": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"preserveConstEnums": true
},
"include": [
Expand Down
Loading

0 comments on commit 904657e

Please sign in to comment.