Skip to content

Commit 5630af6

Browse files
committed
Mock BrowserFS in nodejs test environment
1 parent c24d3a3 commit 5630af6

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

src/pages/playground/__tests__/Playground.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from '@testing-library/react';
1+
import { act, render } from '@testing-library/react';
22
import { require as acequire } from 'ace-builds';
33
import { FSModule } from 'browserfs/dist/node/core/FS';
44
import { Chapter } from 'js-slang/dist/types';
@@ -12,9 +12,12 @@ import {
1212
} from 'src/commons/application/ApplicationTypes';
1313
import { WorkspaceSettingsContext } from 'src/commons/WorkspaceSettingsContext';
1414
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';
1517
import { 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
});

src/pages/playground/__tests__/__snapshots__/Playground.tsx.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,9 +1922,21 @@ exports[`Playground tests Playground with link renders correctly 1`] = `
19221922
>
19231923
<span
19241924
aria-hidden="true"
1925-
class="bp5-icon bp5-icon-large bp5-icon-flow-review"
1926-
data-icon="flow-review"
1927-
/>
1925+
class="bp5-icon bp5-icon-flow-review"
1926+
>
1927+
<svg
1928+
data-icon="flow-review"
1929+
height="20"
1930+
role="img"
1931+
viewBox="0 0 20 20"
1932+
width="20"
1933+
>
1934+
<path
1935+
d="M6.13 9.004A4.005 4.005 0 0110.012 6c1.87 0 3.44 1.278 3.881 3.005h2.768l-2.354-2.317a.97.97 0 01-.283-.691c0-.536.462-.995 1-.995.273 0 .517.107.693.283l4 4.041a.97.97 0 01.284.692.956.956 0 01-.293.682l-3.991 3.997a.944.944 0 01-.694.292c-.537 0-1-.46-1-.997a.97.97 0 01.284-.692l2.345-2.29h-2.765a4.005 4.005 0 01-3.875 2.981 4.005 4.005 0 01-3.874-2.981H3.349l2.376 2.308a.97.97 0 01.283.691 1 1 0 01-.994.983.989.989 0 01-.713-.291L.293 10.699A.956.956 0 010 10.017a.97.97 0 01.283-.692l4.03-4.037a.996.996 0 01.701-.283c.537 0 .994.464.994 1a.97.97 0 01-.283.691L3.34 9.004h2.79z"
1936+
fill-rule="evenodd"
1937+
/>
1938+
</svg>
1939+
</span>
19281940
</div>
19291941
</span>
19301942
</div>

0 commit comments

Comments
 (0)