Skip to content

Commit

Permalink
test(util-dynamodb): move mockOutput to top in marshall
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jan 23, 2021
1 parent b6667d0 commit c671ae6
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions packages/util-dynamodb/src/marshall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ import { INativeAttributeValue, NativeAttributeValue } from "./models";
jest.mock("./convertToAttr");

describe("marshall", () => {
const mockOutput = { S: "mockOutput" };
(convertToAttr as jest.Mock).mockReturnValue({ M: mockOutput });

afterEach(() => {
jest.clearAllMocks();
});

it("with object as an input", () => {
const input = { a: "A", b: "B" };
const output = { a: { S: "A" }, b: { S: "B" } };
(convertToAttr as jest.Mock).mockReturnValue({ M: output });

expect(marshall(input)).toEqual(output);
expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});

["convertEmptyValues", "removeUndefinedValues"].forEach((option) => {
describe(`options.${option}`, () => {
const input = { a: "A", b: "B" };
const output = { a: { S: "A" }, b: { S: "B" } };

beforeEach(() => {
(convertToAttr as jest.Mock).mockReturnValue({ M: output });
});

[false, true].forEach((value) => {
it(`passes ${value} to convertToAttr`, () => {
expect(marshall(input, { [option]: value })).toEqual(output);
expect(marshall(input, { [option]: value })).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, { [option]: value });
});
Expand All @@ -41,11 +37,8 @@ describe("marshall", () => {
it("with type as an input", () => {
type TestInputType = { a: string; b: string };
const input: TestInputType = { a: "A", b: "B" };
const output = { a: { S: "A" }, b: { S: "B" } };

(convertToAttr as jest.Mock).mockReturnValue({ M: output });

expect(marshall(input)).toEqual(output);
expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});
Expand All @@ -56,11 +49,8 @@ describe("marshall", () => {
b: string;
}
const input: TestInputInterface = { a: "A", b: "B" };
const output = { a: { S: "A" }, b: { S: "B" } };

(convertToAttr as jest.Mock).mockReturnValue({ M: output });

expect(marshall(input)).toEqual(output);
expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});
Expand All @@ -75,11 +65,8 @@ describe("marshall", () => {
name: IPersonName;
}
const input: IPerson = { id: "id", name: { firstname: "John", lastname: "Doe" } };
const output = { a: { S: "A" }, b: { S: "B" } };

(convertToAttr as jest.Mock).mockReturnValue({ M: output });

expect(marshall(input)).toEqual(output);
expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});
Expand Down

0 comments on commit c671ae6

Please sign in to comment.