|
| 1 | +import { createPinia, setActivePinia } from "pinia"; |
| 2 | +import { createVuetify } from "vuetify"; |
| 3 | +import { flushPromises, mount } from "@vue/test-utils"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import MockAdapter from "axios-mock-adapter"; |
| 6 | +import Tags from "@/views/Tags.vue"; |
| 7 | +import { tagsApi } from "@/api/http"; |
| 8 | +import { SnackbarPlugin } from "@/plugins/snackbar"; |
| 9 | +import { envVariables } from "@/envVariables"; |
| 10 | +import TagList from "@/components/Tags/TagList.vue"; |
| 11 | +import NoItemsMessage from "@/components/NoItemsMessage.vue"; |
| 12 | +import useTagsStore from "@/store/modules/tags"; |
| 13 | + |
| 14 | +const mockTags = [ |
| 15 | + { |
| 16 | + tenant_id: "fake-tenant-data", |
| 17 | + name: "1", |
| 18 | + created_at: "2025-12-09T10:00:00Z", |
| 19 | + updated_at: "2025-12-09T10:00:00Z", |
| 20 | + }, |
| 21 | + { |
| 22 | + tenant_id: "fake-tenant-data", |
| 23 | + name: "2", |
| 24 | + created_at: "2025-12-09T12:00:00Z", |
| 25 | + updated_at: "2025-12-09T12:00:00Z", |
| 26 | + }, |
| 27 | +]; |
| 28 | + |
| 29 | +describe("Tags View", async () => { |
| 30 | + setActivePinia(createPinia()); |
| 31 | + const vuetify = createVuetify(); |
| 32 | + const mockTagsApi = new MockAdapter(tagsApi.getAxios()); |
| 33 | + const tagsStore = useTagsStore(); |
| 34 | + tagsStore.showTags = true; |
| 35 | + |
| 36 | + localStorage.setItem("tenant", "fake-tenant-data"); |
| 37 | + envVariables.isCloud = true; |
| 38 | + |
| 39 | + mockTagsApi |
| 40 | + .onGet("http://localhost:3000/api/namespaces/fake-tenant-data/tags?filter=&page=1&per_page=10") |
| 41 | + .reply(200, mockTags, { "x-total-count": "2" }); |
| 42 | + |
| 43 | + const wrapper = mount(Tags, { global: { plugins: [vuetify, SnackbarPlugin] } }); |
| 44 | + await flushPromises(); |
| 45 | + |
| 46 | + it("Renders the main heading", () => { |
| 47 | + expect(wrapper.find("h1").text()).toBe("Tags"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("Renders the TagList component with 2 tag rows", () => { |
| 51 | + const tagList = wrapper.findComponent(TagList); |
| 52 | + expect(tagList.exists()).toBe(true); |
| 53 | + |
| 54 | + const tagRows = tagList.findAll('[data-test="tag-name"]'); |
| 55 | + expect(tagRows).toHaveLength(2); |
| 56 | + expect(tagRows[0].text()).toBe("1"); |
| 57 | + expect(tagRows[1].text()).toBe("2"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("Renders the search field when tags exist", () => { |
| 61 | + const searchField = wrapper.find('[data-test="search-text"]'); |
| 62 | + expect(searchField.exists()).toBe(true); |
| 63 | + expect(searchField.text()).toContain("Search by Tag Name"); // Input label |
| 64 | + }); |
| 65 | + |
| 66 | + it("Renders the create tag button when tags exist", () => { |
| 67 | + const createButton = wrapper.find('[data-test="tag-create-button"]'); |
| 68 | + expect(createButton.exists()).toBe(true); |
| 69 | + expect(createButton.text()).toBe("Create Tag"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("Renders NoItemsMessage when no tags exist", async () => { |
| 73 | + tagsStore.showTags = false; |
| 74 | + mockTagsApi.reset(); |
| 75 | + mockTagsApi |
| 76 | + .onGet("http://localhost:3000/api/namespaces/fake-tenant-data/tags?filter=&page=1&per_page=10") |
| 77 | + .reply(200, [], { "x-total-count": "0" }); |
| 78 | + |
| 79 | + wrapper.unmount(); |
| 80 | + const emptyWrapper = mount(Tags, { global: { plugins: [vuetify, SnackbarPlugin] } }); |
| 81 | + await flushPromises(); |
| 82 | + |
| 83 | + expect(emptyWrapper.findComponent(TagList).exists()).toBe(false); |
| 84 | + expect(emptyWrapper.find('[data-test="search-text"]').exists()).toBe(false); |
| 85 | + |
| 86 | + const noItemsMessage = emptyWrapper.findComponent(NoItemsMessage); |
| 87 | + expect(noItemsMessage.exists()).toBe(true); |
| 88 | + expect(noItemsMessage.props("item")).toBe("Tags"); |
| 89 | + expect(noItemsMessage.props("icon")).toBe("mdi-tag-multiple"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments