UNCLASSIFIED - NO CUI

Skip to content
Snippets Groups Projects
Err.spec.js 1.72 KiB
Newer Older
import { mount, RouterLinkStub } from "@vue/test-utils";
import Err from "@/views/Err.vue";
import Search from "@/api/search.js";
import { expect } from "../../../node_modules/vitest/dist/index";

vi.mock("../../../src/api/search.js");

describe("Err.vue", () => {
  afterEach(() => {
    vi.clearAllMocks();
  });

  it("should attempt multiple searches with increasing fuzzyness", async () => {
    const mockSearch = vi.fn().mockReturnValue([]);
    Search.mockImplementation(() => ({
      search: mockSearch,
    }));

    const wrapper = mount(Err, {
      stubs: {
        RouterLink: RouterLinkStub,
      },
      mocks: {
        $route: {
          fullPath: "/mock/path",
        },
      },
    });

    expect(mockSearch).toHaveBeenCalledTimes(4);
    expect(mockSearch.mock.calls[0][0]).toEqual("mock~0 path~0");
    expect(mockSearch.mock.calls[1][0]).toEqual("mock~1 path~1");
    expect(mockSearch.mock.calls[2][0]).toEqual("mock~2 path~2");
    expect(mockSearch.mock.calls[3][0]).toEqual("mock~3 path~3");
  });

  it("should add suggestions and exit early", async () => {
    const mockSearch = vi
      .fn()
      .mockReturnValueOnce([{ ref: "/" }, { ref: "/services" }])
      .mockReturnValueOnce([{ ref: "/products/cnap" }]);
    Search.mockImplementation(() => ({
      search: mockSearch,
    }));

    const wrapper = mount(Err, {
      stubs: {
        RouterLink: RouterLinkStub,
      },
      mocks: {
        $route: {
          fullPath: "/mock/path",
        },
      },
    });

    expect(mockSearch).toHaveBeenCalledTimes(2);
    expect(mockSearch.mock.calls[0][0]).toEqual("mock~0 path~0");
    expect(mockSearch.mock.calls[1][0]).toEqual("mock~1 path~1");
    expect(wrapper.vm.pageSuggestions.length).toEqual(3);
  });
});