Skip to content

test: deprecate react-unit-test-utils 8/14 #346

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/components/Assessment/__snapshots__/index.test.jsx.snap

This file was deleted.

57 changes: 37 additions & 20 deletions src/components/Assessment/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
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 Assessment from './index';

import { useAssessmentData } from './useAssessmentData';

jest.unmock('@openedx/paragon');
jest.unmock('react');
jest.unmock('@edx/frontend-platform/i18n');

jest.mock('./useAssessmentData', () => ({
useAssessmentData: jest.fn(),
}));

jest.mock('./EditableAssessment', () => 'EditableAssessment');
jest.mock('./ReadonlyAssessment', () => 'ReadonlyAssessment');
jest.mock('./EditableAssessment', () => () => <div>Editable Assessment</div>);

jest.mock('./ReadonlyAssessment', () => () => <div>Readonly Assessment</div>);

const renderWithIntl = (component) => render(<IntlProvider locale="en">{component}</IntlProvider>);

describe('<Assessment />', () => {
it('renders the ReadonlyAssessment', () => {
useAssessmentData.mockReturnValue({ initialized: true, hasSubmitted: true });
const wrapper = shallow(<Assessment />);
expect(wrapper.snapshot).toMatchSnapshot();
beforeEach(() => {
jest.clearAllMocks();
});

it('renders the ReadonlyAssessment when assessment has been submitted', () => {
useAssessmentData.mockReturnValue({
initialized: true,
hasSubmitted: true,
});
renderWithIntl(<Assessment />);

expect(wrapper.instance.findByType('ReadonlyAssessment')).toHaveLength(1);
expect(wrapper.instance.findByType('EditableAssessment')).toHaveLength(0);
expect(screen.getByText('Readonly Assessment')).toBeInTheDocument();
expect(screen.queryByText('Editable Assessment')).not.toBeInTheDocument();
});
it('renders the EditableAssessment', () => {
useAssessmentData.mockReturnValue({ initialized: true, hasSubmitted: false });
const wrapper = shallow(<Assessment />);
expect(wrapper.snapshot).toMatchSnapshot();

expect(wrapper.instance.findByType('ReadonlyAssessment')).toHaveLength(0);
expect(wrapper.instance.findByType('EditableAssessment')).toHaveLength(1);
it('renders the EditableAssessment when assessment has not been submitted', () => {
useAssessmentData.mockReturnValue({
initialized: true,
hasSubmitted: false,
});
renderWithIntl(<Assessment />);

expect(screen.getByText('Editable Assessment')).toBeInTheDocument();
expect(screen.queryByText('Readonly Assessment')).not.toBeInTheDocument();
});
it('renders nothing if not initialized', () => {

it('renders nothing when assessment data is not initialized', () => {
useAssessmentData.mockReturnValue({ initialized: false });
const wrapper = shallow(<Assessment />);
expect(wrapper.snapshot).toMatchSnapshot();
const { container } = renderWithIntl(<Assessment />);

expect(wrapper.isEmptyRender()).toBe(true);
expect(container).toBeEmptyDOMElement();
});
});
111 changes: 0 additions & 111 deletions src/components/ProgressBar/__snapshots__/index.test.jsx.snap

This file was deleted.

15 changes: 10 additions & 5 deletions src/components/ProgressBar/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ describe('useProgressStepData', () => {
isXblockStep.mockReturnValueOnce(true);
const result = useProgressStepData(props);
result.onClick();
expect(mockOpenModal).toHaveBeenCalledWith({ view: stepNames.self, title: stepNames.self });
expect(mockOpenModal).toHaveBeenCalledWith({
view: stepNames.self,
title: stepNames.self,
});
});

it('should have href when is not xblock', () => {
const result = useProgressStepData(props);
expect(result.href).toBe(`/${stepRoutes[stepNames.self]}/courseId/xblockId`);
expect(result.href).toBe(
`/${stepRoutes[stepNames.self]}/courseId/xblockId`,
);
});

it('is complete when step state is done', () => {
Expand Down Expand Up @@ -99,12 +104,12 @@ describe('useProgressStepData', () => {
expect(result.isEnabled).toBe(false);
});

it('use effect grade from global state', () => {
it('uses effective grade from global state', () => {
const result = useProgressStepData(props);
expect(result.myGrade).toBe(8);
});

test('for peer step is not enabled when waiting for submissions', () => {
it('disables peer step when waiting for submissions', () => {
useStepInfo.mockReturnValue({
peer: {
numberOfReceivedAssessments: 0,
Expand All @@ -115,7 +120,7 @@ describe('useProgressStepData', () => {
expect(result.isEnabled).toBe(false);
});

test('for peer step is enabled iif peer is complete and no waiting for submission', () => {
it('enables peer step when peer is complete and not waiting for submissions', () => {
useStepInfo.mockReturnValue({
peer: {
numberOfReceivedAssessments: 1,
Expand Down
61 changes: 39 additions & 22 deletions src/components/ProgressBar/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 {
useAssessmentStepOrder,
Expand All @@ -12,6 +14,12 @@ import { isXblockStep } from 'utils';

import ProgressBar from './index';

/* eslint-disable react/prop-types */

jest.unmock('@openedx/paragon');
jest.unmock('react');
jest.unmock('@edx/frontend-platform/i18n');

jest.mock('hooks/app', () => ({
useAssessmentStepOrder: jest.fn(),
useGlobalState: jest.fn(),
Expand All @@ -24,45 +32,54 @@ jest.mock('hooks/routing', () => ({
jest.mock('utils', () => ({
isXblockStep: jest.fn(),
}));
jest.mock('./ProgressStep', () => 'ProgressStep');

jest.mock('./ProgressStep', () => ({ step }) => (
<div data-step={step}>Progress Step {step}</div>
));

const renderWithIntl = (component) => render(<IntlProvider locale="en">{component}</IntlProvider>);

describe('<ProgressBar />', () => {
const props = {
className: 'test-class',
};

beforeEach(() => {
useIsPageDataLoaded.mockReturnValue(true);
jest.clearAllMocks();
});

it('renders null when page data is not loaded', () => {
useIsPageDataLoaded.mockReturnValue(false);
useHasReceivedFinalGrade.mockReturnValue(false);
useGlobalState.mockReturnValue({ activeStepName: stepNames.submission });
useAssessmentStepOrder.mockReturnValue([]);
useViewStep.mockReturnValue(stepNames.submission);
isXblockStep.mockReturnValue(false);
});

it('renders null when page data is not loaded', () => {
useIsPageDataLoaded.mockReturnValueOnce(false);
const wrapper = shallow(<ProgressBar {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();

expect(wrapper.isEmptyRender()).toBe(true);
});

it('renders at least 2 steps: submission and done', () => {
const wrapper = shallow(<ProgressBar {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();
expect(wrapper.instance.findByType('ProgressStep')).toHaveLength(2);
renderWithIntl(<ProgressBar {...props} />);
expect(screen.queryByRole('navigation')).not.toBeInTheDocument();
});

it('renders all steps', () => {
isXblockStep.mockReturnValueOnce(true);
useAssessmentStepOrder.mockReturnValueOnce([
it('renders all steps when xblock step with assessment order', () => {
useIsPageDataLoaded.mockReturnValue(true);
useHasReceivedFinalGrade.mockReturnValue(false);
useGlobalState.mockReturnValue({ activeStepName: stepNames.submission });
useAssessmentStepOrder.mockReturnValue([
stepNames.studentTraining,
stepNames.self,
stepNames.peer,
]);
const wrapper = shallow(<ProgressBar {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();
expect(wrapper.instance.findByType('ProgressStep')).toHaveLength(5);
useViewStep.mockReturnValue(stepNames.submission);
isXblockStep.mockReturnValue(true);

renderWithIntl(<ProgressBar {...props} />);

expect(screen.getByText('Progress Step submission')).toBeInTheDocument();
expect(
screen.getByText('Progress Step studentTraining'),
).toBeInTheDocument();
expect(screen.getByText('Progress Step self')).toBeInTheDocument();
expect(screen.getByText('Progress Step peer')).toBeInTheDocument();
expect(screen.getByText('Progress Step done')).toBeInTheDocument();
});
});
Loading
Loading