|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { expect, describe, it } from 'vitest' |
| 3 | +import ResultGallery from '../ResultGallery.vue' |
| 4 | +import Galleria from 'primevue/galleria' |
| 5 | +import ComfyImage from '@/components/common/ComfyImage.vue' |
| 6 | +import PrimeVue from 'primevue/config' |
| 7 | +import { ResultItemImpl } from '@/stores/queueStore' |
| 8 | + |
| 9 | +type ResultGalleryProps = typeof ResultGallery.__props |
| 10 | + |
| 11 | +describe('ResultGallery', () => { |
| 12 | + let mockResultItem: ResultItemImpl |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + mockResultItem = { |
| 16 | + filename: 'test.jpg', |
| 17 | + type: 'images', |
| 18 | + nodeId: 'test', |
| 19 | + mediaType: 'images', |
| 20 | + url: 'https://picsum.photos/200/300', |
| 21 | + urlWithTimestamp: 'https://picsum.photos/200/300?t=123456', |
| 22 | + supportsPreview: true |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + const mountResultGallery = (props: ResultGalleryProps, options = {}) => { |
| 27 | + return mount(ResultGallery, { |
| 28 | + global: { |
| 29 | + plugins: [PrimeVue], |
| 30 | + components: { Galleria, ComfyImage } |
| 31 | + }, |
| 32 | + props, |
| 33 | + ...options |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + const clickElement = async (element: Element) => { |
| 38 | + element.dispatchEvent(new MouseEvent('mousedown')) |
| 39 | + element.dispatchEvent(new MouseEvent('mouseup')) |
| 40 | + } |
| 41 | + |
| 42 | + it('is dismissed when overlay mask is clicked', async () => { |
| 43 | + const wrapper = mountResultGallery({ |
| 44 | + activeIndex: 0, |
| 45 | + allGalleryItems: [mockResultItem] |
| 46 | + }) |
| 47 | + wrapper.vm.galleryVisible = true |
| 48 | + await wrapper.vm.$nextTick() |
| 49 | + expect(wrapper.findComponent(Galleria).exists()).toBe(true) |
| 50 | + expect(wrapper.vm.galleryVisible).toBe(true) |
| 51 | + |
| 52 | + // Since Galleria uses teleport, we need to query the mask in the global document |
| 53 | + const mask = document.querySelector('[data-mask]') |
| 54 | + expect(mask).not.toBeNull() |
| 55 | + |
| 56 | + // Click the overlay mask to dismiss the gallery |
| 57 | + await clickElement(mask) |
| 58 | + await wrapper.vm.$nextTick() |
| 59 | + expect(wrapper.vm.galleryVisible).toBe(false) |
| 60 | + }) |
| 61 | + |
| 62 | + it('is not dismissed when gallery is clicked', async () => { |
| 63 | + const wrapper = mountResultGallery({ |
| 64 | + activeIndex: 0, |
| 65 | + allGalleryItems: [mockResultItem] |
| 66 | + }) |
| 67 | + wrapper.vm.galleryVisible = true |
| 68 | + await wrapper.vm.$nextTick() |
| 69 | + expect(wrapper.findComponent(Galleria).exists()).toBe(true) |
| 70 | + expect(wrapper.vm.galleryVisible).toBe(true) |
| 71 | + |
| 72 | + // Since Galleria uses teleport, we need to query the mask in the global document |
| 73 | + const gallery = document.querySelector('.p-galleria-content') |
| 74 | + expect(gallery).not.toBeNull() |
| 75 | + |
| 76 | + // The gallery should not be dismissed when the gallery itself is clicked |
| 77 | + await clickElement(gallery) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + expect(wrapper.vm.galleryVisible).toBe(true) |
| 80 | + }) |
| 81 | +}) |
0 commit comments