|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + render, screen, initializeMocks, |
| 4 | +} from '@src/testUtils'; |
| 5 | +import * as hooks from './hooks'; |
| 6 | +import { SettingsWidgetInternal as SettingsWidget, mapDispatchToProps } from '.'; |
| 7 | +import { ProblemTypeKeys } from '../../../../../data/constants/problem'; |
| 8 | +import { actions } from '../../../../../data/redux'; |
| 9 | + |
| 10 | +jest.mock('./settingsComponents/GeneralFeedback', () => 'GeneralFeedback'); |
| 11 | +jest.mock('./settingsComponents/GroupFeedback', () => 'GroupFeedback'); |
| 12 | +jest.mock('./settingsComponents/Randomization', () => 'Randomization'); |
| 13 | +jest.mock('./settingsComponents/HintsCard', () => 'HintsCard'); |
| 14 | +jest.mock('./settingsComponents/ResetCard', () => 'ResetCard'); |
| 15 | +jest.mock('./settingsComponents/ScoringCard', () => 'ScoringCard'); |
| 16 | +jest.mock('./settingsComponents/ShowAnswerCard', () => 'ShowAnswerCard'); |
| 17 | +jest.mock('./settingsComponents/SwitchEditorCard', () => 'SwitchEditorCard'); |
| 18 | +jest.mock('./settingsComponents/TimerCard', () => 'TimerCard'); |
| 19 | +jest.mock('./settingsComponents/TypeCard', () => 'TypeCard'); |
| 20 | + |
| 21 | +describe('SettingsWidget', () => { |
| 22 | + const showAdvancedSettingsCardsBaseProps = { |
| 23 | + isAdvancedCardsVisible: false, |
| 24 | + showAdvancedCards: jest.fn().mockName('showAdvancedSettingsCards.showAdvancedCards'), |
| 25 | + setResetTrue: jest.fn().mockName('showAdvancedSettingsCards.setResetTrue'), |
| 26 | + }; |
| 27 | + |
| 28 | + const props = { |
| 29 | + problemType: ProblemTypeKeys.TEXTINPUT, |
| 30 | + settings: {}, |
| 31 | + defaultSettings: { |
| 32 | + maxAttempts: 2, |
| 33 | + showanswer: 'finished', |
| 34 | + showResetButton: false, |
| 35 | + }, |
| 36 | + images: {}, |
| 37 | + isLibrary: false, |
| 38 | + learningContextId: 'course+org+run', |
| 39 | + setBlockTitle: jest.fn().mockName('setBlockTitle'), |
| 40 | + blockTitle: '', |
| 41 | + updateAnswer: jest.fn().mockName('updateAnswer'), |
| 42 | + updateSettings: jest.fn().mockName('updateSettings'), |
| 43 | + updateField: jest.fn().mockName('updateField'), |
| 44 | + answers: [], |
| 45 | + correctAnswerCount: 0, |
| 46 | + groupFeedbackList: [], |
| 47 | + showMarkdownEditorButton: false, |
| 48 | + |
| 49 | + }; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + initializeMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('behavior', () => { |
| 56 | + it('calls showAdvancedSettingsCards when initialized', () => { |
| 57 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsBaseProps); |
| 58 | + render(<SettingsWidget {...props} />); |
| 59 | + expect(hooks.showAdvancedSettingsCards).toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('renders', () => { |
| 64 | + test('renders Settings widget page', () => { |
| 65 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsBaseProps); |
| 66 | + render(<SettingsWidget {...props} />); |
| 67 | + expect(screen.getByText('Show advanced settings')).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + test('renders Settings widget page advanced settings visible', () => { |
| 71 | + const showAdvancedSettingsCardsProps = { |
| 72 | + ...showAdvancedSettingsCardsBaseProps, |
| 73 | + isAdvancedCardsVisible: true, |
| 74 | + }; |
| 75 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsProps); |
| 76 | + const { container } = render(<SettingsWidget {...props} />); |
| 77 | + expect(screen.queryByText('Show advanced settings')).not.toBeInTheDocument(); |
| 78 | + expect(container.querySelector('showanswercard')).toBeInTheDocument(); |
| 79 | + expect(container.querySelector('resetcard')).toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('renders Settings widget for Advanced Problem with correct widgets', () => { |
| 83 | + const showAdvancedSettingsCardsProps = { |
| 84 | + ...showAdvancedSettingsCardsBaseProps, |
| 85 | + isAdvancedCardsVisible: true, |
| 86 | + }; |
| 87 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsProps); |
| 88 | + const { container } = render( |
| 89 | + <SettingsWidget {...props} problemType={ProblemTypeKeys.ADVANCED} />, |
| 90 | + ); |
| 91 | + expect(container.querySelector('randomization')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('isLibrary', () => { |
| 96 | + const libraryProps = { |
| 97 | + ...props, |
| 98 | + isLibrary: true, |
| 99 | + }; |
| 100 | + test('renders Settings widget page', () => { |
| 101 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsBaseProps); |
| 102 | + const { container } = render(<SettingsWidget {...libraryProps} />); |
| 103 | + expect(container.querySelector('timercard')).not.toBeInTheDocument(); |
| 104 | + expect(container.querySelector('resetcard')).not.toBeInTheDocument(); |
| 105 | + expect(container.querySelector('typecard')).toBeInTheDocument(); |
| 106 | + expect(container.querySelector('hintscard')).toBeInTheDocument(); |
| 107 | + expect(screen.getByText('Show advanced settings')).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('renders Settings widget page advanced settings visible', () => { |
| 111 | + const showAdvancedSettingsCardsProps = { |
| 112 | + ...showAdvancedSettingsCardsBaseProps, |
| 113 | + isAdvancedCardsVisible: true, |
| 114 | + }; |
| 115 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsProps); |
| 116 | + const { container } = render(<SettingsWidget {...libraryProps} />); |
| 117 | + expect(screen.queryByText('Show advanced settings')).not.toBeInTheDocument(); |
| 118 | + expect(container.querySelector('showanswearscard')).not.toBeInTheDocument(); |
| 119 | + expect(container.querySelector('resetcard')).not.toBeInTheDocument(); |
| 120 | + expect(container.querySelector('typecard')).toBeInTheDocument(); |
| 121 | + expect(container.querySelector('hintscard')).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + test('renders Settings widget for Advanced Problem with correct widgets', () => { |
| 125 | + const showAdvancedSettingsCardsProps = { |
| 126 | + ...showAdvancedSettingsCardsBaseProps, |
| 127 | + isAdvancedCardsVisible: true, |
| 128 | + }; |
| 129 | + jest.spyOn(hooks, 'showAdvancedSettingsCards').mockReturnValue(showAdvancedSettingsCardsProps); |
| 130 | + const { container } = render(<SettingsWidget {...libraryProps} problemType={ProblemTypeKeys.ADVANCED} />); |
| 131 | + expect(container.querySelector('randomization')).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('mapDispatchToProps', () => { |
| 136 | + test('setBlockTitle from actions.app.setBlockTitle', () => { |
| 137 | + expect(mapDispatchToProps.setBlockTitle).toEqual(actions.app.setBlockTitle); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('mapDispatchToProps', () => { |
| 142 | + test('updateSettings from actions.problem.updateSettings', () => { |
| 143 | + expect(mapDispatchToProps.updateSettings).toEqual(actions.problem.updateSettings); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('mapDispatchToProps', () => { |
| 148 | + test('updateField from actions.problem.updateField', () => { |
| 149 | + expect(mapDispatchToProps.updateField).toEqual(actions.problem.updateField); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('mapDispatchToProps', () => { |
| 154 | + test('updateAnswer from actions.problem.updateAnswer', () => { |
| 155 | + expect(mapDispatchToProps.updateAnswer).toEqual(actions.problem.updateAnswer); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments