BatchProjectModal.test.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * BatchProjectModal: the "Added N" toast counts what the server added. The
  3. * server skips archives the user may not change (other teams' printers, other
  4. * users' prints), so the number selected can be higher.
  5. */
  6. import { describe, it, expect, vi, beforeEach } from 'vitest';
  7. import { render, screen, waitFor } from '@testing-library/react';
  8. import userEvent from '@testing-library/user-event';
  9. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  10. import { BatchProjectModal } from '../../components/BatchProjectModal';
  11. import { api } from '../../api/client';
  12. const mockShowToast = vi.fn();
  13. vi.mock('../../api/client', () => ({
  14. api: {
  15. getProjects: vi.fn(),
  16. addArchivesToProject: vi.fn(),
  17. updateArchive: vi.fn(),
  18. },
  19. }));
  20. vi.mock('../../contexts/ToastContext', () => ({
  21. useToast: () => ({ showToast: mockShowToast }),
  22. }));
  23. describe('BatchProjectModal', () => {
  24. beforeEach(() => {
  25. vi.clearAllMocks();
  26. (api.getProjects as ReturnType<typeof vi.fn>).mockResolvedValue([
  27. { id: 7, name: 'Voron', status: 'active', parent_id: null },
  28. ]);
  29. });
  30. it('reports the number of archives the server added, not the number selected', async () => {
  31. (api.addArchivesToProject as ReturnType<typeof vi.fn>).mockResolvedValue({
  32. message: 'Added 1 archives to project',
  33. count: 1,
  34. });
  35. const user = userEvent.setup();
  36. const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  37. render(
  38. <QueryClientProvider client={client}>
  39. <BatchProjectModal selectedIds={[1, 2, 3]} onClose={vi.fn()} />
  40. </QueryClientProvider>,
  41. );
  42. await user.click(await screen.findByText('Voron'));
  43. await waitFor(() => expect(mockShowToast).toHaveBeenCalledWith('Added 1 archive to "Voron"'));
  44. expect(api.addArchivesToProject).toHaveBeenCalledWith(7, [1, 2, 3]);
  45. });
  46. });