Skip to content

Commit 2b8c0f2

Browse files
Add unit tests
Signed-off-by: Adhitya Mamallan <[email protected]>
1 parent 0f42e70 commit 2b8c0f2

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

src/views/domain-page/domain-page-cluster-selector/__tests__/domain-page-cluster-selector.test.tsx

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22

33
import { render, screen, fireEvent, act, within } from '@/test-utils/rtl';
44

5+
import { mockActiveActiveDomain } from '@/views/shared/active-active/__fixtures__/active-active-domain';
6+
57
import {
68
mockDomainDescription,
79
mockDomainDescriptionSingleCluster,
@@ -27,6 +29,8 @@ jest.mock('next/navigation', () => ({
2729
}),
2830
}));
2931

32+
jest.mock('../../helpers/get-cluster-replication-status-label');
33+
3034
describe(DomainPageClusterSelector.name, () => {
3135
afterEach(() => {
3236
jest.clearAllMocks();
@@ -35,7 +39,7 @@ describe(DomainPageClusterSelector.name, () => {
3539
it('Should render current cluster correctly', () => {
3640
setup({ domainDescription: mockDomainDescription });
3741

38-
expect(screen.getByText('cluster_1')).toBeInTheDocument();
42+
expect(screen.getByText('cluster_1 (active)')).toBeInTheDocument();
3943
expect(screen.getByRole('combobox')).toBeInTheDocument();
4044
});
4145

@@ -49,15 +53,17 @@ describe(DomainPageClusterSelector.name, () => {
4953
it('Should show available clusters and redirect when one is selected', () => {
5054
setup({ domainDescription: mockDomainDescription });
5155

52-
expect(screen.getByText('cluster_1')).toBeInTheDocument();
56+
expect(screen.getByText('cluster_1 (active)')).toBeInTheDocument();
5357
const clusterSelect = screen.getByRole('combobox');
5458

5559
act(() => {
5660
fireEvent.click(clusterSelect);
5761
});
5862

5963
const clustersMenu = screen.getByRole('listbox');
60-
const cluster2option = within(clustersMenu).getByText('cluster_2');
64+
const cluster2option = within(clustersMenu).getByText(
65+
'cluster_2 (passive)'
66+
);
6167

6268
act(() => {
6369
fireEvent.click(cluster2option);
@@ -67,16 +73,54 @@ describe(DomainPageClusterSelector.name, () => {
6773
'/domains/mock-domain/cluster_2/workflows'
6874
);
6975
});
76+
77+
it('Should show active/passive labels for active-passive domains', () => {
78+
setup({ domainDescription: mockDomainDescription });
79+
80+
const clusterSelect = screen.getByRole('combobox');
81+
82+
act(() => {
83+
fireEvent.click(clusterSelect);
84+
});
85+
86+
const clustersMenu = screen.getByRole('listbox');
87+
88+
expect(
89+
within(clustersMenu).getByText('cluster_1 (active)')
90+
).toBeInTheDocument();
91+
expect(
92+
within(clustersMenu).getByText('cluster_2 (passive)')
93+
).toBeInTheDocument();
94+
});
95+
96+
it('Should show primary label only for active cluster in active-active domains', () => {
97+
setup({ domainDescription: mockActiveActiveDomain, cluster: 'cluster0' });
98+
99+
const clusterSelect = screen.getByRole('combobox');
100+
101+
act(() => {
102+
fireEvent.click(clusterSelect);
103+
});
104+
105+
const clustersMenu = screen.getByRole('listbox');
106+
107+
expect(
108+
within(clustersMenu).getByText('cluster0 (primary)')
109+
).toBeInTheDocument();
110+
expect(within(clustersMenu).getByText('cluster1')).toBeInTheDocument();
111+
});
70112
});
71113

72114
function setup({
73115
domainDescription,
116+
cluster = 'cluster_1',
74117
}: {
75118
domainDescription: DomainDescription;
119+
cluster?: string;
76120
}) {
77121
render(
78122
<DomainPageClusterSelector
79-
cluster="cluster_1"
123+
cluster={cluster}
80124
domainDescription={domainDescription}
81125
/>
82126
);

src/views/domain-page/domain-page-metadata-clusters/__tests__/domain-page-metadata-clusters.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { render, screen } from '@/test-utils/rtl';
22

3+
import { mockActiveActiveDomain } from '@/views/shared/active-active/__fixtures__/active-active-domain';
4+
35
import {
46
mockDomainDescription,
57
mockDomainDescriptionSingleCluster,
68
} from '../../__fixtures__/domain-description';
79
import DomainPageMetadataClusters from '../domain-page-metadata-clusters';
810

11+
jest.mock('../../helpers/get-cluster-replication-status-label');
12+
913
describe(DomainPageMetadataClusters.name, () => {
1014
it('renders plain text for single cluster', async () => {
1115
render(
@@ -16,7 +20,7 @@ describe(DomainPageMetadataClusters.name, () => {
1620
expect(screen.queryAllByRole('link')).toHaveLength(0);
1721
});
1822

19-
it('renders active/passive labels and links for multiple clusters', () => {
23+
it('renders active/passive labels and links for active-passive domains', () => {
2024
const { container } = render(
2125
<DomainPageMetadataClusters {...mockDomainDescription} />
2226
);
@@ -38,4 +42,25 @@ describe(DomainPageMetadataClusters.name, () => {
3842
);
3943
});
4044
});
45+
46+
it('renders primary label only for active cluster in active-active domains', () => {
47+
const { container } = render(
48+
<DomainPageMetadataClusters {...mockActiveActiveDomain} />
49+
);
50+
51+
expect(container).toHaveTextContent('cluster0 (primary), cluster1');
52+
53+
const links = screen.queryAllByRole('link');
54+
expect(links).toHaveLength(2);
55+
56+
links.forEach((link, i) => {
57+
expect(link.innerHTML).toBe(
58+
mockActiveActiveDomain.clusters[i].clusterName
59+
);
60+
expect(link).toHaveAttribute(
61+
'href',
62+
`/domains/${mockActiveActiveDomain.name}/${mockActiveActiveDomain.clusters[i].clusterName}`
63+
);
64+
});
65+
});
4166
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import isActiveActiveDomain from '@/views/shared/active-active/helpers/is-active-active-domain';
2+
3+
import { type DomainDescription } from '../../domain-page.types';
4+
5+
// Manual mock for getClusterReplicationStatusLabel
6+
export default function getClusterReplicationStatusLabel(
7+
domain: DomainDescription,
8+
cluster: string
9+
): string | undefined {
10+
if (isActiveActiveDomain(domain)) {
11+
return cluster === domain.activeClusterName ? 'primary' : undefined;
12+
}
13+
14+
return cluster === domain.activeClusterName ? 'active' : 'passive';
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { mockActiveActiveDomain } from '@/views/shared/active-active/__fixtures__/active-active-domain';
2+
3+
import {
4+
mockDomainDescription,
5+
mockDomainDescriptionSingleCluster,
6+
} from '../../__fixtures__/domain-description';
7+
import getClusterReplicationStatusLabel from '../get-cluster-replication-status-label';
8+
9+
jest.mock('@/views/shared/active-active/helpers/is-active-active-domain');
10+
11+
describe(getClusterReplicationStatusLabel.name, () => {
12+
it('returns "active" for the active cluster in Active-Passive domains', () => {
13+
expect(
14+
getClusterReplicationStatusLabel(
15+
mockDomainDescription,
16+
mockDomainDescription.activeClusterName
17+
)
18+
).toBe('active');
19+
});
20+
21+
it('returns "passive" for non-active clusters in Active-Passive domains', () => {
22+
expect(
23+
getClusterReplicationStatusLabel(mockDomainDescription, 'cluster_2')
24+
).toBe('passive');
25+
});
26+
27+
it('returns "active" for single cluster domain', () => {
28+
expect(
29+
getClusterReplicationStatusLabel(
30+
mockDomainDescriptionSingleCluster,
31+
mockDomainDescriptionSingleCluster.activeClusterName
32+
)
33+
).toBe('active');
34+
});
35+
36+
it('returns "primary" for the active cluster in Active-Active domains', () => {
37+
expect(
38+
getClusterReplicationStatusLabel(
39+
mockActiveActiveDomain,
40+
mockActiveActiveDomain.activeClusterName
41+
)
42+
).toBe('primary');
43+
});
44+
45+
it('returns undefined for non-primary clusters in Active-Active domains', () => {
46+
expect(
47+
getClusterReplicationStatusLabel(mockActiveActiveDomain, 'cluster1')
48+
).toBeUndefined();
49+
});
50+
});

0 commit comments

Comments
 (0)