forked from box/box-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTaskButton.test.js
More file actions
139 lines (115 loc) · 4.86 KB
/
AddTaskButton.test.js
File metadata and controls
139 lines (115 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as React from 'react';
import { render, screen, userEvent } from '../../../test-utils/testing-library';
import { AddTaskButtonComponent as AddTaskButton } from '../AddTaskButton';
jest.mock('../AddTaskMenu', () => ({ onMenuItemClick, isDisabled, setAddTaskButtonRef }) => (
<div data-testid="add-task-menu">
<button
type="button"
onClick={() => onMenuItemClick('GENERAL')}
disabled={isDisabled}
ref={setAddTaskButtonRef}
data-testid="menu-item-general"
>
Add General Task
</button>
</div>
));
jest.mock('../TaskModal', () => ({ isTaskFormOpen, onModalClose, taskType }) => (
<div data-testid="task-modal" data-open={isTaskFormOpen} data-task-type={taskType}>
<button type="button" onClick={onModalClose} data-testid="modal-close">
Close Modal
</button>
</div>
));
describe('elements/content-sidebar/AddTaskButton', () => {
/*
1. Pushing the open state into history keeps the sidebar open upon resize and refresh
2. Preventing the sidebar from closing keeps the task modal open upon edit and resize
*/
const defaultProps = {
history: { replace: jest.fn() },
isDisabled: false,
onTaskModalClose: jest.fn(),
taskFormProps: {
approvers: [],
approverSelectorContacts: [],
completionRule: 'ALL',
createTask: jest.fn(),
getApproverWithQuery: jest.fn(),
getAvatarUrl: jest.fn(),
id: '',
message: '',
},
};
const renderComponent = (props = {}) => {
return render(<AddTaskButton {...defaultProps} {...props} />);
};
beforeEach(() => {
jest.clearAllMocks();
});
test('should call history.replace state with force open state when task menu items are clicked', async () => {
const historyMock = { replace: jest.fn() };
const user = userEvent();
renderComponent({ history: historyMock });
const menuItem = screen.getByTestId('menu-item-general');
await user.click(menuItem);
expect(historyMock.replace).toHaveBeenCalledWith({ state: { open: true } });
});
test('should set state.isTaskFormOpen to false and call onTaskModalClose when task modal is closed', async () => {
const onTaskModalCloseMock = jest.fn();
const user = userEvent();
renderComponent({ onTaskModalClose: onTaskModalCloseMock });
// First click a menu item to open the modal
const menuItem = screen.getByTestId('menu-item-general');
await user.click(menuItem);
// Verify modal is open
const modal = screen.getByTestId('task-modal');
expect(modal).toHaveAttribute('data-open', 'true');
// Close the modal
const closeButton = screen.getByTestId('modal-close');
await user.click(closeButton);
// Verify modal is closed and callback was called
expect(modal).toHaveAttribute('data-open', 'false');
expect(onTaskModalCloseMock).toHaveBeenCalledTimes(1);
});
describe('when routerDisabled is true', () => {
test('should use internalSidebarNavigationHandler when task menu items are clicked', async () => {
const mockNavigationHandler = jest.fn();
const user = userEvent();
renderComponent({
routerDisabled: true,
internalSidebarNavigationHandler: mockNavigationHandler,
});
const menuItem = screen.getByTestId('menu-item-general');
await user.click(menuItem);
expect(mockNavigationHandler).toHaveBeenCalledTimes(1);
expect(mockNavigationHandler).toHaveBeenCalledWith({ open: true }, true);
});
test('should preserve internalSidebarNavigation state when using navigation handler', async () => {
const mockNavigationHandler = jest.fn();
const mockInternalSidebarNavigation = {
sidebar: 'activity',
activeFeedEntryType: 'comments',
activeFeedEntryId: '123',
};
const user = userEvent();
renderComponent({
routerDisabled: true,
internalSidebarNavigationHandler: mockNavigationHandler,
internalSidebarNavigation: mockInternalSidebarNavigation,
});
const menuItem = screen.getByTestId('menu-item-general');
await user.click(menuItem);
expect(mockNavigationHandler).toHaveBeenCalledTimes(1);
expect(mockNavigationHandler).toHaveBeenCalledWith(
{
sidebar: 'activity',
activeFeedEntryType: 'comments',
activeFeedEntryId: '123',
open: true,
},
true,
);
});
});
});