CalendarView.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * The archives calendar shows prints, not archives (archive redesign P4): a
  3. * file printed twice this month is there twice, on the days it ended.
  4. */
  5. /// <reference types="@testing-library/jest-dom" />
  6. import { describe, it, expect, vi } from 'vitest';
  7. import { fireEvent, screen, waitFor } from '@testing-library/react';
  8. import { http, HttpResponse } from 'msw';
  9. import { render } from '../utils';
  10. import { server } from '../mocks/server';
  11. import { CalendarView } from '../../components/CalendarView';
  12. import type { Archive } from '../../api/client';
  13. const now = new Date();
  14. const onDay = (day: number) => new Date(now.getFullYear(), now.getMonth(), day, 12).toISOString();
  15. const archive = (overrides: Partial<Archive>) =>
  16. ({
  17. filename: 'f.3mf',
  18. print_name: 'File',
  19. status: 'completed',
  20. thumbnail_path: null,
  21. filament_color: null,
  22. run_count: 0,
  23. completed_at: null,
  24. created_at: onDay(1),
  25. ...overrides,
  26. }) as Archive;
  27. const run = (id: number, archiveId: number, day: number, status = 'completed') => ({
  28. id,
  29. archive_id: archiveId,
  30. status,
  31. completed_at: onDay(day),
  32. created_at: onDay(day),
  33. filament_color: null,
  34. });
  35. describe('CalendarView', () => {
  36. it('shows each print of an archive on its own day and opens it there', async () => {
  37. let asked: URLSearchParams | null = null;
  38. server.use(
  39. http.get('/api/v1/archives/prints', ({ request }) => {
  40. asked = new URL(request.url).searchParams;
  41. return HttpResponse.json({
  42. items: [run(31, 7, 3, 'failed'), run(32, 7, 5), run(33, 99, 5)],
  43. total: 3,
  44. });
  45. }),
  46. );
  47. const onPrintClick = vi.fn();
  48. render(
  49. <CalendarView
  50. archives={[
  51. archive({ id: 7, print_name: 'Planter', run_count: 2 }),
  52. // Printed before prints were logged: stands for its one print.
  53. archive({ id: 8, print_name: 'Hook', completed_at: onDay(5) }),
  54. archive({ id: 9, print_name: 'Never printed', status: 'archived' }),
  55. ]}
  56. printerId={2}
  57. onPrintClick={onPrintClick}
  58. />
  59. );
  60. // Three prints this month; archive 99 is one the page's filters hide.
  61. await waitFor(() => expect(screen.getByText('Prints this month').previousSibling?.textContent).toBe('3'));
  62. expect(asked!.get('printer_id')).toBe('2');
  63. // The month, as the calendar's local days.
  64. const last = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
  65. const month = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
  66. expect([asked!.get('date_from'), asked!.get('date_to')]).toEqual([`${month}-01`, `${month}-${last}`]);
  67. expect(screen.getByText('Failed').previousSibling?.textContent).toBe('1');
  68. fireEvent.click(screen.getByRole('button', { name: /^5/ }));
  69. fireEvent.click(await screen.findByText('Hook'));
  70. expect(onPrintClick).toHaveBeenLastCalledWith(8, null);
  71. fireEvent.click(screen.getByText('Planter'));
  72. expect(onPrintClick).toHaveBeenLastCalledWith(7, 32);
  73. expect(screen.queryByText('Never printed')).toBeNull();
  74. });
  75. it('reads every page of a busy month', async () => {
  76. const offsets: string[] = [];
  77. server.use(
  78. http.get('/api/v1/archives/prints', ({ request }) => {
  79. const offset = new URL(request.url).searchParams.get('offset') ?? '0';
  80. offsets.push(offset);
  81. const start = Number(offset);
  82. const items = Array.from({ length: offset === '0' ? 500 : 1 }, (_, i) => run(start + i + 1, 7, 2));
  83. return HttpResponse.json({ items, total: 501 });
  84. }),
  85. );
  86. render(<CalendarView archives={[archive({ id: 7, print_name: 'Planter', run_count: 501 })]} />);
  87. await waitFor(() => expect(screen.getByText('Prints this month').previousSibling?.textContent).toBe('501'));
  88. expect(offsets).toEqual(['0', '500']);
  89. });
  90. });