|
| 1 | +import { describe, it, beforeEach, expect, vi } from 'vitest'; |
| 2 | +import { fireEvent, render } from '@testing-library/svelte'; |
| 3 | +import { readable } from 'svelte/store'; |
| 4 | + |
| 5 | +// Mocking fetch |
| 6 | +global.fetch = vi.fn(); |
| 7 | + |
| 8 | +// Mocking the page store |
| 9 | +vi.mock('$app/stores', () => { |
| 10 | + return { |
| 11 | + page: readable({ |
| 12 | + data: { |
| 13 | + users: [ |
| 14 | + { id: 1, email: '[email protected]' }, |
| 15 | + { id: 2, email: '[email protected]' } |
| 16 | + ], |
| 17 | + group: { |
| 18 | + name: 'test', |
| 19 | + user_ids: [1] |
| 20 | + } |
| 21 | + } |
| 22 | + }) |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +// The component to be tested must be imported after the mock setup |
| 27 | +import page from '../../src/routes/v2/admin/groups/[groupId]/edit/+page.svelte'; |
| 28 | + |
| 29 | +describe('Admin group edit page', () => { |
| 30 | + beforeEach(() => { |
| 31 | + fetch.mockClear(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('User is added successfully', async () => { |
| 35 | + const result = render(page); |
| 36 | + |
| 37 | + fetch.mockResolvedValue({ |
| 38 | + ok: true, |
| 39 | + status: 200, |
| 40 | + json: async () => ({ name: 'test', user_ids: [1, 2] }) |
| 41 | + }); |
| 42 | + fireEvent.dragStart(result.getByRole('button', { name: '[email protected]' })); |
| 43 | + fireEvent.drop(result.getByText(/drag the users here/i)); |
| 44 | + |
| 45 | + await result.findByText(/No more users available/); |
| 46 | + expect(result.queryByRole('button', { name: '[email protected]' })).null; |
| 47 | + }); |
| 48 | + |
| 49 | + it('Error happens when user is dragged and dropped', async () => { |
| 50 | + const result = render(page); |
| 51 | + |
| 52 | + fetch.mockResolvedValue({ |
| 53 | + ok: false, |
| 54 | + status: 422, |
| 55 | + json: async () => ({ detail: 'An error happened' }) |
| 56 | + }); |
| 57 | + |
| 58 | + fireEvent.dragStart(result.getByRole('button', { name: '[email protected]' })); |
| 59 | + fireEvent.drop(result.getByText(/drag the users here/i)); |
| 60 | + |
| 61 | + await result.findByText(/An error happened/); |
| 62 | + expect(result.queryByRole('button', { name: '[email protected]' })).not.null; |
| 63 | + }); |
| 64 | +}); |
0 commit comments