Skip to content

Commit fb4cfdc

Browse files
committed
Add some unit tests for HistoryArchiveExportSelector
1 parent 0d8ea47 commit fb4cfdc

File tree

2 files changed

+190
-3
lines changed

2 files changed

+190
-3
lines changed

client/src/components/Common/models/testData/exportData.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ export const FAILED_DOWNLOAD_RESPONSE: ObjectExportTaskResponse = {
9191
};
9292

9393
export const FILE_SOURCE_STORE_RESPONSE: ObjectExportTaskResponse = {
94-
id: "FAKE_RECENT_DOWNLOAD_ID",
94+
id: "FAKE_FILE_SOURCE_EXPORT_ID",
9595
ready: true,
9696
preparing: false,
97-
up_to_date: true,
97+
up_to_date: false,
9898
task_uuid: "35563335-e275-4520-80e8-885793279095",
9999
create_time: RECENT_EXPORT_DATE,
100100
export_metadata: {
@@ -103,6 +103,37 @@ export const FILE_SOURCE_STORE_RESPONSE: ObjectExportTaskResponse = {
103103
},
104104
};
105105

106+
export const RECENT_FILE_SOURCE_STORE_RESPONSE: ObjectExportTaskResponse = {
107+
...FILE_SOURCE_STORE_RESPONSE,
108+
id: "FAKE_RECENT_FILE_SOURCE_EXPORT_ID",
109+
up_to_date: true,
110+
};
111+
112+
export const FAILED_FILE_SOURCE_STORE_RESPONSE: ObjectExportTaskResponse = {
113+
...FILE_SOURCE_STORE_RESPONSE,
114+
id: "FAKE_FAILED_FILE_SOURCE_EXPORT_ID",
115+
export_metadata: {
116+
request_data: FAKE_FILE_SOURCE_REQUEST_DATA,
117+
result_data: FAILED_EXPORT_RESULT_DATA,
118+
},
119+
};
120+
121+
export const IN_PROGRESS_FILE_SOURCE_STORE_RESPONSE: ObjectExportTaskResponse = {
122+
...FILE_SOURCE_STORE_RESPONSE,
123+
id: "FAKE_IN_PROGRESS_FILE_SOURCE_EXPORT_ID",
124+
ready: false,
125+
preparing: true,
126+
export_metadata: {
127+
request_data: FAKE_FILE_SOURCE_REQUEST_DATA,
128+
result_data: undefined,
129+
},
130+
};
131+
106132
export const EXPIRED_STS_DOWNLOAD_RECORD = new ExportRecordModel(EXPIRED_STS_DOWNLOAD_RESPONSE);
107-
export const FILE_SOURCE_STORE_RECORD = new ExportRecordModel(FILE_SOURCE_STORE_RESPONSE);
108133
export const RECENT_STS_DOWNLOAD_RECORD = new ExportRecordModel(RECENT_STS_DOWNLOAD_RESPONSE);
134+
export const FAILED_DOWNLOAD_RECORD = new ExportRecordModel(FAILED_DOWNLOAD_RESPONSE);
135+
136+
export const FILE_SOURCE_STORE_RECORD = new ExportRecordModel(FILE_SOURCE_STORE_RESPONSE);
137+
export const RECENT_FILE_SOURCE_STORE_RECORD = new ExportRecordModel(RECENT_FILE_SOURCE_STORE_RESPONSE);
138+
export const FAILED_FILE_SOURCE_STORE_RECORD = new ExportRecordModel(FAILED_FILE_SOURCE_STORE_RESPONSE);
139+
export const IN_PROGRESS_FILE_SOURCE_STORE_RECORD = new ExportRecordModel(IN_PROGRESS_FILE_SOURCE_STORE_RESPONSE);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { shallowMount } from "@vue/test-utils";
2+
import { getLocalVue } from "tests/jest/helpers";
3+
import axios from "axios";
4+
import MockAdapter from "axios-mock-adapter";
5+
import flushPromises from "flush-promises";
6+
import { BFormCheckbox } from "bootstrap-vue";
7+
import HistoryArchiveExportSelector from "./HistoryArchiveExportSelector.vue";
8+
import type { HistorySummary } from "@/stores/historyStore";
9+
import { mockFetcher } from "@/schema/__mocks__";
10+
import {
11+
FAILED_FILE_SOURCE_STORE_RESPONSE,
12+
FILE_SOURCE_STORE_RESPONSE,
13+
IN_PROGRESS_FILE_SOURCE_STORE_RESPONSE,
14+
RECENT_FILE_SOURCE_STORE_RESPONSE,
15+
RECENT_STS_DOWNLOAD_RESPONSE,
16+
} from "@/components/Common/models/testData/exportData";
17+
18+
jest.mock("@/schema");
19+
20+
const localVue = getLocalVue(true);
21+
22+
const TEST_HISTORY_ID = "test-history-id";
23+
const TEST_HISTORY = {
24+
id: TEST_HISTORY_ID,
25+
name: "fake-history-name",
26+
archived: false,
27+
};
28+
29+
const GET_EXPORTS_API_ENDPOINT = "/api/histories/{history_id}/exports";
30+
31+
const EXPORT_RECORD_BTN = "#create-export-record-btn";
32+
const ARCHIVE_HISTORY_BTN = "#archive-history-btn";
33+
const CONFIRM_DELETE_CHECKBOX = "[type='checkbox']";
34+
35+
async function mountComponentWithHistory(history: HistorySummary) {
36+
const wrapper = shallowMount(HistoryArchiveExportSelector, {
37+
propsData: { history },
38+
localVue,
39+
stubs: {
40+
// Stub with the real component to be able to use setChecked
41+
BFormCheckbox,
42+
},
43+
});
44+
await flushPromises();
45+
return wrapper;
46+
}
47+
48+
describe("HistoryArchiveExportSelector.vue", () => {
49+
let axiosMock: MockAdapter;
50+
51+
beforeEach(async () => {
52+
axiosMock = new MockAdapter(axios);
53+
});
54+
55+
afterEach(() => {
56+
axiosMock.restore();
57+
});
58+
59+
it("should display a button to create an export record if there is no up to date export record", async () => {
60+
mockFetcher.path(GET_EXPORTS_API_ENDPOINT).method("get").mock({ data: [] });
61+
62+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
63+
64+
const createExportButton = wrapper.find(EXPORT_RECORD_BTN);
65+
expect(createExportButton.exists()).toBe(true);
66+
});
67+
68+
it("should display a button to create an export record if the most recent export record is not permanent", async () => {
69+
mockFetcher
70+
.path(GET_EXPORTS_API_ENDPOINT)
71+
.method("get")
72+
.mock({ data: [RECENT_STS_DOWNLOAD_RESPONSE] });
73+
74+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
75+
76+
const createExportButton = wrapper.find(EXPORT_RECORD_BTN);
77+
expect(createExportButton.exists()).toBe(true);
78+
});
79+
80+
it("should display a button to create an export record if there are permanent export records but none are up to date", async () => {
81+
mockFetcher
82+
.path(GET_EXPORTS_API_ENDPOINT)
83+
.method("get")
84+
.mock({ data: [FILE_SOURCE_STORE_RESPONSE, FAILED_FILE_SOURCE_STORE_RESPONSE] });
85+
86+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
87+
88+
const createExportButton = wrapper.find(EXPORT_RECORD_BTN);
89+
expect(createExportButton.exists()).toBe(true);
90+
});
91+
92+
it("should not display a button to create an export record if there is an up to date export record", async () => {
93+
mockFetcher
94+
.path(GET_EXPORTS_API_ENDPOINT)
95+
.method("get")
96+
.mock({ data: [RECENT_FILE_SOURCE_STORE_RESPONSE] });
97+
98+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
99+
100+
const createExportButton = wrapper.find(EXPORT_RECORD_BTN);
101+
expect(createExportButton.exists()).toBe(false);
102+
});
103+
104+
it("should not display a button to create an export record if a record is being created", async () => {
105+
mockFetcher
106+
.path(GET_EXPORTS_API_ENDPOINT)
107+
.method("get")
108+
.mock({ data: [IN_PROGRESS_FILE_SOURCE_STORE_RESPONSE] });
109+
110+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
111+
112+
const createExportButton = wrapper.find(EXPORT_RECORD_BTN);
113+
expect(createExportButton.exists()).toBe(false);
114+
});
115+
116+
it("should disable the Archive button if there is no up to date export record", async () => {
117+
mockFetcher.path(GET_EXPORTS_API_ENDPOINT).method("get").mock({ data: [] });
118+
119+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
120+
121+
const archiveButton = wrapper.find(ARCHIVE_HISTORY_BTN);
122+
expect(archiveButton.attributes("disabled")).toBeTruthy();
123+
});
124+
125+
it("should disable the Archive button if the confirm delete checkbox is not checked", async () => {
126+
mockFetcher
127+
.path(GET_EXPORTS_API_ENDPOINT)
128+
.method("get")
129+
.mock({ data: [RECENT_FILE_SOURCE_STORE_RESPONSE] });
130+
131+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
132+
133+
const confirmDeleteCheckbox = wrapper.find(CONFIRM_DELETE_CHECKBOX);
134+
await confirmDeleteCheckbox.setChecked(false);
135+
expect((confirmDeleteCheckbox.element as HTMLInputElement).checked).toBeFalsy();
136+
137+
const archiveButton = wrapper.find(ARCHIVE_HISTORY_BTN);
138+
expect(archiveButton.attributes("disabled")).toBeTruthy();
139+
});
140+
141+
it("should enable the Archive button if there is an up to date export record and the confirm delete checkbox is checked", async () => {
142+
mockFetcher
143+
.path(GET_EXPORTS_API_ENDPOINT)
144+
.method("get")
145+
.mock({ data: [RECENT_FILE_SOURCE_STORE_RESPONSE] });
146+
147+
const wrapper = await mountComponentWithHistory(TEST_HISTORY as HistorySummary);
148+
149+
const confirmDeleteCheckbox = wrapper.find(CONFIRM_DELETE_CHECKBOX);
150+
await confirmDeleteCheckbox.setChecked(true);
151+
expect((confirmDeleteCheckbox.element as HTMLInputElement).checked).toBeTruthy();
152+
153+
const archiveButton = wrapper.find(ARCHIVE_HISTORY_BTN);
154+
expect(archiveButton.attributes("disabled")).toBeFalsy();
155+
});
156+
});

0 commit comments

Comments
 (0)