Skip to content

Commit

Permalink
test: add default returns to spy targets
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed Aug 18, 2024
1 parent 6e43fd4 commit fdd73ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
63 changes: 29 additions & 34 deletions src/DdbClientWrapper/DdbClientWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ describe("DdbClientWrapper", () => {
const ddbClientSpyTarget = (mockDdbClientWrapper as any)._ddbClient;
const ddbDocClientSpyTarget = (mockDdbClientWrapper as any)._ddbDocClient;

// Arrange spy targets to return empty objects by default:
beforeEach(() => {
vi.spyOn(ddbClientSpyTarget, "send").mockResolvedValue({});
vi.spyOn(ddbDocClientSpyTarget, "send").mockResolvedValue({});
});

// Mock item inputs:
const mockItems = [
{ id: "USER-1", name: "Human McPerson", data: 32 },
Expand Down Expand Up @@ -89,9 +95,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.getItem(getItemValidInput);
expect(result.Item).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.getItem(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.getItem(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -127,9 +132,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.batchGetItems(batchGetItemValidInput);
expect(result.Responses).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.batchGetItems(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.batchGetItems(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -160,9 +164,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.putItem(putItemValidInput);
expect(result.Attributes).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.putItem(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.putItem(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -201,9 +204,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.updateItem(updateItemValidInput);
expect(result.Attributes).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.updateItem(updateItemValidInput);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.updateItem(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -234,9 +236,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.deleteItem(deleteItemValidInput);
expect(result.Attributes).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.deleteItem(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.deleteItem(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -269,11 +270,10 @@ describe("DdbClientWrapper", () => {
});
test(`returns undefined when called with valid arguments but nothing is returned`, async () => {
const result = await mockDdbClientWrapper.batchWriteItems(batchWriteItemValidInput);
expect(result).toBeUndefined();
expect(result.ConsumedCapacity).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.batchWriteItems(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.batchWriteItems(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -308,9 +308,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.query(queryValidInput);
expect(result.Items).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.query(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.query(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -340,9 +339,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.scan(scanValidInput);
expect(result.Items).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.scan(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.scan(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -372,9 +370,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.describeTable(describeTableValidInput);
expect(result.Table).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.describeTable(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.describeTable(null as any)).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -412,9 +409,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.createTable(createTableValidInput);
expect(result.TableDescription).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.createTable(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.createTable(null as any)).resolves.not.toThrow();
});
});

Expand All @@ -441,9 +437,8 @@ describe("DdbClientWrapper", () => {
const result = await mockDdbClientWrapper.listTables();
expect(result.TableNames).toBeUndefined();
});
test(`returns undefined and does not throw when called with invalid arguments`, async () => {
const result = await mockDdbClientWrapper.listTables(null as any);
expect(result).toBeUndefined();
test(`does not throw when called with invalid arguments`, async () => {
await expect(mockDdbClientWrapper.listTables(null as any)).resolves.not.toThrow();
});
});
});
5 changes: 5 additions & 0 deletions src/Model/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ describe("Model", () => {
// Assign var to _ddbDocClient "spy target" (private instance property, hence `as any`):
const ddbDocClientSpyTarget = (mockModel.ddbClient as any)._ddbDocClient;

// Arrange spy target to return empty object by default:
beforeEach(() => {
vi.spyOn(ddbDocClientSpyTarget, "send").mockResolvedValue({});
});

// Mock items/keys for method inputs and defining expected results:
const {
mockItems,
Expand Down

0 comments on commit fdd73ce

Please sign in to comment.