|
| 1 | +import { setActivePinia, createPinia } from "pinia"; |
| 2 | +import { createVuetify } from "vuetify"; |
| 3 | +import { flushPromises, mount, VueWrapper } from "@vue/test-utils"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { VLayout } from "vuetify/components"; |
| 6 | +import InvitationsMenu from "@/components/Invitations/InvitationsMenu.vue"; |
| 7 | +import { SnackbarInjectionKey } from "@/plugins/snackbar"; |
| 8 | +import useInvitationsStore from "@/store/modules/invitations"; |
| 9 | +import { IInvitation } from "@/interfaces/IInvitation"; |
| 10 | + |
| 11 | +const Component = { |
| 12 | + template: "<v-layout><InvitationsMenu v-model=\"show\" /></v-layout>", |
| 13 | + props: ["modelValue"], |
| 14 | + data: () => ({ |
| 15 | + show: true, |
| 16 | + }), |
| 17 | +}; |
| 18 | + |
| 19 | +const mockSnackbar = { |
| 20 | + showSuccess: vi.fn(), |
| 21 | + showError: vi.fn(), |
| 22 | +}; |
| 23 | + |
| 24 | +const mockInvitations: IInvitation[] = [ |
| 25 | + { |
| 26 | + status: "pending", |
| 27 | + role: "operator", |
| 28 | + invited_by: "admin", |
| 29 | + expires_at: "2025-12-31T23:59:59Z", |
| 30 | + created_at: "2025-12-01T00:00:00Z", |
| 31 | + updated_at: "2025-12-01T00:00:00Z", |
| 32 | + status_updated_at: "2025-12-01T00:00:00Z", |
| 33 | + namespace: { |
| 34 | + tenant_id: "tenant1", |
| 35 | + name: "Namespace 1", |
| 36 | + }, |
| 37 | + user: { |
| 38 | + id: "user1", |
| 39 | + email: "user@example.com", |
| 40 | + }, |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +const vuetify = createVuetify(); |
| 45 | + |
| 46 | +const mountWrapper = () => mount(Component, { |
| 47 | + global: { |
| 48 | + plugins: [vuetify], |
| 49 | + provide: { [SnackbarInjectionKey]: mockSnackbar }, |
| 50 | + components: { "v-layout": VLayout, InvitationsMenu }, |
| 51 | + stubs: { teleport: true }, |
| 52 | + }, |
| 53 | + props: { modelValue: true }, |
| 54 | + attachTo: document.body, |
| 55 | +}); |
| 56 | + |
| 57 | +describe("InvitationsMenu", () => { |
| 58 | + let wrapper: VueWrapper<unknown>; |
| 59 | + let menu: VueWrapper<InstanceType<typeof InvitationsMenu>>; |
| 60 | + |
| 61 | + setActivePinia(createPinia()); |
| 62 | + const invitationsStore = useInvitationsStore(); |
| 63 | + invitationsStore.fetchUserPendingInvitationList = vi.fn().mockResolvedValue(Promise.resolve(mockInvitations)); |
| 64 | + |
| 65 | + it("Opens drawer when icon is clicked", async () => { |
| 66 | + wrapper = mountWrapper(); |
| 67 | + await wrapper.find('[data-test="invitations-menu-icon"]').trigger("click"); |
| 68 | + menu = wrapper.findComponent(InvitationsMenu); |
| 69 | + menu.vm.isDrawerOpen = false; |
| 70 | + const icon = wrapper.find('[data-test="invitations-menu-icon"]'); |
| 71 | + await icon.trigger("click"); |
| 72 | + await flushPromises(); |
| 73 | + |
| 74 | + expect(menu.vm.isDrawerOpen).toBe(true); |
| 75 | + const drawerComponent = wrapper.find('[data-test="invitations-drawer"]'); |
| 76 | + expect(drawerComponent.exists()).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it("Fetches invitations on mount", async () => { |
| 80 | + const storeSpy = vi.spyOn(invitationsStore, "fetchUserPendingInvitationList"); |
| 81 | + wrapper.unmount(); |
| 82 | + wrapper = mountWrapper(); |
| 83 | + await flushPromises(); |
| 84 | + expect(storeSpy).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments