Here's a unit test using Enzyme, which is another popular testing approach that can be run locally and is more standalone:
javascript Copy code
import React from 'react';
import { shallow, mount } from 'enzyme';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import SkipProctoredExamInstruction from './SkipProctoredExamInstruction';
import { skipProctoringExam } from '../../data';
// Mock the dependencies
jest.mock('../../data', () => ({
skipProctoringExam: jest.fn().mockReturnValue({ type: 'SKIP_PROCTORING_EXAM' })
}));
jest.mock('@edx/frontend-platform/i18n', () => ({
FormattedMessage: ({ defaultMessage }) => <span>{defaultMessage}</span>
}));
// Mock Footer component
jest.mock('./Footer', () => {
const Footer = () => <div className="footer">Footer</div>;
return Footer;
});
describe('SkipProctoredExamInstruction', () => {
const mockStore = configureStore([]);
const store = mockStore({});
const mockCancelSkipProctoredExam = jest.fn();
let wrapper;
beforeEach(() => {
jest.clearAllMocks();
});
it('renders without crashing', () => {
wrapper = shallow(
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
);
expect(wrapper.exists()).toBe(true);
});
it('renders confirmation text and warning message', () => {
wrapper = shallow(
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
);
const paragraphs = wrapper.find('p');
expect(paragraphs.length).toBe(3);
// Check first paragraph (confirmation question)
expect(paragraphs.at(0).find('FormattedMessage').prop('defaultMessage'))
.toBe('Are you sure you want to take this exam without proctoring?');
// Check second paragraph (warning message)
expect(paragraphs.at(1).find('FormattedMessage').prop('defaultMessage'))
.toContain('If you take this exam without proctoring');
});
it('renders both action buttons with correct text', () => {
wrapper = shallow(
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
);
const buttons = wrapper.find('Button');
expect(buttons.length).toBe(2);
// Confirm button
expect(buttons.at(0).prop('data-testid')).toBe('skip-confirm-exam-button');
expect(buttons.at(0).find('FormattedMessage').prop('defaultMessage'))
.toBe('Continue Exam Without Proctoring');
// Cancel button
expect(buttons.at(1).prop('data-testid')).toBe('skip-cancel-exam-button');
expect(buttons.at(1).find('FormattedMessage').prop('defaultMessage'))
.toBe('Go Back');
});
it('renders Footer component', () => {
wrapper = shallow(
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
);
expect(wrapper.find('Footer').exists()).toBe(true);
});
it('dispatches skipProctoringExam when confirm button is clicked', () => {
// For testing Redux actions, we need to use mount with a Provider
wrapper = mount(
<Provider store={store}>
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
</Provider>
);
const confirmButton = wrapper.find('[data-testid="skip-confirm-exam-button"]');
confirmButton.simulate('click');
expect(skipProctoringExam).toHaveBeenCalledTimes(1);
expect(store.getActions()).toEqual([{ type: 'SKIP_PROCTORING_EXAM' }]);
});
it('calls cancelSkipProctoredExam when cancel button is clicked', () => {
wrapper = mount(
<Provider store={store}>
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
</Provider>
);
const cancelButton = wrapper.find('[data-testid="skip-cancel-exam-button"]');
cancelButton.simulate('click');
expect(mockCancelSkipProctoredExam).toHaveBeenCalledTimes(1);
});
it('has the correct button variants', () => {
wrapper = shallow(
<SkipProctoredExamInstruction
cancelSkipProctoredExam={mockCancelSkipProctoredExam}
/>
);
const buttons = wrapper.find('Button');
expect(buttons.at(0).prop('variant')).toBe('primary');
expect(buttons.at(1).prop('variant')).toBe('secondary');
});
});
You'll need to add these dependencies to your package.json:
json Copy code
{
"devDependencies": {
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"jest": "^27.0.0",
"redux-mock-store": "^1.5.4"
}
}
And set up Enzyme in your Jest setup file (e.g., setupTests.js):
javascript Copy code
import { configure } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17'; // or enzyme-adapter-react-16 depending on your React version
configure({ adapter: new Adapter() });
This approach uses Enzyme's shallow and mount rendering capabilities and is more isolated from external dependencies. It can be run completely locally without network dependencies.
Would you like me to explain how this test works or provide another testing approach?