|
| 1 | +import { render, screen, userEvent } from '@/test-utils/rtl'; |
| 2 | + |
| 3 | +import { type ClusterAttributeScope } from '@/__generated__/proto-ts/uber/cadence/api/v1/ClusterAttributeScope'; |
| 4 | +import { mockActiveActiveDomain } from '@/views/shared/active-active/__fixtures__/active-active-domain'; |
| 5 | +import { type ActiveActiveDomain } from '@/views/shared/active-active/active-active.types'; |
| 6 | + |
| 7 | +import DomainPageMetadataFailoverVersionActiveActive from '../domain-page-metadata-failover-version-active-active'; |
| 8 | + |
| 9 | +describe(DomainPageMetadataFailoverVersionActiveActive.name, () => { |
| 10 | + it('renders the table with headers', () => { |
| 11 | + setup(); |
| 12 | + |
| 13 | + expect(screen.getByText('Scope')).toBeInTheDocument(); |
| 14 | + expect(screen.getByText('Attribute')).toBeInTheDocument(); |
| 15 | + expect(screen.getByText('Active Cluster')).toBeInTheDocument(); |
| 16 | + expect(screen.getByText('Failover Version')).toBeInTheDocument(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('renders default entry with domain data', () => { |
| 20 | + setup(); |
| 21 | + |
| 22 | + expect(screen.getByText('default')).toBeInTheDocument(); |
| 23 | + expect(screen.getByText('-')).toBeInTheDocument(); |
| 24 | + expect(screen.getByText('cluster0')).toBeInTheDocument(); |
| 25 | + expect(screen.getByText('2')).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('renders multiple failover version entries', () => { |
| 29 | + setup({ attributeCount: 2 }); |
| 30 | + |
| 31 | + // Default entry |
| 32 | + expect(screen.getByText('default')).toBeInTheDocument(); |
| 33 | + expect(screen.getByText('cluster0')).toBeInTheDocument(); |
| 34 | + expect(screen.getByText('2')).toBeInTheDocument(); |
| 35 | + |
| 36 | + // Additional entries |
| 37 | + expect(screen.getByText('scope1')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText('attribute1')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('cluster_1')).toBeInTheDocument(); |
| 40 | + expect(screen.getByText('1000')).toBeInTheDocument(); |
| 41 | + |
| 42 | + expect(screen.getByText('scope2')).toBeInTheDocument(); |
| 43 | + expect(screen.getByText('attribute2')).toBeInTheDocument(); |
| 44 | + expect(screen.getByText('cluster_2')).toBeInTheDocument(); |
| 45 | + expect(screen.getByText('1001')).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('does not show "Show all" button when entries are less than or equal to MAX_ATTRIBUTES_COUNT_TRUNCATED', () => { |
| 49 | + // 3 additional attributes + 1 default = 4 total (exactly at the limit) |
| 50 | + setup({ attributeCount: 3 }); |
| 51 | + |
| 52 | + expect(screen.queryByText(/Show all/)).not.toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows "Show all" button when entries exceed MAX_ATTRIBUTES_COUNT_TRUNCATED', () => { |
| 56 | + // 5 additional attributes + 1 default = 6 total (exceeds limit of 4) |
| 57 | + setup({ attributeCount: 5 }); |
| 58 | + |
| 59 | + expect(screen.getByText('Show all (6)')).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('truncates entries when not expanded', () => { |
| 63 | + setup({ attributeCount: 5 }); |
| 64 | + |
| 65 | + // Should show first 4 entries only (default + first 3 attributes) |
| 66 | + expect(screen.getByText('default')).toBeInTheDocument(); |
| 67 | + expect(screen.getByText('scope1')).toBeInTheDocument(); |
| 68 | + expect(screen.getByText('scope2')).toBeInTheDocument(); |
| 69 | + expect(screen.getByText('scope3')).toBeInTheDocument(); |
| 70 | + |
| 71 | + // Should not show 5th and 6th entries |
| 72 | + expect(screen.queryByText('scope4')).not.toBeInTheDocument(); |
| 73 | + expect(screen.queryByText('scope5')).not.toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('expands to show all entries when "Show all" button is clicked', async () => { |
| 77 | + const { user } = setup({ attributeCount: 5 }); |
| 78 | + |
| 79 | + const showAllButton = screen.getByText('Show all (6)'); |
| 80 | + await user.click(showAllButton); |
| 81 | + |
| 82 | + // Now all entries should be visible |
| 83 | + expect(screen.getByText('default')).toBeInTheDocument(); |
| 84 | + expect(screen.getByText('scope1')).toBeInTheDocument(); |
| 85 | + expect(screen.getByText('scope2')).toBeInTheDocument(); |
| 86 | + expect(screen.getByText('scope3')).toBeInTheDocument(); |
| 87 | + expect(screen.getByText('scope4')).toBeInTheDocument(); |
| 88 | + expect(screen.getByText('scope5')).toBeInTheDocument(); |
| 89 | + |
| 90 | + // Button text should change |
| 91 | + expect(screen.getByText('Show less')).toBeInTheDocument(); |
| 92 | + expect(screen.queryByText('Show all (6)')).not.toBeInTheDocument(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('collapses to show truncated entries when "Show less" button is clicked', async () => { |
| 96 | + const { user } = setup({ attributeCount: 5 }); |
| 97 | + |
| 98 | + // Expand first |
| 99 | + const showAllButton = screen.getByText('Show all (6)'); |
| 100 | + await user.click(showAllButton); |
| 101 | + |
| 102 | + // Then collapse |
| 103 | + const showLessButton = screen.getByText('Show less'); |
| 104 | + await user.click(showLessButton); |
| 105 | + |
| 106 | + // Should show first 4 entries only |
| 107 | + expect(screen.getByText('default')).toBeInTheDocument(); |
| 108 | + expect(screen.getByText('scope1')).toBeInTheDocument(); |
| 109 | + expect(screen.getByText('scope2')).toBeInTheDocument(); |
| 110 | + expect(screen.getByText('scope3')).toBeInTheDocument(); |
| 111 | + |
| 112 | + // Should not show 5th and 6th entries |
| 113 | + expect(screen.queryByText('scope4')).not.toBeInTheDocument(); |
| 114 | + expect(screen.queryByText('scope5')).not.toBeInTheDocument(); |
| 115 | + |
| 116 | + // Button text should change back |
| 117 | + expect(screen.getByText('Show all (6)')).toBeInTheDocument(); |
| 118 | + expect(screen.queryByText('Show less')).not.toBeInTheDocument(); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +function setup({ |
| 123 | + attributeCount = 0, |
| 124 | +}: { |
| 125 | + attributeCount?: number; |
| 126 | +} = {}) { |
| 127 | + const user = userEvent.setup(); |
| 128 | + |
| 129 | + const activeClustersByClusterAttribute: Record< |
| 130 | + string, |
| 131 | + ClusterAttributeScope |
| 132 | + > = {}; |
| 133 | + |
| 134 | + // Create additional attributes beyond the default one |
| 135 | + for (let i = 0; i < attributeCount; i++) { |
| 136 | + const scope = `scope${i + 1}`; |
| 137 | + activeClustersByClusterAttribute[scope] = { |
| 138 | + clusterAttributes: { |
| 139 | + [`attribute${i + 1}`]: { |
| 140 | + activeClusterName: `cluster_${i + 1}`, |
| 141 | + failoverVersion: `${1000 + i}`, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + const domain: ActiveActiveDomain = { |
| 148 | + ...mockActiveActiveDomain, |
| 149 | + activeClusters: { |
| 150 | + regionToCluster: {}, |
| 151 | + activeClustersByClusterAttribute, |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + render(<DomainPageMetadataFailoverVersionActiveActive {...domain} />); |
| 156 | + |
| 157 | + return { user }; |
| 158 | +} |
0 commit comments