@@ -2,10 +2,11 @@ import React from 'react';
22
33import { ProblemTypeKeys } from '@src/editors/data/constants/problem' ;
44import { screen , initializeMocks } from '@src/testUtils' ;
5- import { editorRender } from '@src/editors/editorTestRender' ;
5+ import { editorRender , type PartialEditorState } from '@src/editors/editorTestRender' ;
66import { mockWaffleFlags } from '@src/data/apiHooks.mock' ;
77import * as hooks from './hooks' ;
88import { SettingsWidgetInternal as SettingsWidget } from '.' ;
9+ import { ProblemEditorContextProvider } from '../ProblemEditorContext' ;
910
1011jest . mock ( './settingsComponents/GeneralFeedback' , ( ) => 'GeneralFeedback' ) ;
1112jest . mock ( './settingsComponents/GroupFeedback' , ( ) => 'GroupFeedback' ) ;
@@ -23,7 +24,6 @@ describe('SettingsWidget', () => {
2324 const showAdvancedSettingsCardsBaseProps = {
2425 isAdvancedCardsVisible : false ,
2526 showAdvancedCards : jest . fn ( ) . mockName ( 'showAdvancedSettingsCards.showAdvancedCards' ) ,
26- setResetTrue : jest . fn ( ) . mockName ( 'showAdvancedSettingsCards.setResetTrue' ) ,
2727 } ;
2828
2929 const props = {
@@ -49,22 +49,34 @@ describe('SettingsWidget', () => {
4949
5050 } ;
5151
52+ const editorRef = { current : null } ;
53+
54+ const renderSettingsWidget = (
55+ overrideProps = { } ,
56+ options = { } ,
57+ ) => editorRender (
58+ < ProblemEditorContextProvider editorRef = { editorRef } >
59+ < SettingsWidget { ...props } { ...overrideProps } />
60+ </ ProblemEditorContextProvider > ,
61+ options ,
62+ ) ;
63+
5264 beforeEach ( ( ) => {
5365 initializeMocks ( ) ;
5466 } ) ;
5567
5668 describe ( 'behavior' , ( ) => {
5769 it ( 'calls showAdvancedSettingsCards when initialized' , ( ) => {
5870 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsBaseProps ) ;
59- editorRender ( < SettingsWidget { ... props } /> ) ;
71+ renderSettingsWidget ( ) ;
6072 expect ( hooks . showAdvancedSettingsCards ) . toHaveBeenCalled ( ) ;
6173 } ) ;
6274 } ) ;
6375
6476 describe ( 'renders' , ( ) => {
6577 test ( 'renders Settings widget page' , ( ) => {
6678 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsBaseProps ) ;
67- editorRender ( < SettingsWidget { ... props } /> ) ;
79+ renderSettingsWidget ( ) ;
6880 expect ( screen . getByText ( 'Show advanced settings' ) ) . toBeInTheDocument ( ) ;
6981 } ) ;
7082
@@ -74,7 +86,7 @@ describe('SettingsWidget', () => {
7486 isAdvancedCardsVisible : true ,
7587 } ;
7688 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
77- const { container } = editorRender ( < SettingsWidget { ... props } /> ) ;
89+ const { container } = renderSettingsWidget ( ) ;
7890 expect ( screen . queryByText ( 'Show advanced settings' ) ) . not . toBeInTheDocument ( ) ;
7991 expect ( container . querySelector ( 'showanswercard' ) ) . toBeInTheDocument ( ) ;
8092 expect ( container . querySelector ( 'resetcard' ) ) . toBeInTheDocument ( ) ;
@@ -86,12 +98,49 @@ describe('SettingsWidget', () => {
8698 isAdvancedCardsVisible : true ,
8799 } ;
88100 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
89- const { container } = editorRender (
90- < SettingsWidget { ...props } problemType = { ProblemTypeKeys . ADVANCED } /> ,
91- ) ;
101+ const { container } = renderSettingsWidget ( { problemType : ProblemTypeKeys . ADVANCED } ) ;
92102 expect ( container . querySelector ( 'randomization' ) ) . toBeInTheDocument ( ) ;
93103 } ) ;
94104 } ) ;
105+ describe ( 'SwitchEditorCard rendering (markdown vs advanced)' , ( ) => {
106+ test ( 'shows two SwitchEditorCard components when markdown is available and not currently enabled' , ( ) => {
107+ const showAdvancedSettingsCardsProps = {
108+ ...showAdvancedSettingsCardsBaseProps ,
109+ isAdvancedCardsVisible : true ,
110+ } ;
111+ jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
112+ const modifiedInitialState : PartialEditorState = {
113+ problem : {
114+ problemType : null , // non-advanced problem
115+ isMarkdownEditorEnabled : false , // currently in advanced/raw (or standard) editor
116+ rawOLX : '<problem></problem>' ,
117+ rawMarkdown : '## Problem' , // markdown content exists so button should appear
118+ isDirty : false ,
119+ } ,
120+ } ;
121+ const { container } = renderSettingsWidget ( { } , { initialState : modifiedInitialState } ) ;
122+ expect ( container . querySelectorAll ( 'switcheditorcard' ) ) . toHaveLength ( 2 ) ;
123+ } ) ;
124+
125+ test ( 'shows only the advanced SwitchEditorCard when already in markdown mode' , ( ) => {
126+ const showAdvancedSettingsCardsProps = {
127+ ...showAdvancedSettingsCardsBaseProps ,
128+ isAdvancedCardsVisible : true ,
129+ } ;
130+ jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
131+ const modifiedInitialState : PartialEditorState = {
132+ problem : {
133+ problemType : null ,
134+ isMarkdownEditorEnabled : true , // already in markdown editor, so markdown button hidden
135+ rawOLX : '<problem></problem>' ,
136+ rawMarkdown : '## Problem' ,
137+ isDirty : false ,
138+ } ,
139+ } ;
140+ const { container } = renderSettingsWidget ( { } , { initialState : modifiedInitialState } ) ;
141+ expect ( container . querySelectorAll ( 'switcheditorcard' ) ) . toHaveLength ( 1 ) ;
142+ } ) ;
143+ } ) ;
95144
96145 describe ( 'isLibrary' , ( ) => {
97146 const libraryProps = {
@@ -100,7 +149,7 @@ describe('SettingsWidget', () => {
100149 } ;
101150 test ( 'renders Settings widget page' , ( ) => {
102151 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsBaseProps ) ;
103- const { container } = editorRender ( < SettingsWidget { ... libraryProps } /> ) ;
152+ const { container } = renderSettingsWidget ( libraryProps ) ;
104153 expect ( container . querySelector ( 'timercard' ) ) . not . toBeInTheDocument ( ) ;
105154 expect ( container . querySelector ( 'resetcard' ) ) . not . toBeInTheDocument ( ) ;
106155 expect ( container . querySelector ( 'typecard' ) ) . toBeInTheDocument ( ) ;
@@ -114,7 +163,7 @@ describe('SettingsWidget', () => {
114163 isAdvancedCardsVisible : true ,
115164 } ;
116165 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
117- const { container } = editorRender ( < SettingsWidget { ... libraryProps } /> ) ;
166+ const { container } = renderSettingsWidget ( libraryProps ) ;
118167 expect ( screen . queryByText ( 'Show advanced settings' ) ) . not . toBeInTheDocument ( ) ;
119168 expect ( container . querySelector ( 'showanswearscard' ) ) . not . toBeInTheDocument ( ) ;
120169 expect ( container . querySelector ( 'resetcard' ) ) . not . toBeInTheDocument ( ) ;
@@ -128,7 +177,7 @@ describe('SettingsWidget', () => {
128177 isAdvancedCardsVisible : true ,
129178 } ;
130179 jest . spyOn ( hooks , 'showAdvancedSettingsCards' ) . mockReturnValue ( showAdvancedSettingsCardsProps ) ;
131- const { container } = editorRender ( < SettingsWidget { ...libraryProps } problemType = { ProblemTypeKeys . ADVANCED } /> ) ;
180+ const { container } = renderSettingsWidget ( { ...libraryProps , problemType : ProblemTypeKeys . ADVANCED } ) ;
132181 expect ( container . querySelector ( 'randomization' ) ) . toBeInTheDocument ( ) ;
133182 } ) ;
134183 } ) ;
0 commit comments