|
| 1 | +import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import { ObjectKanban } from './ObjectKanban'; |
| 5 | +import { ObjectStackAdapter } from '@object-ui/data-objectstack'; |
| 6 | +import { setupServer } from 'msw/node'; |
| 7 | +import { http, HttpResponse } from 'msw'; |
| 8 | +import React from 'react'; |
| 9 | + |
| 10 | +// Register layout components (if needed by cards) |
| 11 | +// registerLayout(); |
| 12 | + |
| 13 | +const BASE_URL = 'http://localhost'; |
| 14 | + |
| 15 | +// --- Mock Data --- |
| 16 | + |
| 17 | +const mockTasks = { |
| 18 | + value: [ |
| 19 | + { _id: '1', title: 'Task 1', status: 'todo', description: 'Description 1' }, |
| 20 | + { _id: '2', title: 'Task 2', status: 'done', description: 'Description 2' }, |
| 21 | + { _id: '3', title: 'Task 3', status: 'todo', description: 'Description 3' } |
| 22 | + ] |
| 23 | +}; |
| 24 | + |
| 25 | +// --- MSW Setup --- |
| 26 | + |
| 27 | +const handlers = [ |
| 28 | + // OPTIONS handler for CORS preflight checks |
| 29 | + http.options('*', () => { |
| 30 | + return new HttpResponse(null, { status: 200 }); |
| 31 | + }), |
| 32 | + |
| 33 | + // Health check |
| 34 | + http.get(`${BASE_URL}/api/v1`, () => { |
| 35 | + return HttpResponse.json({ status: 'ok', version: '1.0.0' }); |
| 36 | + }), |
| 37 | + |
| 38 | + // Data Query: GET /api/v1/data/tasks |
| 39 | + http.get(`${BASE_URL}/api/v1/data/tasks`, () => { |
| 40 | + return HttpResponse.json(mockTasks); |
| 41 | + }) |
| 42 | +]; |
| 43 | + |
| 44 | +const server = setupServer(...handlers); |
| 45 | + |
| 46 | +// --- Test Suite --- |
| 47 | + |
| 48 | +describe('ObjectKanban with MSW', () => { |
| 49 | + if (!process.env.OBJECTSTACK_API_URL) { |
| 50 | + beforeAll(() => server.listen()); |
| 51 | + afterEach(() => server.resetHandlers()); |
| 52 | + afterAll(() => server.close()); |
| 53 | + } |
| 54 | + |
| 55 | + const dataSource = new ObjectStackAdapter({ |
| 56 | + baseUrl: BASE_URL, |
| 57 | + }); |
| 58 | + |
| 59 | + it('fetches tasks and renders them in columns based on groupBy', async () => { |
| 60 | + render( |
| 61 | + <ObjectKanban |
| 62 | + schema={{ |
| 63 | + type: 'kanban', |
| 64 | + objectName: 'tasks', |
| 65 | + groupBy: 'status', |
| 66 | + columns: [ |
| 67 | + { id: 'todo', title: 'To Do', cards: [] }, |
| 68 | + { id: 'done', title: 'Done', cards: [] } |
| 69 | + ] |
| 70 | + }} |
| 71 | + dataSource={dataSource} |
| 72 | + /> |
| 73 | + ); |
| 74 | + |
| 75 | + // Initial state might show Skeleton, wait for data |
| 76 | + await waitFor(() => { |
| 77 | + expect(screen.getByText('Task 1')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + // Check classification |
| 81 | + // Task 1 (todo) and Task 3 (todo) should be in To Do column. |
| 82 | + // Task 2 (done) should be in Done column. |
| 83 | + |
| 84 | + // We can verify "Task 1" is present. |
| 85 | + expect(screen.getByText('Task 2')).toBeInTheDocument(); |
| 86 | + expect(screen.getByText('Task 3')).toBeInTheDocument(); |
| 87 | + |
| 88 | + // Check descriptions |
| 89 | + expect(screen.getByText('Description 1')).toBeInTheDocument(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments