1
- import { render } from '@testing-library/react' ;
1
+ import { act , render } from '@testing-library/react' ;
2
2
import { require as acequire } from 'ace-builds' ;
3
3
import { FSModule } from 'browserfs/dist/node/core/FS' ;
4
4
import { Chapter } from 'js-slang/dist/types' ;
@@ -12,9 +12,12 @@ import {
12
12
} from 'src/commons/application/ApplicationTypes' ;
13
13
import { WorkspaceSettingsContext } from 'src/commons/WorkspaceSettingsContext' ;
14
14
import { EditorBinding } from 'src/commons/WorkspaceSettingsContext' ;
15
+ import ShareLinkStateEncoder from 'src/features/playground/shareLinks/encoder/Encoder' ;
16
+ import { ShareLinkState } from 'src/features/playground/shareLinks/ShareLinkState' ;
15
17
import { createStore } from 'src/pages/createStore' ;
16
18
17
- import Playground , { handleHash } from '../Playground' ;
19
+ import * as EncoderHooks from '../../../features/playground/shareLinks/encoder/EncoderHooks' ;
20
+ import Playground , { setStateFromPlaygroundConfiguration } from '../Playground' ;
18
21
19
22
// Mock inspector
20
23
( window as any ) . Inspector = jest . fn ( ) ;
@@ -31,6 +34,11 @@ describe('Playground tests', () => {
31
34
let routes : RouteObject [ ] ;
32
35
let mockStore : Store < OverallState > ;
33
36
37
+ // BrowserFS has to be mocked in nodejs environments
38
+ jest
39
+ . spyOn ( EncoderHooks , 'usePlaygroundConfigurationEncoder' )
40
+ . mockReturnValue ( new ShareLinkStateEncoder ( { } as ShareLinkState ) ) ;
41
+
34
42
const getSourceChapterFromStore = ( store : Store < OverallState > ) =>
35
43
store . getState ( ) . playground . languageConfig . chapter ;
36
44
const getEditorValueFromStore = ( store : Store < OverallState > ) =>
@@ -81,38 +89,40 @@ describe('Playground tests', () => {
81
89
82
90
// Using @testing -library/react to render snapshot instead of react-test-renderer
83
91
// 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 ) ;
85
93
expect ( tree ) . toMatchSnapshot ( ) ;
86
94
87
95
expect ( getSourceChapterFromStore ( mockStore ) ) . toBe ( Chapter . SOURCE_2 ) ;
88
96
expect ( getEditorValueFromStore ( mockStore ) ) . toBe ( "display('hello!');" ) ;
89
97
} ) ;
90
98
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 ] ;
94
102
95
103
const mockHandleEditorValueChanged = jest . fn ( ) ;
96
104
const mockHandleChapterSelect = jest . fn ( ) ;
97
105
const mockHandleChangeExecTime = jest . fn ( ) ;
98
106
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
+ }
116
126
} ) ;
117
127
} ) ;
118
128
} ) ;
0 commit comments