|
| 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