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

EREGCSC-2746 -- Upgrade Vitest front end unit tests in key portions of the code #1503

Merged
merged 12 commits into from
Jan 6, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const tableSchema = computed(() => {
}
});


// Get column dates from Django template
const getColumnDates = () => {
if (!document.getElementById("site_config")) return {};
Expand All @@ -47,7 +46,7 @@ const getColumnDates = () => {
);

return rawDates;
}
};

const columnDates = getColumnDates();
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render } from "@testing-library/vue";
import { describe, it, expect } from "vitest";

import TableCaption from "./TableCaption.vue";

describe("Statute Table Caption", () => {
it(`Creates a snapshot of the Statute Selector with default props`, async () => {
const wrapper = render(TableCaption);
expect(wrapper).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<script setup></script>

<template>
<div class="table__caption">
This table shows U.S. Code sections enacted by the Social Security Act.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Statute Table Caption > Creates a snapshot of the Statute Selector with default props 1`] = `
{
"baseElement": <body>
<div>
<div
class="table__caption"
>
This table shows U.S. Code sections enacted by the Social Security Act. See also:
<a
class="external"
href="https://uscode.house.gov/view.xhtml?req=granuleid%3AUSC-prelim-title42-chapter7&saved=%7CZ3JhbnVsZWlkOlVTQy1wcmVsaW0tdGl0bGU0Mi1jaGFwdGVyNy1mcm9udA%3D%3D%7C%7C%7C0%7Cfalse%7Cprelim&edition=prelim"
rel="noopener noreferrer"
target="_blank"
>
full table of contents and text for 42 U.S.C. Chapter 7 (Social Security)
</a>
.
<br />
Learn about these sources:
<a
class="external"
href="https://uscode.house.gov/"
rel="noopener noreferrer"
target="_blank"
>
US Code House.gov
</a>
,
<a
class="external"
href="https://www.govinfo.gov/app/collection/comps/"
rel="noopener noreferrer"
target="_blank"
>
Statute Compilation
</a>
,
<a
class="external"
href="https://www.govinfo.gov/app/collection/uscode"
rel="noopener noreferrer"
target="_blank"
>
US Code Annual
</a>
,
<a
class="external"
href="https://www.ssa.gov/OP_Home/ssact/ssact.htm"
rel="noopener noreferrer"
target="_blank"
>
SSA.gov Compilation
</a>
.
</div>
</div>
</body>,
"container": <div>
<div
class="table__caption"
>
This table shows U.S. Code sections enacted by the Social Security Act. See also:
<a
class="external"
href="https://uscode.house.gov/view.xhtml?req=granuleid%3AUSC-prelim-title42-chapter7&saved=%7CZ3JhbnVsZWlkOlVTQy1wcmVsaW0tdGl0bGU0Mi1jaGFwdGVyNy1mcm9udA%3D%3D%7C%7C%7C0%7Cfalse%7Cprelim&edition=prelim"
rel="noopener noreferrer"
target="_blank"
>
full table of contents and text for 42 U.S.C. Chapter 7 (Social Security)
</a>
.
<br />
Learn about these sources:
<a
class="external"
href="https://uscode.house.gov/"
rel="noopener noreferrer"
target="_blank"
>
US Code House.gov
</a>
,
<a
class="external"
href="https://www.govinfo.gov/app/collection/comps/"
rel="noopener noreferrer"
target="_blank"
>
Statute Compilation
</a>
,
<a
class="external"
href="https://www.govinfo.gov/app/collection/uscode"
rel="noopener noreferrer"
target="_blank"
>
US Code Annual
</a>
,
<a
class="external"
href="https://www.ssa.gov/OP_Home/ssact/ssact.htm"
rel="noopener noreferrer"
target="_blank"
>
SSA.gov Compilation
</a>
.
</div>
</div>,
"debug": [Function],
"emitted": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"html": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import HeaderCell from "./HeaderCell.vue";

describe("Statute Table Header Cell", () => {
describe("SSA table header", () => {
const secondaryCells = ssaSchema.filter((column) => column.header.secondary);
const secondaryCells = ssaSchema.filter(
(column) => column.header.secondary
);

secondaryCells.forEach((column, index) => {
it(`Creates a snapshot of header cell for column ${index + 1} without dates`, async () => {
Expand All @@ -22,7 +24,9 @@ describe("Statute Table Header Cell", () => {
});
await flushPromises();

const dateCell = wrapper.getByTestId(`${column.header.testId}-subtitle-1`).textContent;
const dateCell = wrapper.getByTestId(
`${column.header.testId}-subtitle-1`
).textContent;
expect(dateCell.trim()).toEqual("");

expect(wrapper).toMatchSnapshot();
Expand All @@ -38,7 +42,9 @@ describe("Statute Table Header Cell", () => {
});
await flushPromises();

const dateCell = wrapper.getByTestId(`${column.header.testId}-subtitle-1`).textContent;
const dateCell = wrapper.getByTestId(
`${column.header.testId}-subtitle-1`
).textContent;
expect(dateCell.trim()).toEqual("effective Aug 2023");

expect(wrapper).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ describe("Statute Table URL methods", () => {
});

describe("statuteCompilationUrl", () => {
it("returns null if source_url is undefined", async () => {
const undefinedSourceUrl = { source_url: undefined };
const computedUndefinedUrl =
statuteCompilationUrl(undefinedSourceUrl);
expect(computedUndefinedUrl).toBeNull();
});

it("returns expected URL string if source_url is valid", async () => {
const statuteItem = statutesFixture[0];
const computedUrl = statuteCompilationUrl(statuteItem);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, expect, vi } from "vitest";
import { ref } from "vue";

import TocContainer from "./TocContainer.vue";

import flushPromises from "flush-promises";

describe("Toc Container", () => {
afterEach(() => {
vi.clearAllMocks();
});
describe("getTitlesArray", () => {
it("should properly format titles", async () => {
const titles = ref([]);
TocContainer.getTitlesArray({
apiUrl: "http://localhost:9000/",
titles,
});
await flushPromises();
expect(titles.value).toEqual(["Title 42", "Title 45"]);
});
});

describe("getTOCs", () => {
it("should properly format TOCs", async () => {
// mock global fetch for our single async call
global.fetch = vi.fn(() =>
Promise.resolve({
json: () => Promise.resolve("TOC response"),
})
);
const TOCs = ref([]);
TocContainer.getTOCs({
apiUrl: "http://localhost:9000/",
titlesArr: ["42"],
TOCs,
});
await flushPromises();
expect(fetch).toHaveBeenCalledWith(
"http://localhost:9000/title/42/toc",
{
cache: "no-cache",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
method: "GET",
mode: "cors",
params: undefined,
redirect: "follow",
}
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
<script>
const getTitlesArray = async ({ apiUrl, titles }) => {
const titlesArray = await getTitles({ apiUrl });
const formattedTitleNamesArray = titlesArray.map(
(title) => `Title ${title}`
);
titles.value = formattedTitleNamesArray;
};

const getTOCs = async ({ apiUrl, titlesArr, TOCs }) => {
const tocArray = await Promise.all(
titlesArr.map(async (title) => {
const tocStruct = await getTOC({ title, apiUrl });
return tocStruct;
})
);

TOCs.value = tocArray;
};

export default { getTitlesArray, getTOCs };
</script>

<script setup>
import { provide, reactive, ref, watch } from "vue";
import { provide, ref, watch } from "vue";

import { getTitles, getTOC } from "utilities/api";

Expand All @@ -21,37 +44,20 @@ provide("homeUrl", props.homeUrl);

// Titles
const titles = ref([]);
const getTitlesArray = async () => {
const titlesArray = await getTitles({ apiUrl: props.apiUrl });
const formattedTitleNamesArray = titlesArray.map(
(title) => `Title ${title}`
);
titles.value = formattedTitleNamesArray;
};

// Table of Contents for each title
const TOCs = ref([]);
const getTOCs = async (titlesArr) => {
const tocArray = await Promise.all(
titlesArr.map(async (title) => {
const tocStruct = await getTOC({ title, apiUrl: props.apiUrl });
return tocStruct;
})
);

TOCs.value = tocArray;
};

watch(titles, (newArr) => {
const unformattedTitles = newArr.map((title) => title.split(" ")[1]);
getTOCs(unformattedTitles);
getTOCs({ apiUrl: props.apiUrl, titlesArr: unformattedTitles, TOCs });
});

// tab state, etc
const selectedTitle = ref(0);

// On load
getTitlesArray();
getTitlesArray({ apiUrl: props.apiUrl, titles });
</script>

<template>
Expand Down
Loading
Loading