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