useChildrenState.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { renderHook, act } from '@testing-library/react-hooks';
  2. import { TraceSpan } from '@jaegertracing/jaeger-ui-components';
  3. import { useChildrenState } from './useChildrenState';
  4. describe('useChildrenState', () => {
  5. describe('childrenToggle', () => {
  6. it('toggles children state', async () => {
  7. const { result } = renderHook(() => useChildrenState());
  8. expect(result.current.childrenHiddenIDs.size).toBe(0);
  9. act(() => result.current.childrenToggle('testId'));
  10. expect(result.current.childrenHiddenIDs.size).toBe(1);
  11. expect(result.current.childrenHiddenIDs.has('testId')).toBe(true);
  12. act(() => result.current.childrenToggle('testId'));
  13. expect(result.current.childrenHiddenIDs.size).toBe(0);
  14. });
  15. });
  16. describe('expandAll', () => {
  17. it('expands all', async () => {
  18. const { result } = renderHook(() => useChildrenState());
  19. act(() => result.current.childrenToggle('testId1'));
  20. act(() => result.current.childrenToggle('testId2'));
  21. expect(result.current.childrenHiddenIDs.size).toBe(2);
  22. act(() => result.current.expandAll());
  23. expect(result.current.childrenHiddenIDs.size).toBe(0);
  24. });
  25. });
  26. describe('collapseAll', () => {
  27. it('hides spans that have children', async () => {
  28. const { result } = renderHook(() => useChildrenState());
  29. act(() =>
  30. result.current.collapseAll([
  31. { spanID: 'span1', hasChildren: true } as TraceSpan,
  32. { spanID: 'span2', hasChildren: false } as TraceSpan,
  33. ])
  34. );
  35. expect(result.current.childrenHiddenIDs.size).toBe(1);
  36. expect(result.current.childrenHiddenIDs.has('span1')).toBe(true);
  37. });
  38. it('does nothing if already collapsed', async () => {
  39. const { result } = renderHook(() => useChildrenState());
  40. act(() => result.current.childrenToggle('span1'));
  41. act(() =>
  42. result.current.collapseAll([
  43. { spanID: 'span1', hasChildren: true } as TraceSpan,
  44. { spanID: 'span2', hasChildren: false } as TraceSpan,
  45. ])
  46. );
  47. expect(result.current.childrenHiddenIDs.size).toBe(1);
  48. expect(result.current.childrenHiddenIDs.has('span1')).toBe(true);
  49. });
  50. });
  51. // Other function are not yet used.
  52. });