Skip to content

Commit

Permalink
update setupIndex test
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpaige committed Jan 2, 2025
1 parent 30f481e commit 4ede6c4
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions lib/lambda/setupIndex.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// my-lib.test.ts
import { describe, it, expect, vi, beforeEach } from "vitest";
import { handler } from "./setupIndex"; // The code that calls createIndex, updateFieldMapping
import { handler } from "./setupIndex";

// 1) Define mocks first
const mockCreateIndex = vi.fn();
const mockUpdateFieldMapping = vi.fn();
const mockFns = vi.hoisted(() => ({
createIndex: vi.fn(),
updateFieldMapping: vi.fn(),
}));

describe.skip("handler", () => {
vi.mock("../libs/opensearch-lib", () => mockFns);

describe("handler", () => {
const mockCallback = vi.fn();
const mockEvent = {
osDomain: "test-domain",
Expand All @@ -21,18 +23,20 @@ describe.skip("handler", () => {
it("should create and update indices without errors", async () => {
await handler(mockEvent, expect.anything(), mockCallback);

// 3. Use the mocked functions in the expectations:
expect(mockCreateIndex).toHaveBeenCalledTimes(7);
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-main");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-changelog");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-types");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-subtypes");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-cpocs");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-insights");
expect(mockCreateIndex).toHaveBeenCalledWith("test-domain", "test-namespace-legacyinsights");

expect(mockUpdateFieldMapping).toHaveBeenCalledTimes(1);
expect(mockUpdateFieldMapping).toHaveBeenCalledWith("test-domain", "test-namespace-main", {
expect(mockFns.createIndex).toHaveBeenCalledTimes(7);
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-main");
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-changelog");
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-types");
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-subtypes");
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-cpocs");
expect(mockFns.createIndex).toHaveBeenCalledWith("test-domain", "test-namespace-insights");
expect(mockFns.createIndex).toHaveBeenCalledWith(
"test-domain",
"test-namespace-legacyinsights",
);

expect(mockFns.updateFieldMapping).toHaveBeenCalledTimes(1);
expect(mockFns.updateFieldMapping).toHaveBeenCalledWith("test-domain", "test-namespace-main", {
approvedEffectiveDate: { type: "date" },
changedDate: { type: "date" },
finalDispositionDate: { type: "date" },
Expand All @@ -45,13 +49,12 @@ describe.skip("handler", () => {
});

it("should handle errors and return status 500", async () => {
// Make the first call to mockCreateIndex reject:
mockCreateIndex.mockRejectedValueOnce(new Error("Test error"));
mockFns.createIndex.mockRejectedValueOnce(new Error("Test error"));

await handler(mockEvent, expect.anything(), mockCallback);

expect(mockCreateIndex).toHaveBeenCalledTimes(1);
expect(mockUpdateFieldMapping).not.toHaveBeenCalled();
expect(mockFns.createIndex).toHaveBeenCalledTimes(1);
expect(mockFns.updateFieldMapping).not.toHaveBeenCalled();
expect(mockCallback).toHaveBeenCalledWith(expect.any(Error), {
statusCode: 500,
});
Expand Down

0 comments on commit 4ede6c4

Please sign in to comment.