-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaf5c6e
commit 904657e
Showing
4 changed files
with
378 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.