Skip to content

Commit

Permalink
Unify build api code (#572)
Browse files Browse the repository at this point in the history
* Pass quarantine in to ToplevelBlockEditable
* make action creators for quarantine state changes
* pass store directly to buildAPI
* Move block mode state into redux store
* extract buildAPI function out of ToggleEditor component
* Remove passedAST from BlockEditor
* remove duplicate ast state from ToggleEditor
* Rip out recorded marks. See #571
* Move code state into redux store
* Remove need to pass handleToggle to buildAPI
* Unify all buildApi related functions
* Ditch untested event copying code
* move api building all the way to the top level
* Only construct codemirror facade when needed
* expose redux store to tests using mountCMB helper
  • Loading branch information
pcardune committed Nov 15, 2021
1 parent 6d0f034 commit cae2596
Show file tree
Hide file tree
Showing 23 changed files with 619 additions and 690 deletions.
8 changes: 4 additions & 4 deletions packages/codemirror-blocks/spec/activation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("when dealing with node activation,", () => {
let literal1!: ASTNode;
let literal2!: ASTNode;
beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;
cmb.setValue("11\n54");
const ast = cmb.getAst();
literal1 = ast.rootNodes[0];
Expand Down Expand Up @@ -137,7 +137,7 @@ describe("cut/copy/paste", () => {
let literal2!: ASTNode;

beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;

cmb.setValue("11\n54");
const ast = cmb.getAst();
Expand Down Expand Up @@ -182,7 +182,7 @@ describe("tree navigation", () => {
let lastNode: ASTNode;

beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;

cmb.setValue("(+ 1 2 3) 99 (* 7 (* 1 2))");
const ast = cmb.getAst();
Expand Down Expand Up @@ -333,7 +333,7 @@ describe("when dealing with node selection, ", () => {
let literal2!: ASTNode;
let expr!: FunctionAppNode;
beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;

cmb.setValue("11\n54\n(+ 1 2)");
const ast = cmb.getAst();
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-blocks/spec/api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("when testing CM apis,", () => {
const currentThirdRoot = () => roots()[2];

beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;
cmb.setBlockMode(false);
cmb.setValue(`(+ 1 2)\ny`);
});
Expand Down
23 changes: 15 additions & 8 deletions packages/codemirror-blocks/spec/comment-test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { act } from "@testing-library/react";
import { ASTNode } from "../src/ast";
import { API } from "../src/CodeMirrorBlocks";
import { Pos } from "../src/editor";
import wescheme from "../src/languages/wescheme";
import * as actions from "../src/state/actions";
import { AppStore } from "../src/state/store";

import { wait, teardown, click, mountCMB } from "../src/toolkit/test-utils";

const QUARANTINE_DELAY = 2000;

describe("When editing and moving commented nodes", function () {
let cmb!: API;
beforeEach(async function () {
cmb = await mountCMB(wescheme);
let store!: AppStore;
const setQuarantine = (start: Pos, end: Pos, text: string) =>
act(() => {
store.dispatch(actions.setQuarantine(start, end, text));
});
beforeEach(() => {
const mounted = mountCMB(wescheme);
cmb = mounted.cmb;
store = mounted.store;
});

afterEach(function () {
Expand Down Expand Up @@ -41,11 +52,7 @@ describe("When editing and moving commented nodes", function () {

// TODO(pcardune) reenable
xit("you should be able to insert a commented node after a commented node", async function () {
cmb.setQuarantine(
{ line: 3, ch: 1 },
{ line: 3, ch: 1 },
"1 #| comment1 |#"
);
setQuarantine({ line: 3, ch: 1 }, { line: 3, ch: 1 }, "1 #| comment1 |#");
await wait(QUARANTINE_DELAY);
click(expr0);
expect(cmb.getValue()).toBe(`(comment free)
Expand All @@ -57,7 +64,7 @@ describe("When editing and moving commented nodes", function () {

// TODO(pcardune) reenable
xit("you should be able to insert a commented node after an uncommented node", async function () {
cmb.setQuarantine(
setQuarantine(
{ line: 0, ch: 14 },
{ line: 0, ch: 14 },
"1 #| comment1 |#"
Expand Down
4 changes: 2 additions & 2 deletions packages/codemirror-blocks/spec/drag-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { fireEvent } from "@testing-library/react";

describe("Drag and drop", () => {
let cmb!: API;
beforeEach(async () => {
cmb = await mountCMB(wescheme);
beforeEach(() => {
cmb = mountCMB(wescheme).cmb;
});

afterEach(teardown);
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-blocks/spec/focus-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("focusing,", () => {
let literal3!: ASTNode;

beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;
cmb.setValue("(+ 1 2 3)");
expression = cmb.getAst().rootNodes[0] as FunctionAppNode;
func = expression.fields.func;
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-blocks/spec/markText-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("The CodeMirrorBlocks Class", function () {
let literal1!: ASTNode;
let expression!: FunctionAppNode;
beforeEach(async function () {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;
cmb.setValue("11\n12\n(+ 3 4 5)");
cmb.getAllMarks().forEach((m) => m.clear());
ast = cmb.getAst();
Expand Down
41 changes: 23 additions & 18 deletions packages/codemirror-blocks/spec/new-blocks-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { AST, ASTNode } from "../src/ast";
import type { API } from "../src/CodeMirrorBlocks";
import wescheme from "../src/languages/wescheme";
import { FunctionAppNode } from "../src/nodes";
import { AppStore } from "../src/state/store";
import * as actions from "../src/state/actions";

import {
teardown,
Expand All @@ -13,11 +15,19 @@ import {
mountCMB,
isNodeEditable,
} from "../src/toolkit/test-utils";
import { Pos } from "../src/editor";

describe("The CodeMirrorBlocks Class", function () {
let cmb!: API;
beforeEach(async function () {
cmb = await mountCMB(wescheme);
let store!: AppStore;
const setQuarantine = (start: Pos, end: Pos, text: string) =>
act(() => {
store.dispatch(actions.setQuarantine(start, end, text));
});
beforeEach(() => {
const mounted = mountCMB(wescheme);
cmb = mounted.cmb;
store = mounted.store;
});

afterEach(function () {
Expand Down Expand Up @@ -50,7 +60,6 @@ describe("The CodeMirrorBlocks Class", function () {
let literal!: ASTNode;
beforeEach(async function () {
cmb.setValue("11");
cmb.setBlockMode(true);
literal = cmb.getAst().rootNodes[0];
});

Expand All @@ -60,7 +69,7 @@ describe("The CodeMirrorBlocks Class", function () {
});
it("typing at the end of a line", async function () {
act(() => {
cmb.setQuarantine(
setQuarantine(
{
line: 0,
ch: 2,
Expand All @@ -80,24 +89,20 @@ describe("The CodeMirrorBlocks Class", function () {
expect(cmb.getValue()).toEqual("42\n9\n11");
});
it("typing at the beginning of a line", async function () {
act(() => {
cmb.setQuarantine(
{ line: 0, ch: 0, xRel: 0 } as CodeMirror.Position,
{ line: 0, ch: 0, xRel: 0 } as CodeMirror.Position,
"9"
);
});
setQuarantine(
{ line: 0, ch: 0, xRel: 0 } as CodeMirror.Position,
{ line: 0, ch: 0, xRel: 0 } as CodeMirror.Position,
"9"
);
keyDown("Enter");
expect(cmb.getValue()).toEqual("9\n42\n11");
});
it("typing between two blocks on a line", async function () {
act(() => {
cmb.setQuarantine(
{ line: 0, ch: 3, xRel: 0 } as CodeMirror.Position,
{ line: 0, ch: 3, xRel: 0 } as CodeMirror.Position,
"9"
);
});
setQuarantine(
{ line: 0, ch: 3, xRel: 0 } as CodeMirror.Position,
{ line: 0, ch: 3, xRel: 0 } as CodeMirror.Position,
"9"
);
keyDown("Enter");
expect(cmb.getValue()).toEqual("42\n9\n11");
});
Expand Down
2 changes: 2 additions & 0 deletions packages/codemirror-blocks/spec/state/store-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe("createAppStore()", () => {
"nodeNIdMap": Map {},
"rootNodes": Array [],
},
"blockMode": false,
"code": "",
"collapsedList": Array [],
"editable": Object {},
"errorId": "",
Expand Down
2 changes: 0 additions & 2 deletions packages/codemirror-blocks/spec/ui/BlockEditor-test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cleanup, render } from "@testing-library/react";
import React from "react";
import { AST } from "../../src/ast";
import { Language } from "../../src/CodeMirrorBlocks";
import Context from "../../src/components/Context";
import { addLanguage } from "../../src/languages";
Expand Down Expand Up @@ -35,7 +34,6 @@ xdescribe("on first render", () => {
value=""
onMount={onMount}
keyDownHelpers={{}}
passedAST={AST.from([])}
language={testLanguage}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-blocks/spec/undo-redo-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("when testing undo/redo,", () => {
};

beforeEach(async () => {
cmb = await mountCMB(wescheme);
cmb = mountCMB(wescheme).cmb;
});

afterEach(teardown);
Expand Down
Loading

0 comments on commit cae2596

Please sign in to comment.