import { screen, waitFor, within } from '@testing-library/vue'; import { createTestingPinia } from '@pinia/testing'; import { h, defineComponent } from 'vue'; import { useToast } from './useToast'; describe('useToast', () => { let toast: ReturnType; beforeEach(() => { document.body.innerHTML = '
'; createTestingPinia(); toast = useToast(); }); it('should show a message', async () => { const messageData = { message: 'Test message', title: 'Test title' }; toast.showMessage(messageData); await waitFor(() => { expect(screen.getByRole('alert')).toBeVisible(); expect( within(screen.getByRole('alert')).getByRole('heading', { level: 2 }), ).toHaveTextContent('Test title'); expect(screen.getByRole('alert')).toContainHTML('

Test message

'); }); }); it('should sanitize message and title', async () => { const messageData = { message: '', title: '', }; toast.showMessage(messageData); await waitFor(() => { expect(screen.getByRole('alert')).toBeVisible(); expect( within(screen.getByRole('alert')).getByRole('heading', { level: 2 }), ).toHaveTextContent('alert("xss")'); expect(screen.getByRole('alert')).toContainHTML('

alert("xss")

'); }); }); it('should sanitize but keep valid, allowed HTML tags', async () => { const messageData = { message: 'Refresh to see the latest status.
More info or go to the Usage and plan settings page.', title: 'Title', }; toast.showMessage(messageData); await waitFor(() => { expect(screen.getByRole('alert')).toBeVisible(); expect( within(screen.getByRole('alert')).getByRole('heading', { level: 2 }), ).toHaveTextContent('Title'); expect( within(screen.getByRole('alert')).getByRole('heading', { level: 2 }).querySelectorAll('*'), ).toHaveLength(0); expect(screen.getByRole('alert')).toContainHTML( 'Refresh to see the latest status.
More info or go to the Usage and plan settings page.', ); }); }); it('should render component as message, sanitized as well', async () => { const messageData = { message: h( defineComponent({ template: '

Test content

', }), ), }; toast.showMessage(messageData); await waitFor(() => { expect(screen.getByRole('alert')).toBeVisible(); expect( within(screen.getByRole('alert')).queryByRole('heading', { level: 2 }), ).toHaveTextContent(''); expect( within(screen.getByRole('alert')).getByRole('heading', { level: 2 }).querySelectorAll('*'), ).toHaveLength(0); expect(screen.getByRole('alert')).toContainHTML('

Test content

'); }); }); });