-
Notifications
You must be signed in to change notification settings - Fork 17
test: deprecate react-unit-test-utils 9/14 #347
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
Open
vmnavarro94
wants to merge
3
commits into
openedx:master
Choose a base branch
from
WGU-Open-edX:depr-p9
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
119 changes: 94 additions & 25 deletions
119
src/components/Assessment/EditableAssessment/AssessmentActions.test.jsx
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,72 +1,141 @@ | ||
import { shallow } from '@edx/react-unit-test-utils'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
import { useExitWithoutSavingAction, useSubmitAssessmentAction } from 'hooks/actions'; | ||
import { | ||
useExitWithoutSavingAction, | ||
useSubmitAssessmentAction, | ||
} from 'hooks/actions'; | ||
import AssessmentActions from './AssessmentActions'; | ||
|
||
/* eslint-disable react/prop-types */ | ||
|
||
jest.unmock('@openedx/paragon'); | ||
jest.unmock('react'); | ||
jest.unmock('@edx/frontend-platform/i18n'); | ||
|
||
jest.mock('hooks/actions', () => ({ | ||
useExitWithoutSavingAction: jest.fn(), | ||
useSubmitAssessmentAction: jest.fn(), | ||
})); | ||
|
||
jest.mock('components/ActionButton', () => 'ActionButton'); | ||
jest.mock('components/ConfirmDialog', () => 'ConfirmDialog'); | ||
jest.mock( | ||
'components/ActionButton', | ||
() => ({ | ||
children, variant, onClick, ...props | ||
}) => ( | ||
<button | ||
type="button" | ||
aria-label={`Action Button ${variant}`} | ||
onClick={onClick} | ||
{...props} | ||
> | ||
{children} | ||
</button> | ||
), | ||
); | ||
|
||
jest.mock( | ||
'components/ConfirmDialog', | ||
() => ({ title, onConfirm, ...props }) => ( | ||
<button | ||
type="button" | ||
aria-label={`Confirm Dialog ${title}`} | ||
onClick={onConfirm} | ||
onKeyDown={(e) => e.key === 'Enter' && onConfirm()} | ||
{...props} | ||
/> | ||
), | ||
); | ||
|
||
describe('<AssessmentActions />', () => { | ||
describe('AssessmentActions', () => { | ||
const mockExitWithoutSavingAction = { | ||
action: { | ||
onClick: jest.fn().mockName('useExitWithoutSavingAction.onClick'), | ||
children: 'Exit without saving', | ||
}, | ||
confirmProps: { | ||
onConfirm: jest.fn().mockName('useExitWithoutSavingAction.onConfirm'), | ||
title: 'Exit without saving', | ||
}, | ||
}; | ||
const mockSubmitAssessmentAction = { | ||
action: { | ||
onClick: jest.fn().mockName('useSubmitAssessmentAction.onClick'), | ||
children: 'Submit assessment', | ||
}, | ||
confirmProps: { | ||
onConfirm: jest.fn().mockName('useSubmitAssessmentAction.onConfirm'), | ||
title: 'Submit assessment', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
useExitWithoutSavingAction.mockReturnValue(mockExitWithoutSavingAction); | ||
useSubmitAssessmentAction.mockReturnValue(mockSubmitAssessmentAction); | ||
}); | ||
|
||
it('render default', () => { | ||
const wrapper = shallow(<AssessmentActions />); | ||
expect(wrapper.snapshot).toMatchSnapshot(); | ||
it('renders both action buttons and confirm dialogs', () => { | ||
render(<AssessmentActions />); | ||
|
||
expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2); | ||
expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(2); | ||
expect( | ||
screen.getByLabelText('Action Button outline-primary'), | ||
).toBeInTheDocument(); | ||
expect(screen.getByLabelText('Action Button primary')).toBeInTheDocument(); | ||
expect( | ||
screen.getByLabelText('Confirm Dialog Exit without saving'), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByLabelText('Confirm Dialog Submit assessment'), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('render without submitConfirmDialog', () => { | ||
it('renders without submit confirm dialog when confirmProps is null', () => { | ||
useSubmitAssessmentAction.mockReturnValueOnce({ | ||
action: mockSubmitAssessmentAction.action, | ||
confirmProps: null, | ||
}); | ||
const wrapper = shallow(<AssessmentActions />); | ||
expect(wrapper.snapshot).toMatchSnapshot(); | ||
render(<AssessmentActions />); | ||
|
||
expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2); | ||
expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(1); | ||
expect( | ||
screen.getByLabelText('Action Button outline-primary'), | ||
).toBeInTheDocument(); | ||
expect(screen.getByLabelText('Action Button primary')).toBeInTheDocument(); | ||
expect( | ||
screen.getByLabelText('Confirm Dialog Exit without saving'), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.queryByLabelText('Confirm Dialog Submit assessment'), | ||
).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('has correct mock value', () => { | ||
const wrapper = shallow(<AssessmentActions />); | ||
it('calls the correct handlers when buttons and dialogs are clicked', () => { | ||
render(<AssessmentActions />); | ||
|
||
const exitButton = wrapper.instance.findByType('ActionButton')[0]; | ||
expect(exitButton.props).toMatchObject(mockExitWithoutSavingAction.action); | ||
const exitButton = screen.getByLabelText('Action Button outline-primary'); | ||
expect(exitButton).toHaveTextContent('Exit without saving'); | ||
fireEvent.click(exitButton); | ||
expect(mockExitWithoutSavingAction.action.onClick).toHaveBeenCalled(); | ||
|
||
const exitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[0]; | ||
expect(exitConfirmDialog.props).toMatchObject(mockExitWithoutSavingAction.confirmProps); | ||
const submitButton = screen.getByLabelText('Action Button primary'); | ||
expect(submitButton).toHaveTextContent('Submit assessment'); | ||
fireEvent.click(submitButton); | ||
expect(mockSubmitAssessmentAction.action.onClick).toHaveBeenCalled(); | ||
|
||
const submitButton = wrapper.instance.findByType('ActionButton')[1]; | ||
expect(submitButton.props).toMatchObject(mockSubmitAssessmentAction.action); | ||
const exitDialog = screen.getByLabelText( | ||
'Confirm Dialog Exit without saving', | ||
); | ||
fireEvent.click(exitDialog); | ||
expect( | ||
mockExitWithoutSavingAction.confirmProps.onConfirm, | ||
).toHaveBeenCalled(); | ||
|
||
const submitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[1]; | ||
expect(submitConfirmDialog.props).toMatchObject(mockSubmitAssessmentAction.confirmProps); | ||
const submitDialog = screen.getByLabelText( | ||
'Confirm Dialog Submit assessment', | ||
); | ||
fireEvent.click(submitDialog); | ||
expect( | ||
mockSubmitAssessmentAction.confirmProps.onConfirm, | ||
).toHaveBeenCalled(); | ||
}); | ||
}); |
40 changes: 0 additions & 40 deletions
40
src/components/Assessment/EditableAssessment/__snapshots__/AssessmentActions.test.jsx.snap
This file was deleted.
Oops, something went wrong.
62 changes: 51 additions & 11 deletions
62
src/components/CriterionContainer/GradedCriterion.test.jsx
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,24 +1,64 @@ | ||
import { shallow } from '@edx/react-unit-test-utils'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
|
||
import GradedCriterion from './GradedCriterion'; | ||
|
||
describe('<GradedCriterion />', () => { | ||
const props = { | ||
jest.unmock('@openedx/paragon'); | ||
jest.unmock('react'); | ||
jest.unmock('@edx/frontend-platform/i18n'); | ||
|
||
const renderComponent = (props = {}) => render( | ||
<IntlProvider | ||
messages={{ | ||
'frontend-app-ora.RadioCriterion.optionPoints': '{points} points', | ||
}} | ||
locale="en" | ||
> | ||
<GradedCriterion {...props} /> | ||
</IntlProvider>, | ||
); | ||
|
||
describe('GradedCriterion', () => { | ||
const defaultProps = { | ||
selectedOption: { | ||
name: 'option1', | ||
label: 'Option 1', | ||
points: 1, | ||
points: 5, | ||
}, | ||
feedbackValue: 'Feedback', | ||
feedbackValue: 'Feedback text', | ||
}; | ||
|
||
it('renders correctly', () => { | ||
const wrapper = shallow(<GradedCriterion {...props} />); | ||
expect(wrapper.snapshot).toMatchSnapshot(); | ||
it('displays the option label and points', () => { | ||
renderComponent(defaultProps); | ||
|
||
expect(screen.getByText('Option 1')).toBeInTheDocument(); | ||
expect(screen.getByText('5 points')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays the option name if label is not provided', () => { | ||
const props = { | ||
selectedOption: { | ||
name: 'option1', | ||
points: 5, | ||
}, | ||
}; | ||
renderComponent(props); | ||
|
||
expect(screen.getByText('option1')).toBeInTheDocument(); | ||
expect(screen.getByText('5 points')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays feedback when provided', () => { | ||
renderComponent(defaultProps); | ||
expect(screen.getByText('Feedback text')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders correctly with no feedback', () => { | ||
const wrapper = shallow(<GradedCriterion selectedOption={props.selectedOption} />); | ||
expect(wrapper.snapshot).toMatchSnapshot(); | ||
it('does not display feedback when not provided', () => { | ||
const props = { | ||
selectedOption: defaultProps.selectedOption, | ||
}; | ||
renderComponent(props); | ||
expect(screen.queryByText('Feedback text')).not.toBeInTheDocument(); | ||
}); | ||
}); |
72 changes: 0 additions & 72 deletions
72
src/components/CriterionContainer/__snapshots__/GradedCriterion.test.jsx.snap
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain these mocks? I'd think we'd either want to use actual components in testing, or use minimal placeholders similar to what we had before.