1- import { render } from '@testing-library/react' ;
1+ import { act , render } from '@testing-library/react' ;
22import { require as acequire } from 'ace-builds' ;
33import { FSModule } from 'browserfs/dist/node/core/FS' ;
44import { Chapter } from 'js-slang/dist/types' ;
@@ -12,9 +12,12 @@ import {
1212} from 'src/commons/application/ApplicationTypes' ;
1313import { WorkspaceSettingsContext } from 'src/commons/WorkspaceSettingsContext' ;
1414import { EditorBinding } from 'src/commons/WorkspaceSettingsContext' ;
15+ import ShareLinkStateEncoder from 'src/features/playground/shareLinks/encoder/Encoder' ;
16+ import { ShareLinkState } from 'src/features/playground/shareLinks/ShareLinkState' ;
1517import { createStore } from 'src/pages/createStore' ;
1618
17- import Playground , { handleHash } from '../Playground' ;
19+ import * as EncoderHooks from '../../../features/playground/shareLinks/encoder/EncoderHooks' ;
20+ import Playground , { setStateFromPlaygroundConfiguration } from '../Playground' ;
1821
1922// Mock inspector
2023( window as any ) . Inspector = jest . fn ( ) ;
@@ -31,6 +34,11 @@ describe('Playground tests', () => {
3134 let routes : RouteObject [ ] ;
3235 let mockStore : Store < OverallState > ;
3336
37+ // BrowserFS has to be mocked in nodejs environments
38+ jest
39+ . spyOn ( EncoderHooks , 'usePlaygroundConfigurationEncoder' )
40+ . mockReturnValue ( new ShareLinkStateEncoder ( { } as ShareLinkState ) ) ;
41+
3442 const getSourceChapterFromStore = ( store : Store < OverallState > ) =>
3543 store . getState ( ) . playground . languageConfig . chapter ;
3644 const getEditorValueFromStore = ( store : Store < OverallState > ) =>
@@ -81,38 +89,40 @@ describe('Playground tests', () => {
8189
8290 // Using @testing -library/react to render snapshot instead of react-test-renderer
8391 // as the useRefs require the notion of React DOM
84- const tree = render ( < RouterProvider router = { router } /> ) . container ;
92+ const tree = await act ( ( ) => render ( < RouterProvider router = { router } /> ) . container ) ;
8593 expect ( tree ) . toMatchSnapshot ( ) ;
8694
8795 expect ( getSourceChapterFromStore ( mockStore ) ) . toBe ( Chapter . SOURCE_2 ) ;
8896 expect ( getEditorValueFromStore ( mockStore ) ) . toBe ( "display('hello!');" ) ;
8997 } ) ;
9098
91- describe ( 'handleHash ' , ( ) => {
92- test ( 'disables loading hash with fullJS chapter in URL params ' , ( ) => {
93- const testHash = '#chap=-1&prgrm=CYSwzgDgNghgngCgOQAsCmUoHsCESCUA3EA' ;
99+ describe ( 'setStateFromPlaygroundConfiguration ' , ( ) => {
100+ test ( 'disables loading playground with fullJS/ fullTS chapter in playground configuration ' , ( ) => {
101+ const chaptersThatDisableLoading : Chapter [ ] = [ Chapter . FULL_JS , Chapter . FULL_TS ] ;
94102
95103 const mockHandleEditorValueChanged = jest . fn ( ) ;
96104 const mockHandleChapterSelect = jest . fn ( ) ;
97105 const mockHandleChangeExecTime = jest . fn ( ) ;
98106
99- handleHash (
100- testHash ,
101- {
102- handleChapterSelect : mockHandleChapterSelect ,
103- handleChangeExecTime : mockHandleChangeExecTime
104- } ,
105- 'playground' ,
106- // We cannot make use of 'dispatch' & BrowserFS in test cases. However, the
107- // behaviour being tested here does not actually invoke either of these. As
108- // a workaround, we pass in 'undefined' instead & cast to the expected types.
109- undefined as unknown as Dispatch ,
110- undefined as unknown as FSModule
111- ) ;
112-
113- expect ( mockHandleEditorValueChanged ) . not . toHaveBeenCalled ( ) ;
114- expect ( mockHandleChapterSelect ) . not . toHaveBeenCalled ( ) ;
115- expect ( mockHandleChangeExecTime ) . not . toHaveBeenCalled ( ) ;
107+ for ( const chap of chaptersThatDisableLoading ) {
108+ setStateFromPlaygroundConfiguration (
109+ { chap } as ShareLinkState ,
110+ {
111+ handleChapterSelect : mockHandleChapterSelect ,
112+ handleChangeExecTime : mockHandleChangeExecTime
113+ } ,
114+ 'playground' ,
115+ // We cannot make use of 'dispatch' & BrowserFS in test cases. However, the
116+ // behaviour being tested here does not actually invoke either of these. As
117+ // a workaround, we pass in 'undefined' instead & cast to the expected types.
118+ undefined as unknown as Dispatch ,
119+ null as unknown as FSModule
120+ ) ;
121+
122+ expect ( mockHandleEditorValueChanged ) . not . toHaveBeenCalled ( ) ;
123+ expect ( mockHandleChapterSelect ) . not . toHaveBeenCalled ( ) ;
124+ expect ( mockHandleChangeExecTime ) . not . toHaveBeenCalled ( ) ;
125+ }
116126 } ) ;
117127 } ) ;
118128} ) ;
0 commit comments