|
1 | 1 | import { TSource } from '@hyperdx/common-utils/dist/types'; |
| 2 | +import { SortingState } from '@tanstack/react-table'; |
2 | 3 | import { act, renderHook } from '@testing-library/react'; |
3 | 4 |
|
4 | 5 | import { MetricsDataType, NumberFormat } from '../types'; |
|
7 | 8 | formatAttributeClause, |
8 | 9 | formatNumber, |
9 | 10 | getMetricTableName, |
| 11 | + orderByStringToSortingState, |
| 12 | + sortingStateToOrderByString, |
10 | 13 | stripTrailingSlash, |
11 | 14 | useQueryHistory, |
12 | 15 | } from '../utils'; |
@@ -534,3 +537,96 @@ describe('useQueryHistory', () => { |
534 | 537 | expect(mockSetItem).not.toHaveBeenCalled(); |
535 | 538 | }); |
536 | 539 | }); |
| 540 | + |
| 541 | +describe('sortingStateToOrderByString', () => { |
| 542 | + it('returns undefined for null input', () => { |
| 543 | + expect(sortingStateToOrderByString(null)).toBeUndefined(); |
| 544 | + }); |
| 545 | + |
| 546 | + it('returns undefined for empty array', () => { |
| 547 | + const sortingState: SortingState = []; |
| 548 | + expect(sortingStateToOrderByString(sortingState)).toBeUndefined(); |
| 549 | + }); |
| 550 | + |
| 551 | + it('converts sorting state with desc: false to ASC order', () => { |
| 552 | + const sortingState: SortingState = [{ id: 'timestamp', desc: false }]; |
| 553 | + expect(sortingStateToOrderByString(sortingState)).toBe('timestamp ASC'); |
| 554 | + }); |
| 555 | + |
| 556 | + it('converts sorting state with desc: true to DESC order', () => { |
| 557 | + const sortingState: SortingState = [{ id: 'timestamp', desc: true }]; |
| 558 | + expect(sortingStateToOrderByString(sortingState)).toBe('timestamp DESC'); |
| 559 | + }); |
| 560 | + |
| 561 | + it('handles column names with special characters', () => { |
| 562 | + const sortingState: SortingState = [{ id: 'user_count', desc: false }]; |
| 563 | + expect(sortingStateToOrderByString(sortingState)).toBe('user_count ASC'); |
| 564 | + }); |
| 565 | +}); |
| 566 | + |
| 567 | +describe('orderByStringToSortingState', () => { |
| 568 | + it('returns undefined for undefined input', () => { |
| 569 | + expect(orderByStringToSortingState(undefined)).toBeUndefined(); |
| 570 | + }); |
| 571 | + |
| 572 | + it('returns undefined for empty string', () => { |
| 573 | + expect(orderByStringToSortingState('')).toBeUndefined(); |
| 574 | + }); |
| 575 | + |
| 576 | + it('converts "column ASC" to sorting state with desc: false', () => { |
| 577 | + const result = orderByStringToSortingState('timestamp ASC'); |
| 578 | + expect(result).toEqual([{ id: 'timestamp', desc: false }]); |
| 579 | + }); |
| 580 | + |
| 581 | + it('converts "column DESC" to sorting state with desc: true', () => { |
| 582 | + const result = orderByStringToSortingState('timestamp DESC'); |
| 583 | + expect(result).toEqual([{ id: 'timestamp', desc: true }]); |
| 584 | + }); |
| 585 | + |
| 586 | + it('handles case insensitive direction keywords', () => { |
| 587 | + expect(orderByStringToSortingState('col asc')).toEqual([ |
| 588 | + { id: 'col', desc: false }, |
| 589 | + ]); |
| 590 | + expect(orderByStringToSortingState('col Asc')).toEqual([ |
| 591 | + { id: 'col', desc: false }, |
| 592 | + ]); |
| 593 | + expect(orderByStringToSortingState('col desc')).toEqual([ |
| 594 | + { id: 'col', desc: true }, |
| 595 | + ]); |
| 596 | + expect(orderByStringToSortingState('col Desc')).toEqual([ |
| 597 | + { id: 'col', desc: true }, |
| 598 | + ]); |
| 599 | + expect(orderByStringToSortingState('col DESC')).toEqual([ |
| 600 | + { id: 'col', desc: true }, |
| 601 | + ]); |
| 602 | + }); |
| 603 | + |
| 604 | + it('returns undefined for invalid format without direction', () => { |
| 605 | + expect(orderByStringToSortingState('timestamp')).toBeUndefined(); |
| 606 | + }); |
| 607 | + |
| 608 | + it('returns undefined for invalid format with wrong number of parts', () => { |
| 609 | + expect(orderByStringToSortingState('col name ASC')).toBeUndefined(); |
| 610 | + }); |
| 611 | + |
| 612 | + it('returns undefined for invalid direction keyword', () => { |
| 613 | + expect(orderByStringToSortingState('col INVALID')).toBeUndefined(); |
| 614 | + }); |
| 615 | + |
| 616 | + it('handles column names with underscores', () => { |
| 617 | + const result = orderByStringToSortingState('user_count DESC'); |
| 618 | + expect(result).toEqual([{ id: 'user_count', desc: true }]); |
| 619 | + }); |
| 620 | + |
| 621 | + it('handles column names with numbers', () => { |
| 622 | + const result = orderByStringToSortingState('col123 ASC'); |
| 623 | + expect(result).toEqual([{ id: 'col123', desc: false }]); |
| 624 | + }); |
| 625 | + |
| 626 | + it('round-trips correctly with sortingStateToOrderByString', () => { |
| 627 | + const originalSort: SortingState = [{ id: 'service_name', desc: true }]; |
| 628 | + const orderByString = sortingStateToOrderByString(originalSort); |
| 629 | + const roundTripSort = orderByStringToSortingState(orderByString); |
| 630 | + expect(roundTripSort).toEqual(originalSort); |
| 631 | + }); |
| 632 | +}); |
0 commit comments