FilamentTrends.test.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Tests for the FilamentTrends widget's print counting.
  3. *
  4. * An archive edited down to 0 items produced nothing (#3051). The count here
  5. * used to read `quantity || 1`, which turned that 0 back into one print and
  6. * left this widget contradicting the project page.
  7. */
  8. import { describe, it, expect } from 'vitest';
  9. import { screen } from '@testing-library/react';
  10. import { render } from '../utils';
  11. import { FilamentTrends } from '../../components/FilamentTrends';
  12. import type { ArchiveSlim } from '../../api/client';
  13. const archive = (overrides: Partial<ArchiveSlim>): ArchiveSlim => ({
  14. printer_id: 1,
  15. print_name: 'Benchy',
  16. print_time_seconds: 3600,
  17. actual_time_seconds: 3600,
  18. filament_used_grams: 20,
  19. filament_type: 'PLA',
  20. filament_color: '#00ae42',
  21. status: 'completed',
  22. started_at: '2026-09-06T10:00:00Z',
  23. completed_at: '2026-09-06T11:00:00Z',
  24. cost: 1,
  25. energy_kwh: 0.1,
  26. energy_cost: 0.02,
  27. quantity: 1,
  28. created_at: '2026-09-06T10:00:00Z',
  29. ...overrides,
  30. });
  31. /** The "<n> prints" caption under the summary heading. */
  32. const printCount = () =>
  33. screen
  34. .getAllByText((_, el) => el?.tagName === 'P' && /^\d+ prints$/.test(el.textContent ?? ''))
  35. .map((el) => el.textContent)[0];
  36. describe('FilamentTrends print count (#3051)', () => {
  37. it('counts a ruined plate as zero prints, not one', () => {
  38. render(<FilamentTrends archives={[archive({ quantity: 0 }), archive({ quantity: 3 })]} />);
  39. expect(printCount()).toBe('3 prints');
  40. });
  41. it('still treats a missing quantity as a single print', () => {
  42. const noQuantity = archive({});
  43. delete (noQuantity as Partial<ArchiveSlim>).quantity;
  44. render(<FilamentTrends archives={[noQuantity as ArchiveSlim]} />);
  45. expect(printCount()).toBe('1 prints');
  46. });
  47. });