Skip to content

Commit

Permalink
VUU-27 improvements to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vferraro-scottlogic committed Oct 20, 2023
1 parent 18ec1cf commit 847f7c1
Showing 1 changed file with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,35 @@ const metadata: LayoutMetadata[] = [
const metadataToAdd: Omit<LayoutMetadata, "id" | "created"> = {
name: "layout 1",
group: "group 1",
screenshot: "",
screenshot: "screenshot",
user: "username",
};

const layout: LayoutJSON = {
type: "",
type: "View",
};

const uniqueId = uuidv4();
const dateString = new Date().toISOString();
const fetchError = new Error("Something went wrong with your request");

type FetchResponse<T> = {
json?: () => Promise<T>;
ok: boolean;
statusText?: string;
};

type CreateLayoutResponseJSON = {
metadata: LayoutMetadata;
};

describe("RemoteLayoutPersistenceManager", () => {
beforeEach(() => {
vi.clearAllMocks();
});

describe("createLayout", () => {
const responseJSON = {
const responseJSON: CreateLayoutResponseJSON = {
metadata: {
...metadataToAdd,
id: uniqueId,
Expand All @@ -51,7 +61,7 @@ describe("RemoteLayoutPersistenceManager", () => {
};

it("resolves with metadata when fetch resolves, response is ok and contains metadata", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<CreateLayoutResponseJSON> = {
json: () => new Promise((resolve) => resolve(responseJSON)),
ok: true,
};
Expand All @@ -66,7 +76,7 @@ describe("RemoteLayoutPersistenceManager", () => {
it("rejects with error when response is not ok", () => {
const errorMessage = "Not Found";

const fetchResponse = {
const fetchResponse: FetchResponse<CreateLayoutResponseJSON> = {
json: () => new Promise((resolve) => resolve(responseJSON)),
ok: false,
statusText: errorMessage,
Expand All @@ -81,7 +91,7 @@ describe("RemoteLayoutPersistenceManager", () => {
});

it("rejects with error when metadata in response is falsey", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<object> = {
json: () => new Promise((resolve) => resolve({})),
ok: true,
};
Expand All @@ -106,7 +116,7 @@ describe("RemoteLayoutPersistenceManager", () => {

describe("updateLayout", () => {
it("resolves when fetch resolves and response is ok", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<void> = {
ok: true,
};

Expand All @@ -120,7 +130,7 @@ describe("RemoteLayoutPersistenceManager", () => {
it("rejects with error when response is not ok", () => {
const errorMessage = "Not Found";

const fetchResponse = {
const fetchResponse: FetchResponse<void> = {
ok: false,
statusText: errorMessage,
};
Expand All @@ -145,7 +155,7 @@ describe("RemoteLayoutPersistenceManager", () => {

describe("deleteLayout", () => {
it("resolves when fetch resolves and response is ok", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<void> = {
ok: true,
};

Expand All @@ -159,7 +169,7 @@ describe("RemoteLayoutPersistenceManager", () => {
it("rejects with error when response is not ok", () => {
const errorMessage = "Not Found";

const fetchResponse = {
const fetchResponse: FetchResponse<void> = {
ok: false,
statusText: errorMessage,
};
Expand All @@ -184,7 +194,7 @@ describe("RemoteLayoutPersistenceManager", () => {

describe("loadMetadata", () => {
it("resolves with array of metadata when response is ok", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<LayoutMetadata[]> = {
json: () => new Promise((resolve) => resolve(metadata)),
ok: true,
};
Expand All @@ -199,8 +209,8 @@ describe("RemoteLayoutPersistenceManager", () => {
it("rejects with error when response is not ok", () => {
const errorMessage = "Not Found";

const fetchResponse = {
json: () => new Promise((resolve) => resolve(undefined)),
const fetchResponse: FetchResponse<void> = {
json: () => new Promise((resolve) => resolve()),
ok: false,
statusText: errorMessage,
};
Expand All @@ -214,8 +224,8 @@ describe("RemoteLayoutPersistenceManager", () => {
});

it("rejects with error when metadata is falsey in response", () => {
const fetchResponse = {
json: () => new Promise((resolve) => resolve(undefined)),
const fetchResponse: FetchResponse<void> = {
json: () => new Promise((resolve) => resolve()),
ok: true,
};

Expand All @@ -239,7 +249,7 @@ describe("RemoteLayoutPersistenceManager", () => {

describe("loadLayout", () => {
it("resolves with array of metadata when response is ok", () => {
const fetchResponse = {
const fetchResponse: FetchResponse<LayoutJSON> = {
json: () => new Promise((resolve) => resolve(layout)),
ok: true,
};
Expand All @@ -254,8 +264,8 @@ describe("RemoteLayoutPersistenceManager", () => {
it("rejects with error when response is not ok", () => {
const errorMessage = "Not Found";

const fetchResponse = {
json: () => new Promise((resolve) => resolve(undefined)),
const fetchResponse: FetchResponse<void> = {
json: () => new Promise((resolve) => resolve()),
ok: false,
statusText: errorMessage,
};
Expand All @@ -269,9 +279,8 @@ describe("RemoteLayoutPersistenceManager", () => {
});

it("rejects with error when metadata is falsey in response", () => {
const fetchResponse = {
json: () =>
new Promise((resolve: (value?: unknown) => void) => resolve()),
const fetchResponse: FetchResponse<void> = {
json: () => new Promise((resolve) => resolve()),
ok: true,
};

Expand Down

0 comments on commit 847f7c1

Please sign in to comment.