Skip to content

Commit

Permalink
add sanitize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igatanasov committed Nov 13, 2024
1 parent 76cad53 commit fe7f887
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions packages/editor-ui/src/composables/useToast.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useToast } from './useToast';
import { createPinia, setActivePinia } from 'pinia';
import { sanitizeHtml } from '@/utils/htmlUtils';

vi.mock('@/utils/htmlUtils', () => ({
sanitizeHtml: vi.fn((str) => str),
}));
import { ElNotification as Notification } from 'element-plus';

vi.mock('element-plus', async () => {
const original = await vi.importActual('element-plus');
return {
...original,
ElNotification: vi.fn(),
ElTooltip: vi.fn(),
};
});

describe('useToast', () => {
let toast: ReturnType<typeof useToast>;
Expand All @@ -16,11 +21,20 @@ describe('useToast', () => {
toast = useToast();
});

afterEach(() => {
vi.restoreAllMocks();
});

it('should show a message', () => {
const messageData = { message: 'Test message', title: 'Test title' };
const notification = toast.showMessage(messageData);
toast.showMessage(messageData);

expect(notification).toBeDefined();
expect(Notification).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Test message',
title: 'Test title',
}),
);
});

it('should sanitize message and title', () => {
Expand All @@ -31,7 +45,11 @@ describe('useToast', () => {

toast.showMessage(messageData);

expect(sanitizeHtml).toHaveBeenCalledWith(messageData.message);
expect(sanitizeHtml).toHaveBeenCalledWith(messageData.title);
expect(Notification).toHaveBeenCalledWith(
expect.objectContaining({
message: 'alert("xss")',
title: 'alert("xss")',
}),
);
});
});

0 comments on commit fe7f887

Please sign in to comment.