node-history-panel.test.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import { describe, expect, it, vi } from 'vitest';
  3. import NodeHistoryPanel from '@/pages/nodes/NodeHistoryPanel';
  4. import { HttpUtil, Msg } from '@/utils';
  5. const plots = vi.hoisted(() => [] as { scales: { y: { range: () => [number, number] } } }[]);
  6. vi.mock('uplot', () => ({
  7. default: class {
  8. static paths = { spline: () => undefined };
  9. static pxRatio = 1;
  10. constructor(opts: (typeof plots)[number]) {
  11. plots.push(opts);
  12. }
  13. setData() {}
  14. setSize() {}
  15. redraw() {}
  16. destroy() {}
  17. },
  18. }));
  19. // The net series fell through to Sparkline's percentage defaults: a 0-100 scale
  20. // and a "%" label, so 512 KB/s rendered as "512%" far above the chart.
  21. describe('NodeHistoryPanel', () => {
  22. it('charts net throughput in KB/s on its own scale', async () => {
  23. const samples: Record<string, number> = {
  24. cpu: 40,
  25. mem: 60,
  26. netUp: 512 * 1024,
  27. netDown: 200 * 1024,
  28. };
  29. vi.spyOn(HttpUtil, 'get').mockImplementation(async (url: string) => {
  30. const metric = url.split('/').at(-2) ?? '';
  31. return new Msg(true, '', [{ t: 1_700_000_000, v: samples[metric] }]);
  32. });
  33. render(<NodeHistoryPanel node={{ id: 7 }} />);
  34. // Sparkline updates its chart refs in a passive effect after the DOM commits.
  35. await waitFor(() => {
  36. expect(screen.getAllByRole('img')).toHaveLength(4);
  37. expect(screen.getAllByRole('img').map((el) => el.getAttribute('aria-label'))).toEqual([
  38. '40%',
  39. '60%',
  40. '512',
  41. '200',
  42. ]);
  43. expect(plots.map((p) => p.scales.y.range())).toEqual([
  44. [0, 100],
  45. [0, 100],
  46. [0, 512 * 1.1],
  47. [0, 200 * 1.1],
  48. ]);
  49. });
  50. });
  51. });