Skip to content

Commit 416651b

Browse files
add unit tests
Signed-off-by: Adhitya Mamallan <adhitya.mamallan@uber.com>
1 parent 81d9d80 commit 416651b

File tree

7 files changed

+344
-2
lines changed

7 files changed

+344
-2
lines changed

src/views/workflow-history-v2/workflow-history-header/__tests__/workflow-history-header.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ jest.mock(
2323
))
2424
);
2525

26+
jest.mock(
27+
'../../workflow-history-switch-to-v1-button/workflow-history-switch-to-v1-button',
28+
() => jest.fn(() => <button>Switch to V1</button>)
29+
);
30+
2631
describe(WorkflowHistoryHeader.name, () => {
2732
it('should render the header with title', () => {
2833
setup();
@@ -34,6 +39,11 @@ describe(WorkflowHistoryHeader.name, () => {
3439
expect(screen.getByText('Export JSON')).toBeInTheDocument();
3540
});
3641

42+
it('should render switch to V1 button', () => {
43+
setup();
44+
expect(screen.getByText('Switch to V1')).toBeInTheDocument();
45+
});
46+
3747
it('should render segmented control with grouped and ungrouped segments', () => {
3848
setup();
3949
expect(screen.getByText('Grouped')).toBeInTheDocument();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react';
2+
3+
import { HttpResponse } from 'msw';
4+
5+
import { render, screen, userEvent, waitFor } from '@/test-utils/rtl';
6+
7+
import { WorkflowHistoryContext } from '@/views/workflow-history/workflow-history-context-provider/workflow-history-context-provider';
8+
9+
import WorkflowHistorySwitchToV1Button from '../workflow-history-switch-to-v1-button';
10+
11+
jest.mock(
12+
'../../workflow-history-view-toggle-button/workflow-history-view-toggle-button',
13+
() =>
14+
jest.fn(({ onClick, label }) => <button onClick={onClick}>{label}</button>)
15+
);
16+
17+
describe(WorkflowHistorySwitchToV1Button.name, () => {
18+
it('should render button when config is OPT_IN', async () => {
19+
setup({ configValue: 'OPT_IN' });
20+
21+
expect(
22+
await screen.findByText('Switch to the legacy History view')
23+
).toBeInTheDocument();
24+
});
25+
26+
it('should render button when config is OPT_OUT', async () => {
27+
setup({ configValue: 'OPT_OUT' });
28+
29+
expect(
30+
await screen.findByText('Switch to the legacy History view')
31+
).toBeInTheDocument();
32+
});
33+
34+
it('should not render when config is DISABLED', async () => {
35+
setup({ configValue: 'DISABLED' });
36+
37+
await waitFor(() => {
38+
expect(
39+
screen.queryByText('Switch to the legacy History view')
40+
).not.toBeInTheDocument();
41+
});
42+
});
43+
44+
it('should not render when config is ENABLED', async () => {
45+
setup({ configValue: 'ENABLED' });
46+
47+
await waitFor(() => {
48+
expect(
49+
screen.queryByText('Switch to the legacy History view')
50+
).not.toBeInTheDocument();
51+
});
52+
});
53+
54+
it('should call setIsWorkflowHistoryV2Enabled with false when button is clicked', async () => {
55+
const { user, mockSetIsWorkflowHistoryV2Enabled } = setup({
56+
configValue: 'OPT_OUT',
57+
});
58+
59+
const button = await screen.findByText('Switch to the legacy History view');
60+
await user.click(button);
61+
62+
expect(mockSetIsWorkflowHistoryV2Enabled).toHaveBeenCalledTimes(1);
63+
expect(mockSetIsWorkflowHistoryV2Enabled).toHaveBeenCalledWith(false);
64+
});
65+
});
66+
67+
function setup({ configValue }: { configValue: string }) {
68+
const user = userEvent.setup();
69+
const mockSetIsWorkflowHistoryV2Enabled = jest.fn();
70+
71+
const contextValue = {
72+
ungroupedViewUserPreference: null,
73+
setUngroupedViewUserPreference: jest.fn(),
74+
isWorkflowHistoryV2Enabled: true,
75+
setIsWorkflowHistoryV2Enabled: mockSetIsWorkflowHistoryV2Enabled,
76+
};
77+
78+
const renderResult = render(
79+
<WorkflowHistoryContext.Provider value={contextValue}>
80+
<WorkflowHistorySwitchToV1Button />
81+
</WorkflowHistoryContext.Provider>,
82+
{
83+
endpointsMocks: [
84+
{
85+
path: '/api/config',
86+
httpMethod: 'GET',
87+
mockOnce: false,
88+
httpResolver: async () => {
89+
return HttpResponse.json(configValue);
90+
},
91+
},
92+
],
93+
}
94+
);
95+
96+
return {
97+
user,
98+
mockSetIsWorkflowHistoryV2Enabled,
99+
...renderResult,
100+
};
101+
}

src/views/workflow-history-v2/workflow-history-switch-to-v1-button/workflow-history-switch-to-v1-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default function WorkflowHistorySwitchToV1Button() {
1313

1414
const { setIsWorkflowHistoryV2Enabled } = useContext(WorkflowHistoryContext);
1515

16-
if (historyPageV2Config === 'ENABLED') return null;
16+
if (historyPageV2Config === 'DISABLED' || historyPageV2Config === 'ENABLED') {
17+
return null;
18+
}
1719

1820
return (
1921
<WorkflowHistoryViewToggleButton
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import React from 'react';
2+
3+
import { render, screen, userEvent, waitFor } from '@/test-utils/rtl';
4+
5+
import WorkflowHistoryViewToggleButton from '../workflow-history-view-toggle-button';
6+
import { type Props } from '../workflow-history-view-toggle-button.types';
7+
8+
describe(WorkflowHistoryViewToggleButton.name, () => {
9+
it('should render button with label', () => {
10+
setup();
11+
12+
expect(screen.getByText('Test Button')).toBeInTheDocument();
13+
});
14+
15+
it('should call onClick when button is clicked', async () => {
16+
const { user, mockOnClick } = setup();
17+
18+
const button = screen.getByText('Test Button');
19+
await user.click(button);
20+
21+
expect(mockOnClick).toHaveBeenCalledTimes(1);
22+
});
23+
24+
it('should render tooltip content when popover is opened on hover', async () => {
25+
const { user } = setup();
26+
27+
const button = screen.getByText('Test Button');
28+
await user.hover(button);
29+
30+
await waitFor(() => {
31+
expect(screen.getByText('First paragraph')).toBeInTheDocument();
32+
expect(screen.getByText('Second paragraph')).toBeInTheDocument();
33+
});
34+
});
35+
36+
it('should render link buttons when provided', async () => {
37+
const { user } = setup({
38+
tooltipContent: {
39+
content: ['Test content'],
40+
linkButtons: [
41+
{
42+
label: 'Learn More',
43+
href: '/docs',
44+
startEnhancer: null,
45+
},
46+
{
47+
label: 'FAQ',
48+
href: '/faq',
49+
startEnhancer: null,
50+
},
51+
],
52+
},
53+
});
54+
55+
const button = screen.getByText('Test Button');
56+
await user.hover(button);
57+
58+
await waitFor(() => {
59+
expect(screen.getByText('Learn More')).toBeInTheDocument();
60+
expect(screen.getByText('FAQ')).toBeInTheDocument();
61+
});
62+
});
63+
64+
it('should not render link buttons when not provided', async () => {
65+
const { user } = setup({
66+
tooltipContent: {
67+
content: ['Test content'],
68+
},
69+
});
70+
71+
const button = screen.getByText('Test Button');
72+
await user.hover(button);
73+
74+
await waitFor(() => {
75+
expect(screen.getByText('Test content')).toBeInTheDocument();
76+
});
77+
78+
expect(screen.queryByText('Learn More')).not.toBeInTheDocument();
79+
});
80+
81+
it('should render with primary kind', () => {
82+
setup({ kind: 'primary' });
83+
84+
expect(screen.getByText('Test Button')).toBeInTheDocument();
85+
});
86+
87+
it('should render with secondary kind', () => {
88+
setup({ kind: 'secondary' });
89+
90+
expect(screen.getByText('Test Button')).toBeInTheDocument();
91+
});
92+
});
93+
94+
function setup(props: Partial<Props> = {}) {
95+
const user = userEvent.setup();
96+
const mockOnClick = jest.fn();
97+
98+
const defaultProps: Props = {
99+
kind: 'primary',
100+
label: 'Test Button',
101+
onClick: mockOnClick,
102+
tooltipContent: {
103+
content: ['First paragraph', 'Second paragraph'],
104+
},
105+
...props,
106+
};
107+
108+
const renderResult = render(
109+
<WorkflowHistoryViewToggleButton {...defaultProps} />
110+
);
111+
112+
return {
113+
user,
114+
mockOnClick,
115+
...renderResult,
116+
};
117+
}

src/views/workflow-history/workflow-history-header/__tests__/workflow-history-header.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ jest.mock(
4949
))
5050
);
5151

52+
jest.mock(
53+
'../../workflow-history-switch-to-v2-button/workflow-history-switch-to-v2-button',
54+
() => jest.fn(() => <button>Switch to V2</button>)
55+
);
56+
5257
describe(WorkflowHistoryHeader.name, () => {
5358
beforeEach(() => {
5459
jest.clearAllMocks();
@@ -69,6 +74,11 @@ describe(WorkflowHistoryHeader.name, () => {
6974
expect(screen.getByText('Export JSON')).toBeInTheDocument();
7075
});
7176

77+
it('should render switch to V2 button', () => {
78+
setup();
79+
expect(screen.getByText('Switch to V2')).toBeInTheDocument();
80+
});
81+
7282
it('should call toggleIsExpandAllEvents when expand button is clicked', async () => {
7383
const { user, mockToggleIsExpandAllEvents } = setup();
7484
const expandButton = screen.getByText('Expand All');
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import React from 'react';
2+
3+
import { HttpResponse } from 'msw';
4+
5+
import { render, screen, userEvent, waitFor } from '@/test-utils/rtl';
6+
7+
import { WorkflowHistoryContext } from '../../workflow-history-context-provider/workflow-history-context-provider';
8+
import WorkflowHistorySwitchToV2Button from '../workflow-history-switch-to-v2-button';
9+
10+
jest.mock(
11+
'@/views/workflow-history-v2/workflow-history-view-toggle-button/workflow-history-view-toggle-button',
12+
() =>
13+
jest.fn(({ onClick, label }) => <button onClick={onClick}>{label}</button>)
14+
);
15+
16+
describe(WorkflowHistorySwitchToV2Button.name, () => {
17+
it('should render button when config is OPT_IN', async () => {
18+
setup({ configValue: 'OPT_IN' });
19+
20+
expect(
21+
await screen.findByText('Switch to the new History view')
22+
).toBeInTheDocument();
23+
});
24+
25+
it('should render button when config is OPT_OUT', async () => {
26+
setup({ configValue: 'OPT_OUT' });
27+
28+
expect(
29+
await screen.findByText('Switch to the new History view')
30+
).toBeInTheDocument();
31+
});
32+
33+
it('should not render when config is DISABLED', async () => {
34+
setup({ configValue: 'DISABLED' });
35+
36+
await waitFor(() => {
37+
expect(
38+
screen.queryByText('Switch to the new History view')
39+
).not.toBeInTheDocument();
40+
});
41+
});
42+
43+
it('should not render when config is ENABLED', async () => {
44+
setup({ configValue: 'ENABLED' });
45+
46+
await waitFor(() => {
47+
expect(
48+
screen.queryByText('Switch to the new History view')
49+
).not.toBeInTheDocument();
50+
});
51+
});
52+
53+
it('should call setIsWorkflowHistoryV2Enabled with true when button is clicked', async () => {
54+
const { user, mockSetIsWorkflowHistoryV2Enabled } = setup({
55+
configValue: 'OPT_OUT',
56+
});
57+
58+
const button = await screen.findByText('Switch to the new History view');
59+
await user.click(button);
60+
61+
expect(mockSetIsWorkflowHistoryV2Enabled).toHaveBeenCalledTimes(1);
62+
expect(mockSetIsWorkflowHistoryV2Enabled).toHaveBeenCalledWith(true);
63+
});
64+
});
65+
66+
function setup({ configValue }: { configValue: string }) {
67+
const user = userEvent.setup();
68+
const mockSetIsWorkflowHistoryV2Enabled = jest.fn();
69+
70+
const contextValue = {
71+
ungroupedViewUserPreference: null,
72+
setUngroupedViewUserPreference: jest.fn(),
73+
isWorkflowHistoryV2Enabled: false,
74+
setIsWorkflowHistoryV2Enabled: mockSetIsWorkflowHistoryV2Enabled,
75+
};
76+
77+
const renderResult = render(
78+
<WorkflowHistoryContext.Provider value={contextValue}>
79+
<WorkflowHistorySwitchToV2Button />
80+
</WorkflowHistoryContext.Provider>,
81+
{
82+
endpointsMocks: [
83+
{
84+
path: '/api/config',
85+
httpMethod: 'GET',
86+
mockOnce: false,
87+
httpResolver: async () => {
88+
return HttpResponse.json(configValue);
89+
},
90+
},
91+
],
92+
}
93+
);
94+
95+
return {
96+
user,
97+
mockSetIsWorkflowHistoryV2Enabled,
98+
...renderResult,
99+
};
100+
}

src/views/workflow-history/workflow-history-switch-to-v2-button/workflow-history-switch-to-v2-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default function WorkflowHistorySwitchToV2Button() {
1313

1414
const { setIsWorkflowHistoryV2Enabled } = useContext(WorkflowHistoryContext);
1515

16-
if (historyPageV2Config === 'DISABLED') return null;
16+
if (historyPageV2Config === 'DISABLED' || historyPageV2Config === 'ENABLED') {
17+
return null;
18+
}
1719

1820
return (
1921
<WorkflowHistoryViewToggleButton

0 commit comments

Comments
 (0)