Add tests for different types of cleanup

This commit is contained in:
Sam Nolan 2022-10-04 13:38:15 +11:00
parent a0e3f70dbb
commit b412eee646
2 changed files with 50 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { environment, SqProject, SqValue } from "@quri/squiggle-lang"; import { environment, SqProject, SqValue } from "@quri/squiggle-lang";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
import { JsImports, jsImportsToSquiggleCode } from "../jsImports"; import { JsImports, jsImportsToSquiggleCode } from "../jsImports";
import * as uuid from 'uuid'; import * as uuid from "uuid";
type SquiggleArgs = { type SquiggleArgs = {
code?: string; code?: string;

View File

@ -4,16 +4,58 @@ import "@testing-library/jest-dom";
import { SquiggleChart } from "../src/index"; import { SquiggleChart } from "../src/index";
import { SqProject } from "@quri/squiggle-lang"; import { SqProject } from "@quri/squiggle-lang";
test("Creates and cleans up source", async () => { test("Creates and cleans up source with no name", async () => {
const project = SqProject.create(); const project = SqProject.create();
const { unmount } = render(<SquiggleChart code={"normal(0, 1)"} project={project} />); const { unmount } = render(
expect(project.getSourceIds().length).toBe(1) <SquiggleChart code={"normal(0, 1)"} project={project} />
);
expect(project.getSourceIds().length).toBe(1);
const sourceId = project.getSourceIds()[0] const sourceId = project.getSourceIds()[0];
expect(project.getSource(sourceId)).toBe("normal(0, 1)") expect(project.getSource(sourceId)).toBe("normal(0, 1)");
unmount(); unmount();
expect(project.getSourceIds().length).toBe(0) expect(project.getSourceIds().length).toBe(0);
expect(project.getSource(sourceId)).toBe(undefined) expect(project.getSource(sourceId)).toBe(undefined);
});
test("Does not clean up existing source", async () => {
const project = SqProject.create();
project.setSource("main", "normal(0, 1)");
const { unmount } = render(
<SquiggleChart sourceName={"main"} project={project} />
);
expect(project.getSourceIds()).toStrictEqual(["main"]);
const sourceId = project.getSourceIds()[0];
expect(project.getSource(sourceId)).toBe("normal(0, 1)");
unmount();
expect(project.getSourceIds()).toStrictEqual(["main"]);
expect(project.getSource(sourceId)).toBe("normal(0, 1)");
});
test("Does clean up when given non-existant source", async () => {
const project = SqProject.create();
const { unmount } = render(
<SquiggleChart
code={"normal(0, 1)"}
sourceName={"main"}
project={project}
/>
);
expect(project.getSourceIds()).toStrictEqual(["main"]);
const sourceId = project.getSourceIds()[0];
expect(project.getSource(sourceId)).toBe("normal(0, 1)");
unmount();
expect(project.getSourceIds()).toStrictEqual([]);
expect(project.getSource(sourceId)).toBe(undefined);
}); });