import { render } from '@testing-library/vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue'
import { createI18n } from 'vue-i18n'
import SecretFormDialog from './SecretFormDialog.vue'
vi.mock('../composables/useSecretForm', () => ({
useSecretForm: () => ({
form: { provider: '', name: '', secretValue: '' },
errors: {},
loading: false,
apiError: '',
providerOptions: [],
handleSubmit: vi.fn()
})
}))
vi.mock('primevue/inputtext', () => ({
default: { name: 'InputText', template: '' }
}))
vi.mock('primevue/password', () => ({
default: { name: 'Password', template: '' }
}))
let capturedPointerDownOutside: ((event: Event) => void) | null = null
vi.mock('@/components/ui/button/Button.vue', () => ({
default: { name: 'Button', template: '' }
}))
vi.mock('@/components/ui/select/Select.vue', () => ({
default: { name: 'Select', template: '
' }
}))
vi.mock('@/components/ui/select/SelectContent.vue', () => ({
default: { name: 'SelectContent', template: '
' }
}))
vi.mock('@/components/ui/select/SelectItem.vue', () => ({
default: { name: 'SelectItem', template: '
' }
}))
vi.mock('@/components/ui/select/SelectTrigger.vue', () => ({
default: { name: 'SelectTrigger', template: '
' }
}))
vi.mock('@/components/ui/select/SelectValue.vue', () => ({
default: { name: 'SelectValue', template: '' }
}))
vi.mock('@/components/ui/dialog/Dialog.vue', () => ({
default: { name: 'Dialog', template: '
' }
}))
vi.mock('@/components/ui/dialog/DialogPortal.vue', () => ({
default: { name: 'DialogPortal', template: '
' }
}))
vi.mock('@/components/ui/dialog/DialogOverlay.vue', () => ({
default: { name: 'DialogOverlay', template: '' }
}))
vi.mock('@/components/ui/dialog/DialogContent.vue', () => ({
default: defineComponent({
name: 'DialogContent',
inheritAttrs: false,
setup(_, { attrs }) {
const onPointerDownOutside = (attrs as Record)[
'onPointerDownOutside'
] as ((event: Event) => void) | undefined
capturedPointerDownOutside = onPointerDownOutside ?? null
},
template: '
'
})
}))
vi.mock('@/components/ui/dialog/DialogHeader.vue', () => ({
default: { name: 'DialogHeader', template: '
' }
}))
vi.mock('@/components/ui/dialog/DialogTitle.vue', () => ({
default: { name: 'DialogTitle', template: '
' }
}))
vi.mock('@/components/ui/dialog/DialogClose.vue', () => ({
default: { name: 'DialogClose', template: '' }
}))
const i18n = createI18n({ legacy: false, locale: 'en', messages: { en: {} } })
describe('SecretFormDialog', () => {
beforeEach(() => {
capturedPointerDownOutside = null
})
it('prevents backdrop pointer-down-outside from closing the dialog', () => {
render(SecretFormDialog, {
global: { plugins: [i18n] },
props: { visible: true }
})
expect(capturedPointerDownOutside).not.toBeNull()
const event = new CustomEvent('pointerDownOutside', { cancelable: true })
capturedPointerDownOutside!(event)
expect(event.defaultPrevented).toBe(true)
})
})