Skip to content

Commit

Permalink
chore(test): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Oct 10, 2021
1 parent 9b454fa commit 3833ba9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 73 deletions.
23 changes: 13 additions & 10 deletions src/test/Document.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { Document, Imperial } from "../lib";
import { IMPERIAL_TOKEN, RESPONSE } from "./common";

const DAY = 86_400_000;
import { DAY, IMPERIAL_TOKEN, RESPONSE } from "./common";

describe("Document", () => {
const content = "test";
const client = new Imperial(IMPERIAL_TOKEN);
let document: Document;

beforeEach(() => {
document = new Document(client, { ...RESPONSE.document, content });
document = new Document(client, { ...RESPONSE.data, content });
});

it("should be valid", () => {
expect(document).toBeInstanceOf(Document);
expect(document.id).toBe(RESPONSE.document.documentId);
expect(document.link).toBe(`https://${client.rest.hostname}/p/${RESPONSE.document.documentId}`);
expect(document.id).toBe(RESPONSE.data.id);
expect(document.link).toBe(`https://${client.rest.hostname}/p/${RESPONSE.data.id}`);
expect(document.content).toBe(content);
expect(document.creation).toBeInstanceOf(Date);
expect(document.daysLeft).toBe(null);
expect(document.timestamps.creation).toBeInstanceOf(Date);
expect(document.timestamps.daysLeft).toBe(null);
});

it("should not be null", () => {
document = new Document(client, { ...document.toJSON(), expirationDate: new Date().valueOf() + DAY });
expect(document.daysLeft).not.toBe(null);
const json = document.toJSON();

document = new Document(client, {
...json,
timestamps: { ...json.timestamps, expiration: (new Date().valueOf() + DAY) / 1000 },
});
expect(document.timestamps.daysLeft).not.toBe(null);
});
});
33 changes: 22 additions & 11 deletions src/test/common.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
export const DAY = 86_400_000;

export const IMPERIAL_TOKEN = "IMPERIAL-00000000-0000-0000-0000-000000000000";

export const RESPONSE = {
success: true,
document: {
documentId: "bwxUUGyD",
language: "auto",
imageEmbed: false,
instantDelete: true,
creationDate: 1617463955786,
expirationDate: 1617895955786,
allowedEditors: [],
encrypted: false,
password: null,
public: true,
data: {
id: "HKnB",
content: "test",
views: 0,
links: {
raw: "https://staging-balls-api.impb.in/r/HKnB",
formatted: "https://staging-balls-api.impb.in/p/HKnB",
},
timestamps: {
creation: (new Date().valueOf() - 7 * DAY) / 1000,
expiration: (new Date().valueOf() - DAY) / 1000,
},
settings: {
language: "auto",
imageEmbed: false,
instantDelete: false,
encrypted: false,
password: "",
public: false,
editors: [],
},
},
};
26 changes: 13 additions & 13 deletions src/test/createDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,46 @@ describe("createDocument", () => {
});

it("should create a document - fully valid", async () => {
const document = await client.createDocument("i am a valid request");
const document = await client.document.create("i am a valid request");

expect(document.id).toBe(RESPONSE.document.documentId);
expect(document.public).toBe(RESPONSE.document.public);
expect(document.imageEmbed).toBe(RESPONSE.document.imageEmbed);
expect(document.id).toBe(RESPONSE.data.id);
expect(document.settings.public).toBe(RESPONSE.data.settings.public);
expect(document.settings.imageEmbed).toBe(RESPONSE.data.settings.imageEmbed);
});

it("should create a document - text not a string", async () => {
// @ts-expect-error
await expect(client.createDocument({})).resolves.toBeInstanceOf(Document);
await expect(client.document.create({})).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.createDocument([])).resolves.toBeInstanceOf(Document);
await expect(client.document.create([])).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.createDocument(12345)).resolves.toBeInstanceOf(Document);
await expect(client.document.create(12345)).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.createDocument(() => {})).resolves.toBeInstanceOf(Document);
await expect(client.document.create(() => {})).resolves.toBeInstanceOf(Document);
});

it("should fail to create a document - wrong second parameter", async () => {
const error = new TypeError(OPTIONS_WRONG_TYPE);

// @ts-expect-error
await expect(client.createDocument("STRING", "")).rejects.toThrow(error);
await expect(client.document.create("STRING", "")).rejects.toThrow(error);

// @ts-expect-error
await expect(client.createDocument("ARRAY", [])).rejects.toThrow(error);
await expect(client.document.create("ARRAY", [])).rejects.toThrow(error);

// @ts-expect-error
await expect(client.createDocument("NUMBER", 12345)).rejects.toThrow(error);
await expect(client.document.create("NUMBER", 12345)).rejects.toThrow(error);

// @ts-expect-error
await expect(client.createDocument("NULL", null)).rejects.toThrow(error);
await expect(client.document.create("NULL", null)).rejects.toThrow(error);
});

it("should fail to create a document - no data", async () => {
// @ts-expect-error
await expect(client.createDocument()).rejects.toThrow(new Error(NO_TEXT));
await expect(client.document.create()).rejects.toThrow(new Error(NO_TEXT));
});

afterEach(() => {
Expand Down
20 changes: 10 additions & 10 deletions src/test/deleteDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ import { ID_WRONG_TYPE, NO_ID, NO_TOKEN } from "../lib/errors/Messages";
import { IMPERIAL_TOKEN, RESPONSE } from "./common";
const fetchMock: typeof fetchMockJest = require("node-fetch");

describe("createDocument", () => {
describe("deleteDocument", () => {
let client: Imperial;

beforeEach(() => {
client = new Imperial(IMPERIAL_TOKEN);

fetchMock.delete(`${client.rest.api}/document/${RESPONSE.document.documentId}`, {
fetchMock.delete(`${client.rest.api}/document/${RESPONSE.data.id}`, {
body: { success: true },
headers: { "Content-Type": "application/json" },
});
});

it("should delete a document - fully valid", async () => {
await client.deleteDocument(RESPONSE.document.documentId);
await client.document.delete(RESPONSE.data.id);

await client.deleteDocument(new URL(`https://${client.rest.hostname}/p/${RESPONSE.document.documentId}`));
await client.document.delete(new URL(`https://${client.rest.hostname}/p/${RESPONSE.data.id}`));
});

it("should fail to delete a document - no token", async () => {
client.setApiToken(undefined);

await expect(client.deleteDocument(RESPONSE.document.documentId)).rejects.toThrow(new Error(NO_TOKEN));
await expect(client.document.delete(RESPONSE.data.id)).rejects.toThrow(new Error(NO_TOKEN));
});

it("should fail to delete a document - wrong id type", async () => {
const error = new Error(ID_WRONG_TYPE);

await expect(client.deleteDocument({})).rejects.toThrow(error);
await expect(client.document.delete({})).rejects.toThrow(error);

await expect(client.deleteDocument([])).rejects.toThrow(error);
await expect(client.document.delete([])).rejects.toThrow(error);

await expect(client.deleteDocument(12345)).rejects.toThrow(error);
await expect(client.document.delete(12345)).rejects.toThrow(error);

await expect(client.deleteDocument(() => {})).rejects.toThrow(error);
await expect(client.document.delete(() => {})).rejects.toThrow(error);
});

it("should fail to delete a document - no id", async () => {
// @ts-expect-error
await expect(client.deleteDocument()).rejects.toThrow(new Error(NO_ID));
await expect(client.document.delete()).rejects.toThrow(new Error(NO_ID));
});

afterEach(() => {
Expand Down
32 changes: 16 additions & 16 deletions src/test/editDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { ID_WRONG_TYPE, NO_ID, NO_TEXT, NO_TOKEN } from "../lib/errors/Messages"
import { IMPERIAL_TOKEN, RESPONSE } from "./common";
const fetchMock: typeof fetchMockJest = require("node-fetch");

describe("createDocument", () => {
describe("editDocument", () => {
let client: Imperial;

beforeEach(() => {
client = new Imperial(IMPERIAL_TOKEN);

fetchMock.patch(`${client.rest.api}/document`, (_: any, req: any) => {
if (JSON.parse(req.body).document !== RESPONSE.document.documentId)
if (JSON.parse(req.body).id !== RESPONSE.data.id)
return {
body: { success: false },
status: 400,
Expand All @@ -30,60 +30,60 @@ describe("createDocument", () => {
});

it("should edit a document - fully valid", async () => {
await client.editDocument(RESPONSE.document.documentId, "i am a valid edit");
await client.document.edit(RESPONSE.data.id, "i am a valid edit");

await client.editDocument(
new URL(`https://${client.rest.hostname}/p/${RESPONSE.document.documentId}`),
await client.document.edit(
new URL(`https://${client.rest.hostname}/p/${RESPONSE.data.id}`),
"i am a valid edit",
);
});

it("should edit a document - text not a string", async () => {
// @ts-expect-error
await expect(client.editDocument(RESPONSE.document.documentId, {})).resolves.toBeInstanceOf(Document);
await expect(client.document.edit(RESPONSE.data.id, {})).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.editDocument(RESPONSE.document.documentId, [])).resolves.toBeInstanceOf(Document);
await expect(client.document.edit(RESPONSE.data.id, [])).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.editDocument(RESPONSE.document.documentId, 12345)).resolves.toBeInstanceOf(Document);
await expect(client.document.edit(RESPONSE.data.id, 12345)).resolves.toBeInstanceOf(Document);

// @ts-expect-error
await expect(client.editDocument(RESPONSE.document.documentId, () => {})).resolves.toBeInstanceOf(Document);
await expect(client.document.edit(RESPONSE.data.id, () => {})).resolves.toBeInstanceOf(Document);
});

it("should fail to edit a document - no token", async () => {
client.setApiToken(undefined);

await expect(async () => {
await client.editDocument(RESPONSE.document.documentId, "i am a valid edit");
await client.document.edit(RESPONSE.data.id, "i am a valid edit");
}).rejects.toThrow(new Error(NO_TOKEN));
});

it("should fail to delete a document - no id", async () => {
// @ts-expect-error
await expect(client.editDocument()).rejects.toThrow(new Error(NO_ID));
await expect(client.document.edit()).rejects.toThrow(new Error(NO_ID));
});

it("should fail to edit a document - wrong type of id", async () => {
const error = new Error(ID_WRONG_TYPE);

// @ts-expect-error
await expect(client.editDocument({})).rejects.toThrow(error);
await expect(client.document.edit({})).rejects.toThrow(error);

// @ts-expect-error
await expect(client.editDocument([])).rejects.toThrow(error);
await expect(client.document.edit([])).rejects.toThrow(error);

// @ts-expect-error
await expect(client.editDocument(12345)).rejects.toThrow(error);
await expect(client.document.edit(12345)).rejects.toThrow(error);

// @ts-expect-error
await expect(client.editDocument(() => {})).rejects.toThrow(error);
await expect(client.document.edit(() => {})).rejects.toThrow(error);
});

it("should fail to delete a document - no text", async () => {
// @ts-expect-error
await expect(client.editDocument(RESPONSE.document.documentId)).rejects.toThrow(new Error(NO_TEXT));
await expect(client.document.edit(RESPONSE.data.id)).rejects.toThrow(new Error(NO_TEXT));
});

afterEach(() => {
Expand Down
20 changes: 10 additions & 10 deletions src/test/getDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ import { ID_WRONG_TYPE, NO_ID } from "../lib/errors/Messages";
import { IMPERIAL_TOKEN, RESPONSE } from "./common";
const fetchMock: typeof fetchMockJest = require("node-fetch");

describe("createDocument", () => {
describe("getDocument", () => {
let client: Imperial;

beforeEach(() => {
client = new Imperial(IMPERIAL_TOKEN);

fetchMock.get(`${client.rest.api}/document/${RESPONSE.document.documentId}`, {
body: { success: true },
fetchMock.get(`${client.rest.api}/document/${RESPONSE.data.id}`, {
body: RESPONSE,
headers: { "Content-Type": "application/json" },
});
});

it("should fetch a document - fully valid", async () => {
await client.getDocument(RESPONSE.document.documentId);
await client.document.get(RESPONSE.data.id);

await client.getDocument(new URL(`https://${client.rest.hostname}/p/${RESPONSE.document.documentId}`));
await client.document.get(new URL(`https://${client.rest.hostname}/p/${RESPONSE.data.id}`));
});

it("should fail to fetch a document - wrong id type", async () => {
const error = new Error(ID_WRONG_TYPE);

await expect(client.getDocument({})).rejects.toThrow(error);
await expect(client.document.get({})).rejects.toThrow(error);

await expect(client.getDocument([])).rejects.toThrow(error);
await expect(client.document.get([])).rejects.toThrow(error);

await expect(client.getDocument(12345)).rejects.toThrow(error);
await expect(client.document.get(12345)).rejects.toThrow(error);

await expect(client.getDocument(() => {})).rejects.toThrow(error);
await expect(client.document.get(() => {})).rejects.toThrow(error);
});

it("should fail to fetch a document - no id", async () => {
// @ts-expect-error
await expect(client.getDocument()).rejects.toThrow(new Error(NO_ID));
await expect(client.document.get()).rejects.toThrow(new Error(NO_ID));
});

afterEach(() => {
Expand Down
8 changes: 5 additions & 3 deletions src/test/purgeDocuments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const fetchMock: typeof fetchMockJest = require("node-fetch");

const numberDeleted = 420;

describe("createDocument", () => {
describe("purgeDocument", () => {
let client: Imperial;

beforeEach(() => {
Expand All @@ -26,15 +26,17 @@ describe("createDocument", () => {
});
});

it("should purge document - valid", async () => {
it.skip("should purge document - valid", async () => {
// @ts-ignore
const response = await client.purgeDocuments();

expect(response.numberDeleted).toBe(numberDeleted);
});

it("should not purge document - no token", async () => {
it.skip("should not purge document - no token", async () => {
client.setApiToken();

// @ts-ignore
await expect(client.purgeDocuments()).rejects.toThrowError(new Error(NO_TOKEN));
});

Expand Down

0 comments on commit 3833ba9

Please sign in to comment.