Skip to content

Commit

Permalink
Add more tests for client
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 21, 2023
1 parent d5efacb commit 4fc4d14
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
75 changes: 75 additions & 0 deletions tests/common/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import nock from "nock";

import { ApitallyClient } from "../../src/common/client";
import { APITALLY_HUB_BASE_URL, CLIENT_ID, ENV } from "../utils";

describe("Client", () => {
beforeAll(() => {
nock(APITALLY_HUB_BASE_URL)
.persist()
.post(/\/requests$/)
.reply(202);
});

it("Argument validation on instantiation", () => {
expect(() => new ApitallyClient({ clientId: "xxx" })).toThrow("xxx");
expect(
() => new ApitallyClient({ clientId: CLIENT_ID, env: "..." }),
).toThrow("...");
});

it("Singleton instantiation", () => {
expect(() => ApitallyClient.getInstance()).toThrow("not initialized");
expect(() => {
new ApitallyClient({ clientId: CLIENT_ID, env: ENV });
new ApitallyClient({ clientId: CLIENT_ID, env: "other" });
}).toThrow("already initialized");
});

it("Stop sync if client ID is invalid", async () => {
nock(APITALLY_HUB_BASE_URL)
.persist()
.post(/\/(info|requests)$/)
.reply(404, `Client ID '${CLIENT_ID}' not found`);

const client = new ApitallyClient({
clientId: CLIENT_ID,
env: ENV,
});
jest.spyOn(client.logger, "error").mockImplementation(() => {});
client.setAppInfo({ paths: [], versions: new Map(), client: "js:test" });

await new Promise((resolve) => setTimeout(resolve, 10));
expect(client["syncIntervalId"]).toBeUndefined();
});

it("Exit if initial API key sync fails", async () => {
nock(APITALLY_HUB_BASE_URL)
.persist()
.post(/\/info$/)
.reply(202);
nock(APITALLY_HUB_BASE_URL)
.persist()
.get(/\/keys$/)
.reply(400);

const client = new ApitallyClient({
clientId: CLIENT_ID,
env: ENV,
syncApiKeys: true,
});
jest.spyOn(client.logger, "error").mockImplementation(() => {});
const exitSpy = jest
.spyOn(process, "exit")
.mockImplementation(() => undefined as never);

await new Promise((resolve) => setTimeout(resolve, 10));
expect(exitSpy).toHaveBeenCalledWith(1);
});

afterEach(async () => {
try {
await ApitallyClient.getInstance().handleShutdown();
} catch (error) {} // eslint-disable-line no-empty
});
});
10 changes: 5 additions & 5 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import nock from "nock";

const APITALLY_HUB_BASE_URL = "https://hub.apitally.io";
const SALT = "54fd2b80dbfeb87d924affbc91b77c76";
const API_KEY_HASH =
"bcf46e16814691991c8ed756a7ca3f9cef5644d4f55cd5aaaa5ab4ab4f809208";

export const APITALLY_HUB_BASE_URL = "https://hub.apitally.io";
export const CLIENT_ID = "fa4f144d-33be-4694-95e4-f5c18b0f151d";
export const ENV = "default";
export const API_KEY = "7ll40FB.DuHxzQQuGQU4xgvYvTpmnii7K365j9VI";

const SALT = "54fd2b80dbfeb87d924affbc91b77c76";
const API_KEY_HASH =
"bcf46e16814691991c8ed756a7ca3f9cef5644d4f55cd5aaaa5ab4ab4f809208";

export const mockApitallyHub = () => {
nock(APITALLY_HUB_BASE_URL)
.persist()
Expand Down

0 comments on commit 4fc4d14

Please sign in to comment.