speed-tag-stable.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { render } from '@testing-library/react';
  2. import { describe, expect, it } from 'vitest';
  3. import { ClientSpeedTag } from '@/components/clients/ClientSpeedTag';
  4. import {
  5. SPEED_COLUMN_WIDTH,
  6. SPEED_TABLE_CELL_INLINE_PADDING,
  7. SPEED_TAG_CLASS_NAME,
  8. SPEED_TAG_STYLE,
  9. SPEED_TAG_WIDTH,
  10. } from '@/components/utility/speedTagStyle';
  11. import { InboundSpeedTag } from '@/pages/inbounds/list/InboundSpeedTag';
  12. import { SizeFormatter } from '@/utils';
  13. import '@/styles/utils.css';
  14. const SMALL_RATE = { up: 512, down: 1023 } as const;
  15. const LARGE_RATE = { up: 12_340_000, down: 99_990_000 } as const;
  16. const FORMATTER_BOUNDARY_RATE = {
  17. up: SizeFormatter.ONE_GB - 1,
  18. down: SizeFormatter.ONE_GB - 1,
  19. } as const;
  20. const FORMATTER_BOUNDARY_NATURAL_WIDTH = 192.765625;
  21. function firstTag(): HTMLElement {
  22. const tag = document.querySelector('.ant-tag');
  23. if (!(tag instanceof HTMLElement)) {
  24. throw new Error('expected an Ant Design Tag in the document');
  25. }
  26. return tag;
  27. }
  28. function expectFluidTag(tag: HTMLElement) {
  29. expect(tag.classList.contains(SPEED_TAG_CLASS_NAME)).toBe(false);
  30. expect(tag.style.width).toBe('');
  31. }
  32. function expectStableTableTag(tag: HTMLElement) {
  33. const style = getComputedStyle(tag);
  34. expect(tag.classList.contains(SPEED_TAG_CLASS_NAME)).toBe(true);
  35. expect(style.width).toBe(`${SPEED_TAG_WIDTH}px`);
  36. expect(style.display).toBe(SPEED_TAG_STYLE.display);
  37. expect(style.justifyContent).toBe(SPEED_TAG_STYLE.justifyContent);
  38. expect(style.alignItems).toBe(SPEED_TAG_STYLE.alignItems);
  39. expect(style.textAlign).toBe(SPEED_TAG_STYLE.textAlign);
  40. expect(style.whiteSpace).toBe(SPEED_TAG_STYLE.whiteSpace);
  41. expect(style.fontVariantNumeric).toBe(SPEED_TAG_STYLE.fontVariantNumeric);
  42. expect(style.overflow).toBe('hidden');
  43. expect(style.textOverflow).toBe('ellipsis');
  44. }
  45. describe('stable table speed tags (issue #5912)', () => {
  46. it('scopes ClientSpeedTag stable sizing to table cells', () => {
  47. const { rerender } = render(<ClientSpeedTag speed={SMALL_RATE} />);
  48. expectFluidTag(firstTag());
  49. rerender(<ClientSpeedTag speed={LARGE_RATE} tableCell />);
  50. expectStableTableTag(firstTag());
  51. });
  52. it('scopes InboundSpeedTag stable sizing to table cells', () => {
  53. const { rerender } = render(<InboundSpeedTag speed={SMALL_RATE} />);
  54. expectFluidTag(firstTag());
  55. rerender(<InboundSpeedTag speed={LARGE_RATE} withTooltip tableCell />);
  56. expectStableTableTag(firstTag());
  57. });
  58. it('fits the widest formatter rollover and includes small-cell padding', () => {
  59. render(
  60. <>
  61. <ClientSpeedTag speed={FORMATTER_BOUNDARY_RATE} tableCell />
  62. <InboundSpeedTag speed={FORMATTER_BOUNDARY_RATE} tableCell />
  63. </>,
  64. );
  65. const tags = Array.from(document.querySelectorAll<HTMLElement>('.ant-tag'));
  66. expect(tags).toHaveLength(2);
  67. expect(tags[0]?.textContent).toBe('↑ 1024.00 MB/s / ↓ 1024.00 MB/s');
  68. expect(tags[1]?.textContent).toBe(tags[0]?.textContent);
  69. for (const tag of tags) expectStableTableTag(tag);
  70. expect(SPEED_TAG_WIDTH).toBeGreaterThan(FORMATTER_BOUNDARY_NATURAL_WIDTH);
  71. expect(SPEED_COLUMN_WIDTH).toBe(SPEED_TAG_WIDTH + SPEED_TABLE_CELL_INLINE_PADDING * 2);
  72. });
  73. });