Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jsdom instead of window in jest test #1812

Merged
merged 13 commits into from Apr 29, 2019
16 changes: 9 additions & 7 deletions packages/app/src/app/components/CodeEditor/VSCode/index.tsx
Expand Up @@ -504,13 +504,15 @@ class MonacoEditor extends React.Component<Props> implements Editor {
this.currentTitle = newModule.title;
this.currentDirectoryShortid = newModule.directoryShortid;

if (errors) {
this.setErrors(errors);
}

if (corrections) {
this.setCorrections(corrections);
}
// Let the model load first
setTimeout(() => {
if (errors) {
this.setErrors(errors);
}
if (corrections) {
this.setCorrections(corrections);
}
}, 100);

if (this.props.onCodeReceived) {
// Whenever the user changes a module we set up a state that defines
Expand Down
Expand Up @@ -46,10 +46,3 @@ export const Blocks = styled.span`
font-weight: 300;
color: rgba(255, 255, 255, 0.7);
`;

export const Tests = styled.div`
padding: 1rem;
box-sizing: border-box;
overflow-y: overlay;
height: calc(100% - 4rem);
`;
Expand Up @@ -6,15 +6,8 @@ import PlayIcon from 'react-icons/lib/go/playback-play';
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
import { File, Status } from '../';

import {
Action,
TestName,
TestTitle,
Blocks,
Tests,
ErrorNotice,
} from './elements';
import { StatusElements } from '../elements';
import { Action, TestName, TestTitle, Blocks, ErrorNotice } from './elements';
import { StatusElements, Tests } from '../elements';

import TestBlock from './TestBlock';
import ErrorDetails from './ErrorDetails';
Expand Down
@@ -1,7 +1,7 @@

import styled from 'styled-components';

export const Container = styled.div`
position: relative;
height: 100%;
`;

Expand Down Expand Up @@ -32,13 +32,6 @@ export const TestStatuses = styled.div`
font-size: 0.875rem;
`;

export const Tests = styled.div`
padding: 1rem;
box-sizing: border-box;
overflow-y: overlay;
height: calc(100% - 4rem);
`;

export const HappyMessage = styled.div`
color: ${props => props.theme.green};
font-weight: 500;
Expand Down
Expand Up @@ -3,8 +3,9 @@
import * as React from 'react';

import { Test } from '../';
import { Tests } from '../elements';

import { Container, HappyMessage, Item, Tests, ItemTitle } from './elements';
import { Container, HappyMessage, Item, ItemTitle } from './elements';

import TestSummaryText from '../TestSummaryText';
import TestProgressBar from '../TestProgressBar';
Expand Down
@@ -1,4 +1,3 @@

import styled, { css } from 'styled-components';

import Check from 'react-icons/lib/go/check';
Expand Down Expand Up @@ -66,3 +65,17 @@ export const StatusElements = {
running: Loading,
idle: Dot,
};

export const Tests = styled.div`
padding: 1rem;
box-sizing: border-box;
overflow-y: auto;

/* Using absolute for correct scrolling, browsers have trouble handling
* an inner scroll inside a container unless the child is absolute */
position: absolute;
top: 3.5rem;
bottom: 0;
left: 0;
right: 0;
`;
10 changes: 10 additions & 0 deletions packages/app/src/app/store/modules/editor/actions.js
Expand Up @@ -276,6 +276,16 @@ export function addCorrectionFromPreview({ state, props }) {
state.push('editor.corrections', correction);
}

export function clearErrors({ state, props }) {
const currentErrors = state.get('editor.errors');

const newErrors = clearCorrectionsFromAction(currentErrors, props.action);

if (newErrors.length !== currentErrors.length) {
state.set('editor.errors', newErrors);
}
}

export function clearCorrections({ state, props }) {
const currentCorrections = state.get('editor.corrections');

Expand Down
1 change: 1 addition & 0 deletions packages/app/src/app/store/modules/editor/sequences.js
Expand Up @@ -297,6 +297,7 @@ export const handlePreviewAction = [
),
'show-error': actions.addErrorFromPreview,
'show-correction': actions.addCorrectionFromPreview,
'clear-errors': actions.clearErrors,
'clear-corrections': actions.clearCorrections,
'source.module.rename': [
actions.consumeRenameModuleFromPreview,
Expand Down
13 changes: 8 additions & 5 deletions packages/app/src/app/utils/corrections.ts
@@ -1,10 +1,13 @@
import { ModuleCorrection } from '@codesandbox/common/lib/types';
import { ModuleCorrection, ModuleError } from '@codesandbox/common/lib/types';
import { CorrectionClearAction } from 'codesandbox-api/dist/types/actions/correction';
import { ErrorClearAction } from 'codesandbox-api/dist/types/actions/error';

export function clearCorrectionsFromAction(
currentCorrections: ModuleCorrection[],
action: CorrectionClearAction
) {
export function clearCorrectionsFromAction<
T extends ModuleCorrection | ModuleError
>(
currentCorrections: T[],
action: CorrectionClearAction | ErrorClearAction
): T[] {
if (action.path === '*') {
return currentCorrections.filter(cor => cor.source !== action.source);
}
Expand Down
40 changes: 20 additions & 20 deletions packages/app/src/sandbox/compile.ts
Expand Up @@ -63,7 +63,7 @@ export function getHTMLParts(html: string) {
return { head: '', body: html };
}

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

Expand Down Expand Up @@ -384,6 +384,20 @@ overrideDocumentClose();

inject();

interface CompileOptions {
sandboxId: string;
modules: { [path: string]: Module };
externalResources: string[];
hasActions?: boolean;
isModuleView?: boolean;
template: TemplateType;
entry: string;
showOpenInCodeSandbox?: boolean;
skipEval?: boolean;
hasFileResolver?: boolean;
disableDependencyPreprocessing?: boolean;
}

async function compile({
sandboxId,
modules,
Expand All @@ -396,7 +410,7 @@ async function compile({
skipEval = false,
hasFileResolver = false,
disableDependencyPreprocessing = false,
}) {
}: CompileOptions) {
dispatch({
type: 'start',
});
Expand Down Expand Up @@ -485,7 +499,7 @@ async function compile({

const foundMain = isModuleView
? entry
: possibleEntries.find(p => modules[p]);
: possibleEntries.find(p => !!modules[p]);

if (!foundMain) {
throw new Error(
Expand Down Expand Up @@ -539,7 +553,7 @@ async function compile({
if (!manager.webpackHMR) {
const htmlModulePath = templateDefinition
.getHTMLEntries(configurations)
.find(p => modules[p]);
.find(p => !!modules[p]);
const htmlModule = modules[htmlModulePath];

const { head, body } = getHTMLParts(
Expand Down Expand Up @@ -706,21 +720,7 @@ async function compile({
}
}

type Arguments = {
sandboxId: string;
modules: Array<{
code: string;
path: string;
}>;
entry: string | undefined;
externalResources: Array<string>;
hasActions: boolean;
template: string;
showOpenInCodeSandbox?: boolean;
skipEval?: boolean;
};

const tasks: Array<Arguments> = [];
const tasks: CompileOptions[] = [];
let runningTask = false;

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

if (!runningTask) {
Expand Down
36 changes: 15 additions & 21 deletions packages/app/src/sandbox/eval/loaders/eval.js
Expand Up @@ -30,29 +30,23 @@ export default function(
const process = buildProcess(env);
g.global = global;

const globalsCode = Object.keys(globals).length
? ', ' + Object.keys(globals).join(', ')
: '';
const globalsValues = Object.keys(globals).map(k => globals[k]);
const allGlobals = {
require,
module: asUMD ? undefined : module,
exports: asUMD ? undefined : exports,
process,
setImmediate: requestFrame,
global: asUMD ? undefined : global,
...globals,
};

const allGlobalKeys = Object.keys(allGlobals);
const globalsCode = allGlobalKeys.length ? allGlobalKeys.join(', ') : '';
const globalsValues = allGlobalKeys.map(k => allGlobals[k]);
try {
const newCode =
`(function evaluate(require, module, exports, process, setImmediate, global` +
globalsCode +
`) {` +
code +
`\n})`;
const newCode = `(function evaluate(` + globalsCode + `) {` + code + `\n})`;
// eslint-disable-next-line no-eval
(0, eval)(newCode).apply(
this,
[
require,
asUMD ? undefined : module,
asUMD ? undefined : exports,
process,
requestFrame,
asUMD ? undefined : global,
].concat(globalsValues)
);
(0, eval)(newCode).apply(this, globalsValues);

return module.exports;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/sandbox/eval/manager.ts
Expand Up @@ -34,7 +34,7 @@ type Externals = {
[name: string]: string;
};

type ModuleObject = {
export type ModuleObject = {
[path: string]: Module;
};

Expand Down