|
| 1 | +import { describe, it, beforeEach, expect, vi } from 'vitest'; |
| 2 | +import { 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 | + workflow: { |
| 14 | + id: 1, |
| 15 | + name: 'test', |
| 16 | + project_id: 1, |
| 17 | + task_list: [{ id: 1, workflow_id: 1, task_id: 1, task: { id: 1, name: 'test' } }], |
| 18 | + project: { id: 1, name: 'test' } |
| 19 | + }, |
| 20 | + datasets: [{ id: 1, name: 'test' }], |
| 21 | + defaultDatasetId: 1, |
| 22 | + userInfo: { id: 1, slurm_accounts: [] } |
| 23 | + }, |
| 24 | + params: { projectId: 1 } |
| 25 | + }) |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +// Mocking public variables |
| 30 | +vi.mock('$env/dynamic/public', () => { |
| 31 | + return { env: {} }; |
| 32 | +}); |
| 33 | + |
| 34 | +// The component to be tested must be imported after the mock setup |
| 35 | +import page from '../../src/routes/v2/projects/[projectId]/workflows/[workflowId]/+page.svelte'; |
| 36 | + |
| 37 | +describe('Workflow page', () => { |
| 38 | + beforeEach(() => { |
| 39 | + fetch.mockClear(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('Display error when submission failed before starting execution of tasks', async () => { |
| 43 | + fetch.mockImplementation((url) => { |
| 44 | + return Promise.resolve({ |
| 45 | + ok: true, |
| 46 | + status: 200, |
| 47 | + json: async () => { |
| 48 | + switch (url) { |
| 49 | + case '/api/v2/task': |
| 50 | + return [{ id: 1, workflow_id: 1, task_id: 1, task: { id: 1, name: 'test' } }]; |
| 51 | + case '/api/v2/task-legacy': |
| 52 | + return { data: [] }; |
| 53 | + case '/api/v2/project/1/dataset/1': |
| 54 | + return { id: 1, name: 'test' }; |
| 55 | + case '/api/v2/project/1/status?dataset_id=1&workflow_id=1': |
| 56 | + return { status: {} }; // status is an empty object, since no tasks started |
| 57 | + case '/api/v2/project/1/workflow/1/job': |
| 58 | + return [ |
| 59 | + { |
| 60 | + id: 1, |
| 61 | + project_id: 1, |
| 62 | + workflow_id: 1, |
| 63 | + dataset_id: 1, |
| 64 | + status: 'failed', |
| 65 | + log: 'Exception error occurred while creating job folder and subfolders.\nOriginal error: test' |
| 66 | + } |
| 67 | + ]; |
| 68 | + default: |
| 69 | + throw Error(`Unexpected API call: ${url}`); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + const result = render(page); |
| 76 | + expect( |
| 77 | + await result.findByText(/Exception error occurred while creating job folder and subfolders/) |
| 78 | + ).toBeDefined(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments