Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): attempt to fix test #968

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions lib/lambda/setupIndex.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { handler } from "./setupIndex";
import * as opensearchLib from "libs/opensearch-lib";

const mockFns = vi.hoisted(() => ({
createIndex: vi.fn(),
updateFieldMapping: vi.fn(),
}));
const MOCK_EVENT = {
osDomain: "test-domain",
indexNamespace: "test-namespace-",
};
const MOCK_CALLBACK = vi.fn();

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

describe.skip("handler", () => {
// TODO: fix this test - it shows an error: SyntaxError: Unexpected token ')' [which is just not the case]
const mockCallback = vi.fn();
const mockEvent = {
osDomain: "test-domain",
indexNamespace: "test-namespace-",
};
describe("handler", () => {
const spiedOnUpdateFieldMapping = vi.spyOn(opensearchLib, "updateFieldMapping");

beforeEach(() => {
vi.clearAllMocks();
mockCallback.mockClear();
vi.resetAllMocks();
MOCK_CALLBACK.mockClear();
});

it("should create and update indices without errors", async () => {
await handler(mockEvent, expect.anything(), mockCallback);

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",
const spiedOnCreateIndex = vi
.spyOn(opensearchLib, "createIndex")
.mockImplementation(() => Promise.resolve());

await handler(MOCK_EVENT, expect.anything(), MOCK_CALLBACK);

expect(spiedOnCreateIndex).toHaveBeenCalledTimes(7);

const expectedIndices = [
"test-namespace-main",
"test-namespace-changelog",
"test-namespace-types",
"test-namespace-subtypes",
"test-namespace-cpocs",
"test-namespace-insights",
"test-namespace-legacyinsights",
);
];

for (const index of expectedIndices) {
expect(opensearchLib.createIndex).toHaveBeenCalledWith("test-domain", index);
}

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

expect(mockCallback).toHaveBeenCalledWith(null, { statusCode: 200 });
expect(MOCK_CALLBACK).toHaveBeenCalledWith(null, { statusCode: 200 });
});

it("should handle errors and return status 500", async () => {
mockFns.createIndex.mockRejectedValueOnce(new Error("Test error"));
const spiedOnCreateIndex = vi
.spyOn(opensearchLib, "createIndex")
.mockRejectedValueOnce(new Error("Test error"));

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

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