Skip to content

Commit

Permalink
VUU-54: Extract expectError
Browse files Browse the repository at this point in the history
  • Loading branch information
pling-scottlogic committed Oct 10, 2023
1 parent 6e91966 commit 7ac0227
Showing 1 changed file with 72 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,60 +133,68 @@ describe("updateLayout", () => {
it("errors if there is no metadata in local storage with requested ID ", async () => {
saveLocalEntity(layoutsSaveLocation, [existingLayout]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`No metadata with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`No metadata with ID ${existingId}`);
});

it("errors if there is no layout in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`No layout with ID ${existingId}`);
});

it("errors if there is no metadata or layout in local storage with requested ID ", async () => {
const requestedId = "non_existent_id";

expect(persistenceManager.updateLayout(requestedId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`No metadata with ID ${requestedId}; No layout with ID ${requestedId}`));
expectError(() =>
persistenceManager.updateLayout(requestedId, metadataToAdd, layoutToAdd),
`No metadata with ID ${requestedId}; No layout with ID ${requestedId}`);
});

it("errors if there are multiple metadata entries in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`Non-unique metadata with ID ${existingId}`);
});

it("errors if there are multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`Non-unique metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and no layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}; No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`Non-unique metadata with ID ${existingId}; No layout with ID ${existingId}`);
});

it("errors if there are no metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd))
.rejects.toStrictEqual(new Error(`No metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.updateLayout(existingId, metadataToAdd, layoutToAdd),
`No metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`);
});
});

Expand All @@ -208,59 +216,68 @@ describe("deleteLayout", () => {
it("errors if there is no metadata in local storage with requested ID ", async () => {
saveLocalEntity(layoutsSaveLocation, [existingLayout]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`No metadata with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`No metadata with ID ${existingId}`);
});

it("errors if there is no layout in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`No layout with ID ${existingId}`);
});

it("errors if there is no metadata or layout in local storage with requested ID ", async () => {
const requestedId = "non_existent_id";
expect(persistenceManager.deleteLayout(requestedId))
.rejects.toStrictEqual(new Error(`No metadata with ID ${requestedId}; No layout with ID ${requestedId}`));

expectError(() =>
persistenceManager.deleteLayout(requestedId),
`No metadata with ID ${requestedId}; No layout with ID ${requestedId}`);
});

it("errors if there are multiple metadata entries in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`Non-unique metadata with ID ${existingId}`);
});

it("errors if there are multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`Non-unique metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and no layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique metadata with ID ${existingId}; No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`Non-unique metadata with ID ${existingId}; No layout with ID ${existingId}`);
});

it("errors if there are no metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.deleteLayout(existingId))
.rejects.toStrictEqual(new Error(`No metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.deleteLayout(existingId),
`No metadata with ID ${existingId}; Non-unique layout with ID ${existingId}`);
});
});

Expand All @@ -286,14 +303,17 @@ describe("loadLayout", () => {
it("errors if there is no layout in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);

expect(persistenceManager.loadLayout(existingId))
.rejects.toStrictEqual(new Error(`No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.loadLayout(existingId),
`No layout with ID ${existingId}`);
});

it("errors if there is no metadata or layout in local storage with requested ID ", async () => {
const requestedId = "non_existent_id";
expect(persistenceManager.loadLayout(requestedId))
.rejects.toStrictEqual(new Error(`No layout with ID ${requestedId}`));

expectError(() =>
persistenceManager.loadLayout(requestedId),
`No layout with ID ${requestedId}`);
});

it("retrieves layout if there are multiple metadata entries in local storage with requested ID ", async () => {
Expand All @@ -309,30 +329,34 @@ describe("loadLayout", () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.loadLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.loadLayout(existingId),
`Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.loadLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.loadLayout(existingId),
`Non-unique layout with ID ${existingId}`);
});

it("errors if there are multiple metadata entries and no layouts in local storage with requested ID ", async () => {
saveLocalEntity(metadataSaveLocation, [existingMetadata, existingMetadata]);

expect(persistenceManager.loadLayout(existingId))
.rejects.toStrictEqual(new Error(`No layout with ID ${existingId}`));
expectError(() =>
persistenceManager.loadLayout(existingId),
`No layout with ID ${existingId}`);
});

it("errors if there are no metadata entries and multiple layouts in local storage with requested ID ", async () => {
saveLocalEntity(layoutsSaveLocation, [existingLayout, existingLayout]);

expect(persistenceManager.loadLayout(existingId))
.rejects.toStrictEqual(new Error(`Non-unique layout with ID ${existingId}`));
expectError(() =>
persistenceManager.loadLayout(existingId),
`Non-unique layout with ID ${existingId}`);
});
});

Expand All @@ -358,3 +382,7 @@ describe("loadMetadata", () => {
expect(await persistenceManager.loadMetadata()).toEqual([]);
});
});

const expectError = (f: () => Promise<unknown>, message: string) => {
expect(f).rejects.toStrictEqual(new Error(message));
};

0 comments on commit 7ac0227

Please sign in to comment.