|
| 1 | +import { getLocalVue } from "@tests/vitest/helpers"; |
| 2 | +import { shallowMount } from "@vue/test-utils"; |
| 3 | +import flushPromises from "flush-promises"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { computed } from "vue"; |
| 6 | + |
| 7 | +import type { PathDestination } from "@/composables/datasetPathDestination"; |
| 8 | +import * as datasetPathDestinationModule from "@/composables/datasetPathDestination"; |
| 9 | + |
| 10 | +import DatasetIndex from "./DatasetIndex.vue"; |
| 11 | + |
| 12 | +const localVue = getLocalVue(); |
| 13 | + |
| 14 | +vi.mock("@/composables/datasetPathDestination"); |
| 15 | + |
| 16 | +describe("DatasetIndex", () => { |
| 17 | + let mockDatasetPathDestination: ReturnType<typeof vi.fn>; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + // Reset mock before each test |
| 21 | + mockDatasetPathDestination = vi.fn(); |
| 22 | + vi.mocked(datasetPathDestinationModule.useDatasetPathDestination).mockReturnValue({ |
| 23 | + datasetPathDestination: computed(() => mockDatasetPathDestination) as any, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("renders GTable when dataset has valid content", async () => { |
| 28 | + const mockPathDestination: PathDestination = { |
| 29 | + datasetContent: [ |
| 30 | + { path: "file1.txt", class: "File" }, |
| 31 | + { path: "file2.csv", class: "File" }, |
| 32 | + ], |
| 33 | + isDirectory: false, |
| 34 | + }; |
| 35 | + |
| 36 | + mockDatasetPathDestination.mockResolvedValue(mockPathDestination); |
| 37 | + |
| 38 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 39 | + propsData: { |
| 40 | + historyDatasetId: "test-dataset-123", |
| 41 | + }, |
| 42 | + localVue, |
| 43 | + }); |
| 44 | + |
| 45 | + // Wait for async computedAsync to resolve |
| 46 | + await flushPromises(); |
| 47 | + |
| 48 | + // Table renders (shallowMount converts it to anonymous-stub) |
| 49 | + const gTable = wrapper.find("#g-table-0"); |
| 50 | + expect(gTable.exists()).toBe(true); |
| 51 | + expect(gTable.attributes("fields")).toBeDefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("displays error message when dataset is not composite", async () => { |
| 55 | + mockDatasetPathDestination.mockResolvedValue(null); |
| 56 | + |
| 57 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 58 | + propsData: { |
| 59 | + historyDatasetId: "test-dataset-123", |
| 60 | + }, |
| 61 | + localVue, |
| 62 | + }); |
| 63 | + |
| 64 | + await flushPromises(); |
| 65 | + |
| 66 | + expect(wrapper.text()).toContain("Dataset is not composite!"); |
| 67 | + const gTable = wrapper.find("gtable-stub"); |
| 68 | + expect(gTable.exists()).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it("displays error message when path points to a file", async () => { |
| 72 | + const mockPathDestination: PathDestination = { |
| 73 | + datasetContent: [{ path: "file1.txt", class: "File" }], |
| 74 | + isDirectory: false, |
| 75 | + fileLink: "/api/datasets/123/file1.txt", |
| 76 | + }; |
| 77 | + |
| 78 | + mockDatasetPathDestination.mockResolvedValue(mockPathDestination); |
| 79 | + |
| 80 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 81 | + propsData: { |
| 82 | + historyDatasetId: "test-dataset-123", |
| 83 | + path: "file1.txt", |
| 84 | + }, |
| 85 | + localVue, |
| 86 | + }); |
| 87 | + |
| 88 | + await flushPromises(); |
| 89 | + |
| 90 | + expect(wrapper.text()).toContain("is not a directory!"); |
| 91 | + const gTable = wrapper.find("gtable-stub"); |
| 92 | + expect(gTable.exists()).toBe(false); |
| 93 | + }); |
| 94 | + |
| 95 | + it("displays error message when path is not found", async () => { |
| 96 | + const mockPathDestination: PathDestination = { |
| 97 | + datasetContent: [{ path: "other.txt", class: "File" }], |
| 98 | + isDirectory: false, |
| 99 | + filepath: "nonexistent.txt", |
| 100 | + }; |
| 101 | + |
| 102 | + mockDatasetPathDestination.mockResolvedValue(mockPathDestination); |
| 103 | + |
| 104 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 105 | + propsData: { |
| 106 | + historyDatasetId: "test-dataset-123", |
| 107 | + path: "nonexistent.txt", |
| 108 | + }, |
| 109 | + localVue, |
| 110 | + }); |
| 111 | + |
| 112 | + await flushPromises(); |
| 113 | + |
| 114 | + expect(wrapper.text()).toContain("nonexistent.txt"); |
| 115 | + expect(wrapper.text()).toContain("is not found!"); |
| 116 | + const gTable = wrapper.find("gtable-stub"); |
| 117 | + expect(gTable.exists()).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it("renders GTable with correct fields configuration", async () => { |
| 121 | + const mockPathDestination: PathDestination = { |
| 122 | + datasetContent: [ |
| 123 | + { path: "test.txt", class: "File" }, |
| 124 | + { path: "data.csv", class: "File" }, |
| 125 | + ], |
| 126 | + isDirectory: false, |
| 127 | + }; |
| 128 | + |
| 129 | + mockDatasetPathDestination.mockResolvedValue(mockPathDestination); |
| 130 | + |
| 131 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 132 | + propsData: { |
| 133 | + historyDatasetId: "test-dataset-123", |
| 134 | + }, |
| 135 | + localVue, |
| 136 | + }); |
| 137 | + |
| 138 | + await flushPromises(); |
| 139 | + |
| 140 | + const html = wrapper.html(); |
| 141 | + expect(html).toContain("fields"); |
| 142 | + expect(html).toContain("items"); |
| 143 | + expect(html).toContain("[object Object]"); |
| 144 | + }); |
| 145 | + |
| 146 | + it("filters directory content when viewing a subdirectory", async () => { |
| 147 | + const mockPathDestination: PathDestination = { |
| 148 | + datasetContent: [ |
| 149 | + { path: "subfolder/nested.txt", class: "File" }, |
| 150 | + { path: "subfolder/another.csv", class: "File" }, |
| 151 | + ], |
| 152 | + isDirectory: true, |
| 153 | + filepath: "subfolder", |
| 154 | + }; |
| 155 | + |
| 156 | + mockDatasetPathDestination.mockResolvedValue(mockPathDestination); |
| 157 | + |
| 158 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 159 | + propsData: { |
| 160 | + historyDatasetId: "test-dataset-123", |
| 161 | + path: "subfolder", |
| 162 | + }, |
| 163 | + localVue, |
| 164 | + }); |
| 165 | + |
| 166 | + await flushPromises(); |
| 167 | + |
| 168 | + // Check that table renders (shallowMount converts GTable to anonymous-stub) |
| 169 | + expect(wrapper.html()).toContain("fields"); |
| 170 | + expect(wrapper.html()).not.toContain("is not found"); |
| 171 | + expect(wrapper.html()).not.toContain("is not a directory"); |
| 172 | + }); |
| 173 | + |
| 174 | + it("does not render GTable when error message is displayed", async () => { |
| 175 | + mockDatasetPathDestination.mockResolvedValue(null); |
| 176 | + |
| 177 | + const wrapper = shallowMount(DatasetIndex as object, { |
| 178 | + propsData: { |
| 179 | + historyDatasetId: "test-dataset-123", |
| 180 | + }, |
| 181 | + localVue, |
| 182 | + }); |
| 183 | + |
| 184 | + await flushPromises(); |
| 185 | + |
| 186 | + const gTable = wrapper.find("gtable-stub"); |
| 187 | + const errorDiv = wrapper.find("div"); |
| 188 | + |
| 189 | + expect(gTable.exists()).toBe(false); |
| 190 | + expect(errorDiv.exists()).toBe(true); |
| 191 | + expect(errorDiv.text()).toContain("Dataset is not composite!"); |
| 192 | + }); |
| 193 | +}); |
0 commit comments