-
Notifications
You must be signed in to change notification settings - Fork 344
feat(router): optional router in AddTaskButton #4176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a98a028
feat(router): optional router in AddTaskButton
rafalmaksymiuk 91719c1
Merge branch 'master' into FROFRA-39
rafalmaksymiuk 3266c3f
feat(router): noop commit to fix merge queue
rafalmaksymiuk 485137e
feat(router): noop commit to fix merge queue
rafalmaksymiuk 5e0816e
Merge branch 'master' into FROFRA-39
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 118 additions & 35 deletions
153
src/elements/content-sidebar/__tests__/AddTaskButton.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,139 @@ | ||
| import * as React from 'react'; | ||
| import { mount } from 'enzyme'; | ||
| import { render, screen, userEvent } from '../../../test-utils/testing-library'; | ||
| import { AddTaskButtonComponent as AddTaskButton } from '../AddTaskButton'; | ||
|
|
||
| jest.mock('../../../components/date-picker/DatePicker', () => props => { | ||
| // only spread `input` attritutes to the input field | ||
| const { name, value = '', className, onChange, placeholder } = props; | ||
| const localInputProps = { | ||
| name, | ||
| value, | ||
| className, | ||
| onChange, | ||
| placeholder, | ||
| }; | ||
| return ( | ||
| <input type="date" {...localInputProps} {...props.inputProps} /> // eslint-disable-line react/prop-types | ||
| ); | ||
| }); | ||
| 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 | ||
| */ | ||
|
|
||
| test('should call history.replace state with force open state when task menu items are clicked', () => { | ||
| 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 wrapper = mount(<AddTaskButton history={historyMock} />); | ||
| const user = userEvent(); | ||
|
|
||
| const button = wrapper.find('Button'); | ||
| button.simulate('click'); | ||
| renderComponent({ history: historyMock }); | ||
|
|
||
| const menuItem = wrapper.find('MenuItem').first(); | ||
| menuItem.simulate('click'); | ||
| 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', () => { | ||
| test('should set state.isTaskFormOpen to false and call onTaskModalClose when task modal is closed', async () => { | ||
| const onTaskModalCloseMock = jest.fn(); | ||
| const mockButtonRef = { | ||
| current: { | ||
| focus: jest.fn(), | ||
| }, | ||
| }; | ||
| const wrapper = shallow(<AddTaskButton onTaskModalClose={onTaskModalCloseMock} />); | ||
| wrapper.instance().buttonRef = mockButtonRef; | ||
| wrapper.setState({ isTaskFormOpen: true }); | ||
|
|
||
| wrapper.instance().handleModalClose(); | ||
|
|
||
| expect(wrapper.state('isTaskFormOpen')).toBe(false); | ||
| expect(mockButtonRef.current.focus).toHaveBeenCalledTimes(1); | ||
| 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, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.