Skip to content

Commit 1497507

Browse files
authored
Use jsdom instead of window in jest test (#1812)
* Hacky implementation of jsdom inside jest tests * Update path to jsdom * Properly set globals according to jest defaults * Properly set global to jsdom window * Make globals dynamic * Improve scrolling of tests view * Proper fix for the styling of tests * Convert jest-lite to typescript * Fix scrolling for the full test details * Deduplicate Tests container * Properly mark errors as jest errors, clear them on new start * Move clear error to less spammy place * Fix error loading later on in the editor
1 parent de37da9 commit 1497507

File tree

20 files changed

+189322
-237
lines changed

20 files changed

+189322
-237
lines changed

packages/app/src/app/components/CodeEditor/VSCode/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,15 @@ class MonacoEditor extends React.Component<Props> implements Editor {
504504
this.currentTitle = newModule.title;
505505
this.currentDirectoryShortid = newModule.directoryShortid;
506506

507-
if (errors) {
508-
this.setErrors(errors);
509-
}
510-
511-
if (corrections) {
512-
this.setCorrections(corrections);
513-
}
507+
// Let the model load first
508+
setTimeout(() => {
509+
if (errors) {
510+
this.setErrors(errors);
511+
}
512+
if (corrections) {
513+
this.setCorrections(corrections);
514+
}
515+
}, 100);
514516

515517
if (this.props.onCodeReceived) {
516518
// Whenever the user changes a module we set up a state that defines

packages/app/src/app/components/Preview/DevTools/Tests/TestDetails/elements.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,3 @@ export const Blocks = styled.span`
4646
font-weight: 300;
4747
color: rgba(255, 255, 255, 0.7);
4848
`;
49-
50-
export const Tests = styled.div`
51-
padding: 1rem;
52-
box-sizing: border-box;
53-
overflow-y: overlay;
54-
height: calc(100% - 4rem);
55-
`;

packages/app/src/app/components/Preview/DevTools/Tests/TestDetails/index.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ import PlayIcon from 'react-icons/lib/go/playback-play';
66
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
77
import { File, Status } from '../';
88

9-
import {
10-
Action,
11-
TestName,
12-
TestTitle,
13-
Blocks,
14-
Tests,
15-
ErrorNotice,
16-
} from './elements';
17-
import { StatusElements } from '../elements';
9+
import { Action, TestName, TestTitle, Blocks, ErrorNotice } from './elements';
10+
import { StatusElements, Tests } from '../elements';
1811

1912
import TestBlock from './TestBlock';
2013
import ErrorDetails from './ErrorDetails';

packages/app/src/app/components/Preview/DevTools/Tests/TestOverview/elements.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
import styled from 'styled-components';
32

43
export const Container = styled.div`
4+
position: relative;
55
height: 100%;
66
`;
77

@@ -32,13 +32,6 @@ export const TestStatuses = styled.div`
3232
font-size: 0.875rem;
3333
`;
3434

35-
export const Tests = styled.div`
36-
padding: 1rem;
37-
box-sizing: border-box;
38-
overflow-y: overlay;
39-
height: calc(100% - 4rem);
40-
`;
41-
4235
export const HappyMessage = styled.div`
4336
color: ${props => props.theme.green};
4437
font-weight: 500;

packages/app/src/app/components/Preview/DevTools/Tests/TestOverview/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import * as React from 'react';
44

55
import { Test } from '../';
6+
import { Tests } from '../elements';
67

7-
import { Container, HappyMessage, Item, Tests, ItemTitle } from './elements';
8+
import { Container, HappyMessage, Item, ItemTitle } from './elements';
89

910
import TestSummaryText from '../TestSummaryText';
1011
import TestProgressBar from '../TestProgressBar';

packages/app/src/app/components/Preview/DevTools/Tests/elements.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import styled, { css } from 'styled-components';
32

43
import Check from 'react-icons/lib/go/check';
@@ -66,3 +65,17 @@ export const StatusElements = {
6665
running: Loading,
6766
idle: Dot,
6867
};
68+
69+
export const Tests = styled.div`
70+
padding: 1rem;
71+
box-sizing: border-box;
72+
overflow-y: auto;
73+
74+
/* Using absolute for correct scrolling, browsers have trouble handling
75+
* an inner scroll inside a container unless the child is absolute */
76+
position: absolute;
77+
top: 3.5rem;
78+
bottom: 0;
79+
left: 0;
80+
right: 0;
81+
`;

packages/app/src/app/store/modules/editor/actions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ export function addCorrectionFromPreview({ state, props }) {
276276
state.push('editor.corrections', correction);
277277
}
278278

279+
export function clearErrors({ state, props }) {
280+
const currentErrors = state.get('editor.errors');
281+
282+
const newErrors = clearCorrectionsFromAction(currentErrors, props.action);
283+
284+
if (newErrors.length !== currentErrors.length) {
285+
state.set('editor.errors', newErrors);
286+
}
287+
}
288+
279289
export function clearCorrections({ state, props }) {
280290
const currentCorrections = state.get('editor.corrections');
281291

packages/app/src/app/store/modules/editor/sequences.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export const handlePreviewAction = [
297297
),
298298
'show-error': actions.addErrorFromPreview,
299299
'show-correction': actions.addCorrectionFromPreview,
300+
'clear-errors': actions.clearErrors,
300301
'clear-corrections': actions.clearCorrections,
301302
'source.module.rename': [
302303
actions.consumeRenameModuleFromPreview,

packages/app/src/app/utils/corrections.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { ModuleCorrection } from '@codesandbox/common/lib/types';
1+
import { ModuleCorrection, ModuleError } from '@codesandbox/common/lib/types';
22
import { CorrectionClearAction } from 'codesandbox-api/dist/types/actions/correction';
3+
import { ErrorClearAction } from 'codesandbox-api/dist/types/actions/error';
34

4-
export function clearCorrectionsFromAction(
5-
currentCorrections: ModuleCorrection[],
6-
action: CorrectionClearAction
7-
) {
5+
export function clearCorrectionsFromAction<
6+
T extends ModuleCorrection | ModuleError
7+
>(
8+
currentCorrections: T[],
9+
action: CorrectionClearAction | ErrorClearAction
10+
): T[] {
811
if (action.path === '*') {
912
return currentCorrections.filter(cor => cor.source !== action.source);
1013
}

packages/app/src/sandbox/compile.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function getHTMLParts(html: string) {
6363
return { head: '', body: html };
6464
}
6565

66-
function sendTestCount(manager: Manager, modules: Array<Module>) {
66+
function sendTestCount(manager: Manager, modules: { [path: string]: Module }) {
6767
const testRunner = manager.testRunner;
6868
const tests = testRunner.findTests(modules);
6969

@@ -395,6 +395,20 @@ overrideDocumentClose();
395395

396396
inject();
397397

398+
interface CompileOptions {
399+
sandboxId: string;
400+
modules: { [path: string]: Module };
401+
externalResources: string[];
402+
hasActions?: boolean;
403+
isModuleView?: boolean;
404+
template: TemplateType;
405+
entry: string;
406+
showOpenInCodeSandbox?: boolean;
407+
skipEval?: boolean;
408+
hasFileResolver?: boolean;
409+
disableDependencyPreprocessing?: boolean;
410+
}
411+
398412
async function compile({
399413
sandboxId,
400414
modules,
@@ -407,7 +421,7 @@ async function compile({
407421
skipEval = false,
408422
hasFileResolver = false,
409423
disableDependencyPreprocessing = false,
410-
}) {
424+
}: CompileOptions) {
411425
dispatch({
412426
type: 'start',
413427
});
@@ -496,7 +510,7 @@ async function compile({
496510

497511
const foundMain = isModuleView
498512
? entry
499-
: possibleEntries.find(p => modules[p]);
513+
: possibleEntries.find(p => !!modules[p]);
500514

501515
if (!foundMain) {
502516
throw new Error(
@@ -550,7 +564,7 @@ async function compile({
550564
if (!manager.webpackHMR) {
551565
const htmlModulePath = templateDefinition
552566
.getHTMLEntries(configurations)
553-
.find(p => modules[p]);
567+
.find(p => !!modules[p]);
554568
const htmlModule = modules[htmlModulePath];
555569

556570
const { head, body } = getHTMLParts(
@@ -717,21 +731,7 @@ async function compile({
717731
}
718732
}
719733

720-
type Arguments = {
721-
sandboxId: string;
722-
modules: Array<{
723-
code: string;
724-
path: string;
725-
}>;
726-
entry: string | undefined;
727-
externalResources: Array<string>;
728-
hasActions: boolean;
729-
template: string;
730-
showOpenInCodeSandbox?: boolean;
731-
skipEval?: boolean;
732-
};
733-
734-
const tasks: Array<Arguments> = [];
734+
const tasks: CompileOptions[] = [];
735735
let runningTask = false;
736736

737737
async function executeTaskIfAvailable() {
@@ -752,7 +752,7 @@ async function executeTaskIfAvailable() {
752752
* and if there are 3 tasks we will remove the second task, this one is unnecessary as it is not the
753753
* latest version.
754754
*/
755-
export default function queueTask(data: Arguments) {
755+
export default function queueTask(data: CompileOptions) {
756756
tasks[0] = data;
757757

758758
if (!runningTask) {

0 commit comments

Comments
 (0)