|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, initializeMocks } from '@src/testUtils'; |
| 3 | +import AnswerOption from './AnswerOption'; |
| 4 | +import * as hooks from './hooks'; |
| 5 | +import { selectors } from '../../../../../data/redux'; |
| 6 | + |
| 7 | +const { problem } = selectors; |
| 8 | + |
| 9 | +jest.mock('../../../../../data/redux', () => ({ |
| 10 | + __esModule: true, |
| 11 | + default: jest.fn(), |
| 12 | + selectors: { |
| 13 | + problem: { |
| 14 | + problemType: jest.fn().mockReturnValue(''), |
| 15 | + }, |
| 16 | + app: { |
| 17 | + images: jest.fn(state => ({ images: state })), |
| 18 | + isLibrary: jest.fn().mockReturnValue(true), |
| 19 | + learningContextId: jest.fn(state => ({ learningContextId: state })), |
| 20 | + blockId: jest.fn(state => ({ blockId: state })), |
| 21 | + }, |
| 22 | + }, |
| 23 | + thunkActions: { |
| 24 | + video: { |
| 25 | + importTranscripts: jest.fn(), |
| 26 | + }, |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +jest.mock('../../../../../sharedComponents/ExpandableTextArea', () => 'ExpandableTextArea'); |
| 31 | + |
| 32 | +describe('AnswerOption', () => { |
| 33 | + const answerWithOnlyFeedback = { |
| 34 | + id: 'A', |
| 35 | + title: 'Answer 1', |
| 36 | + correct: true, |
| 37 | + selectedFeedback: 'some feedback', |
| 38 | + isAnswerRange: true, |
| 39 | + }; |
| 40 | + const answerWithSelectedUnselectedFeedback = { |
| 41 | + id: 'B', |
| 42 | + title: 'Answer 2', |
| 43 | + correct: true, |
| 44 | + selectedFeedback: 'selected feedback', |
| 45 | + unselectedFeedback: 'unselected feedback', |
| 46 | + isAnswerRange: false, |
| 47 | + }; |
| 48 | + const answerRange = { |
| 49 | + id: 'A', |
| 50 | + title: 'Answer Range 1', |
| 51 | + correct: true, |
| 52 | + selectedFeedback: 'selected feedback', |
| 53 | + unselectedFeedback: 'unselected feedback', |
| 54 | + isAnswerRange: true, |
| 55 | + }; |
| 56 | + |
| 57 | + const props = { |
| 58 | + hasSingleAnswer: false, |
| 59 | + answer: answerWithOnlyFeedback, |
| 60 | + // redux |
| 61 | + problemType: 'multiplechoiceresponse', |
| 62 | + images: {}, |
| 63 | + isLibrary: false, |
| 64 | + learningContextId: 'course+org+run', |
| 65 | + blockId: 'block-id', |
| 66 | + }; |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + jest.spyOn(hooks, 'removeAnswer').mockReturnValue(jest.fn()); |
| 70 | + jest.spyOn(hooks, 'setAnswer').mockReturnValue(jest.fn()); |
| 71 | + jest.spyOn(hooks, 'setAnswerTitle').mockReturnValue(jest.fn()); |
| 72 | + jest.spyOn(hooks, 'setSelectedFeedback').mockReturnValue(jest.fn()); |
| 73 | + jest.spyOn(hooks, 'setUnselectedFeedback').mockReturnValue(jest.fn()); |
| 74 | + jest.spyOn(hooks, 'useFeedback').mockReturnValue({ |
| 75 | + isFeedbackVisible: false, |
| 76 | + toggleFeedback: jest.fn(), |
| 77 | + }); |
| 78 | + initializeMocks(); |
| 79 | + }); |
| 80 | + |
| 81 | + test('renders correct option with feedback', () => { |
| 82 | + jest.spyOn(problem, 'problemType').mockReturnValue('multiplechoiceresponse'); |
| 83 | + render(<AnswerOption {...props} />); |
| 84 | + expect(screen.getByPlaceholderText('Enter an answer')).toBeInTheDocument(); |
| 85 | + expect(screen.getByRole('button', { name: 'Delete answer' })).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('renders correct option with selected unselected feedback', () => { |
| 89 | + jest.spyOn(problem, 'problemType').mockReturnValue('choiceresponse'); |
| 90 | + const myProps = { ...props, answer: answerWithSelectedUnselectedFeedback }; |
| 91 | + render(<AnswerOption {...myProps} />); |
| 92 | + expect(screen.getByText(answerWithSelectedUnselectedFeedback.id)).toBeInTheDocument(); |
| 93 | + }); |
| 94 | + |
| 95 | + test('renders correct option with optionresponse input problem', () => { |
| 96 | + jest.spyOn(problem, 'problemType').mockReturnValue('optionresponse'); |
| 97 | + const myProps = { ...props }; |
| 98 | + render(<AnswerOption {...myProps} />); |
| 99 | + expect(screen.getByRole('textbox')).toBeInTheDocument(); |
| 100 | + expect(screen.getByText(answerWithOnlyFeedback.title)).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + test('renders correct option with numeric input problem and answer range', () => { |
| 104 | + jest.spyOn(problem, 'problemType').mockReturnValue('numericalresponse'); |
| 105 | + const myProps = { ...props }; |
| 106 | + render(<AnswerOption {...myProps} answer={answerRange} />); |
| 107 | + expect(screen.getByText(answerRange.title)).toBeInTheDocument(); |
| 108 | + expect(screen.getByRole('textbox')).toBeInTheDocument(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments