Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 630bd2b

Browse files
committed
Tests: Add test for gather controller
1 parent 1c04e45 commit 630bd2b

21 files changed

+1413
-260
lines changed

babel.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// babel.config.js
2+
// All presets and plugins in this file, except for preset-typescript, are for transpiling
3+
// @jupyterlab source code during tests. preset-typescript is for transpiling the source code
4+
// from this project while running tests.
5+
module.exports = {
6+
plugins: ["@babel/plugin-proposal-class-properties", "inline-react-svg"],
7+
presets: [
8+
"@babel/preset-typescript",
9+
["@babel/preset-env", { targets: { node: "current" } }],
10+
"@babel/preset-react"
11+
]
12+
};

jest.config.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
14
module.exports = {
2-
preset: 'ts-jest',
3-
testEnvironment: 'node',
4-
};
5+
// Loads mocks necessary for tests.
6+
setupFiles: ["./src/test/setup.js"],
7+
8+
// Execute scripts within a DOM to allow JupyterLab code to run.
9+
// The test environment that will be used for testing
10+
testEnvironment: "jsdom",
11+
12+
testMatch: ["<rootDir>/src/test/**/*.test.ts"],
13+
14+
// Transpile all Typescript and Javascript files.
15+
transform: {
16+
"^.+\\.[tj]sx?$": require.resolve("babel-jest")
17+
},
18+
19+
// Transpile all @jupyterlab JavaScript files.
20+
transformIgnorePatterns: [".*/node_modules/(?!@jupyterlab/).*.js"]
21+
};

package-lock.json

+1,159-82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@
5151
"jquery": "^3.3.1"
5252
},
5353
"devDependencies": {
54+
"@babel/plugin-proposal-class-properties": "^7.5.5",
55+
"@babel/preset-env": "^7.6.3",
56+
"@babel/preset-react": "^7.6.3",
57+
"@babel/preset-typescript": "^7.6.0",
5458
"@types/diff-match-patch": "^1.0.32",
5559
"@types/jest": "^24.0.21",
5660
"@types/jquery": "^3.3.4",
5761
"@types/node": "^8.10.58",
62+
"babel-jest": "^24.9.0",
63+
"babel-plugin-inline-react-svg": "^1.1.0",
5864
"css-loader": "^1.0.0",
5965
"file-loader": "^1.1.11",
6066
"jest": "^24.9.0",
67+
"jest-fetch-mock": "^2.1.2",
6168
"jison": "^0.4.18",
6269
"prettier": "1.17.0",
6370
"raw-loader": "^0.5.1",

src/main/execution-logger.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
GatherModel,
3-
IGatherObserver,
4-
GatherModelEvent,
5-
GatherEventData,
6-
} from '../model';
7-
import { LabCell } from '../model/labcell';
1+
import { GatherEventData, GatherModel, GatherModelEvent, IGatherObserver } from "../model";
2+
import { LabCell } from "../model/cell";
83

94
export class ExecutionLogger implements IGatherObserver {
105
constructor(gatherModel: GatherModel) {

src/main/gather-actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SlicedExecution } from "@msrvida/python-program-analysis";
99
import { JSONObject } from "@phosphor/coreutils";
1010
import { Signal } from "@phosphor/signaling";
1111
import { OutputSelection } from "../model";
12-
import { LogCell } from "../model/labcell";
12+
import { LogCell } from "../model/cell";
1313

1414
/**
1515
* Listens to changes to the clipboard.

src/main/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { DisposableDelegate, IDisposable } from "@phosphor/disposable";
1010
import { Widget } from "@phosphor/widgets";
1111
import "../../style/index.css";
1212
import { GatherController, GatherModel, GatherState, SliceSelection } from "../model";
13+
import { LogCell } from "../model/cell";
1314
import { GatherModelRegistry, getGatherModelForActiveNotebook } from "../model/gather-registry";
14-
import { LogCell } from "../model/labcell";
1515
import { CellChangeListener } from "../overlay/cell-listener";
1616
import { MarkerManager } from "../overlay/gather-markers";
1717
import { NotifactionExtension as NotificationExtension } from "../overlay/notification";

src/model/labcell.ts src/model/cell.ts

+18-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { UUID } from "@phosphor/coreutils";
88
* Abstract class for accessing cell data.
99
*/
1010
export abstract class AbstractCell implements Cell {
11-
abstract is_cell: boolean;
1211
abstract id: string;
1312
abstract executionCount: number;
1413
abstract executionEventId: string;
@@ -77,37 +76,37 @@ export abstract class AbstractCell implements Cell {
7776
}
7877
}
7978

79+
export interface LogCellOptions {
80+
id?: string;
81+
executionCount?: number;
82+
persistentId?: string;
83+
executionEventId?: string;
84+
hasError?: boolean;
85+
text?: string;
86+
outputs?: nbformat.IOutput[];
87+
}
88+
8089
/**
8190
* Static cell data. Provides an interfaces to cell data loaded from a log.
8291
*/
8392
export class LogCell extends AbstractCell {
84-
constructor(data: {
85-
id?: string;
86-
executionCount?: number;
87-
persistentId?: string;
88-
executionEventId?: string;
89-
hasError?: boolean;
90-
text?: string;
91-
outputs?: nbformat.IOutput[];
92-
}) {
93+
constructor(options: LogCellOptions) {
9394
super();
94-
this.is_cell = true;
95-
this.id = data.id || UUID.uuid4();
96-
this.executionCount = data.executionCount || undefined;
97-
this.persistentId = data.persistentId || UUID.uuid4();
98-
this.executionEventId = data.executionEventId || UUID.uuid4();
99-
this.hasError = data.hasError || false;
100-
this.text = data.text || "";
95+
this.id = options.id || UUID.uuid4();
96+
this.executionCount = options.executionCount || undefined;
97+
this.persistentId = options.persistentId || UUID.uuid4();
98+
this.executionEventId = options.executionEventId || UUID.uuid4();
99+
this.hasError = options.hasError || false;
100+
this.text = options.text || "";
101101
this.lastExecutedText = this.text;
102-
this.outputs = data.outputs || [];
102+
this.outputs = options.outputs || [];
103103
this.gathered = false;
104104
}
105105

106106
deepCopy(): AbstractCell {
107107
return new LogCell(this);
108108
}
109109

110-
readonly is_cell: boolean;
111110
readonly id: string;
112111
readonly executionCount: number;
113112
readonly persistentId: string;
@@ -215,7 +214,5 @@ export class LabCell extends AbstractCell {
215214
return this._model.toJSON();
216215
}
217216

218-
is_cell: boolean = true;
219-
is_outputter_cell: boolean = true;
220217
private _model: ICodeCellModel;
221218
}

src/model/controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ExecutionLogSlicer, LocationSet } from "@msrvida/python-program-analysi
44
import { GatherEventData, GatherModel, GatherModelEvent, GatherState, IGatherObserver } from ".";
55
import { Clipboard, NotebookOpener, ScriptOpener } from "../main/gather-actions";
66
import { log } from "../util/log";
7-
import { LogCell } from "./labcell";
7+
import { LogCell } from "./cell";
88
import { DefSelection, OutputSelection } from "./selections";
99

1010
/**

src/model/model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SlicedExecution
66
} from "@msrvida/python-program-analysis";
77
import { log } from "../util/log";
8-
import { LogCell } from "./labcell";
8+
import { LogCell } from "./cell";
99
import { CellOutput, DefSelection, EditorDef, OutputSelection, SliceSelection } from "./selections";
1010

1111
/**

src/model/selections.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ref, SlicedExecution, Cell } from '@msrvida/python-program-analysis';
1+
import { Cell, Ref, SlicedExecution } from "@msrvida/python-program-analysis";
22

33
/**
44
* A user's selection.
@@ -22,51 +22,45 @@ export class DefSelection {
2222
return {
2323
defType: this.editorDef.def.type,
2424
defLevel: this.editorDef.def.level,
25-
cell: this.cell,
25+
cell: this.cell
2626
};
2727
}
2828
}
2929

3030
/**
3131
* A slice selected for a def.
3232
*/
33-
export type SliceSelection = {
33+
export interface SliceSelection {
3434
userSelection: UserSelection;
3535
slice: SlicedExecution;
36-
};
36+
}
3737

3838
/**
3939
* A def located in an editor.
4040
*/
41-
export type EditorDef = {
41+
export interface EditorDef {
4242
editor: CodeMirror.Editor;
4343
cell: Cell;
4444
def: Ref;
45-
};
45+
}
4646

4747
/**
4848
* An output for a cell.
4949
*/
50-
export type CellOutput = {
50+
export interface CellOutput {
5151
outputIndex: number;
5252
element: HTMLElement;
5353
cell: Cell;
54-
};
54+
}
5555

5656
/**
5757
* An ouput selected for a cell.
5858
*/
59-
export type OutputSelection = {
59+
export interface OutputSelection {
6060
outputIndex: number;
6161
cell: Cell;
62-
};
63-
export function instanceOfOutputSelection(
64-
object: any
65-
): object is OutputSelection {
66-
return (
67-
object &&
68-
typeof object == 'object' &&
69-
'outputIndex' in object &&
70-
'cell' in object
71-
);
62+
}
63+
64+
export function instanceOfOutputSelection(object: any): object is OutputSelection {
65+
return object && typeof object == "object" && "outputIndex" in object && "cell" in object;
7266
}

src/overlay/cell-listener.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { CodeCellModel, ICellModel, ICodeCellModel } from '@jupyterlab/cells';
2-
import { NotebookPanel } from '@jupyterlab/notebook';
3-
import { IObservableList } from '@jupyterlab/observables';
4-
import { GatherModel } from '../model';
5-
import { LabCell } from '../model/labcell';
6-
import { UUID } from '@phosphor/coreutils';
1+
import { CodeCellModel, ICellModel, ICodeCellModel } from "@jupyterlab/cells";
2+
import { NotebookPanel } from "@jupyterlab/notebook";
3+
import { IObservableList } from "@jupyterlab/observables";
4+
import { UUID } from "@phosphor/coreutils";
5+
import { GatherModel } from "../model";
6+
import { LabCell } from "../model/cell";
77

88
/**
99
* Listens to cell executions and edits.
@@ -35,7 +35,7 @@ export class CellChangeListener {
3535
}
3636

3737
private _registerCell(cell: ICellModel) {
38-
if (cell.type !== 'code') {
38+
if (cell.type !== "code") {
3939
return;
4040
}
4141
/*
@@ -44,7 +44,7 @@ export class CellChangeListener {
4444
*/
4545
cell.stateChanged.connect((changedCell, cellStateChange) => {
4646
if (
47-
cellStateChange.name === 'executionCount' &&
47+
cellStateChange.name === "executionCount" &&
4848
cellStateChange.newValue !== undefined &&
4949
cellStateChange.newValue !== null
5050
) {
@@ -64,14 +64,12 @@ export class CellChangeListener {
6464
});
6565
}
6666

67-
private _registerAddedCells(
68-
cellListChange: IObservableList.IChangedArgs<ICellModel>
69-
): void {
70-
if (cellListChange.type === 'add' || cellListChange.type === 'remove') {
67+
private _registerAddedCells(cellListChange: IObservableList.IChangedArgs<ICellModel>): void {
68+
if (cellListChange.type === "add" || cellListChange.type === "remove") {
7169
const cellModel = cellListChange.newValues[0] as ICellModel;
72-
if (cellListChange.type === 'add') {
70+
if (cellListChange.type === "add") {
7371
this._registerCell(cellModel);
74-
} else if (cellListChange.type === 'remove') {
72+
} else if (cellListChange.type === "remove") {
7573
if (cellModel instanceof CodeCellModel) {
7674
this._gatherModel.lastDeletedCell = new LabCell(cellModel);
7775
}

src/overlay/element-finder.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as py from '@msrvida/python-program-analysis';
2-
import { Cell, isCodeCellModel } from '@jupyterlab/cells';
3-
import { CodeMirrorEditor } from '@jupyterlab/codemirror';
4-
import { NotebookPanel } from '@jupyterlab/notebook';
5-
import CodeMirror from 'codemirror';
6-
import { LabCell } from '../model/labcell';
1+
import { Cell, isCodeCellModel } from "@jupyterlab/cells";
2+
import { CodeMirrorEditor } from "@jupyterlab/codemirror";
3+
import { NotebookPanel } from "@jupyterlab/notebook";
4+
import * as py from "@msrvida/python-program-analysis";
5+
import CodeMirror from "codemirror";
6+
import { LabCell } from "../model/cell";
77

88
/**
99
* Finds the HTML elements in a notebook corresponding to a cell. Useful for looking up HTML
@@ -56,7 +56,7 @@ export class NotebookElementFinder {
5656
return outputElements;
5757
}
5858
let cellElement = cellWidget.node;
59-
var outputNodes = cellElement.querySelectorAll('.jp-OutputArea-output');
59+
var outputNodes = cellElement.querySelectorAll(".jp-OutputArea-output");
6060
for (var i = 0; i < outputNodes.length; i++) {
6161
if (outputNodes[i] instanceof HTMLElement) {
6262
outputElements.push(outputNodes[i] as HTMLElement);

src/overlay/gather-markers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
IGatherObserver,
2222
OutputSelection
2323
} from "../model";
24-
import { LabCell } from "../model/labcell";
24+
import { LabCell } from "../model/cell";
2525
import { log } from "../util/log";
2626
import { NotebookElementFinder } from "./element-finder";
2727

src/persistence/load.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CellExecution } from "@msrvida/python-program-analysis";
44
import { JSONArray, JSONExt, JSONObject } from "@phosphor/coreutils";
55
import { log } from "util";
66
import { GatherModel } from "../model";
7-
import { LogCell } from "../model/labcell";
7+
import { LogCell } from "../model/cell";
88

99
/**
1010
* Key for accessing execution history in Jupyter notebook metadata.

src/persistence/store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { nbformat } from "@jupyterlab/coreutils";
22
import { INotebookModel } from "@jupyterlab/notebook";
33
import { ExecutionLogSlicer } from "@msrvida/python-program-analysis";
44
import { JSONArray, JSONObject } from "@phosphor/coreutils";
5-
import { LogCell } from "../model/labcell";
5+
import { LogCell } from "../model/cell";
66
import { EXECUTION_HISTORY_METADATA_KEY } from "./load";
77

88
interface CellExecutionJson extends JSONObject {

0 commit comments

Comments
 (0)