Add cleanup tests

This commit is contained in:
Sam Nolan 2022-10-04 12:22:41 +11:00
parent 2be9d274f2
commit a0e3f70dbb
6 changed files with 34 additions and 12 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ yarn-error.log
.vscode .vscode
todo.txt todo.txt
result result
shell.nix

View File

@ -44,6 +44,7 @@
"@types/node": "^18.8.0", "@types/node": "^18.8.0",
"@types/react": "^18.0.21", "@types/react": "^18.0.21",
"@types/styled-components": "^5.1.26", "@types/styled-components": "^5.1.26",
"@types/uuid": "^8.3.4",
"@types/webpack": "^5.28.0", "@types/webpack": "^5.28.0",
"canvas": "^2.10.1", "canvas": "^2.10.1",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
@ -84,7 +85,8 @@
"format": "prettier --write .", "format": "prettier --write .",
"prepack": "yarn run build:cjs && yarn run bundle", "prepack": "yarn run build:cjs && yarn run bundle",
"test": "jest", "test": "jest",
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand" "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
"test:profile": "node --cpu-prof node_modules/.bin/jest --runInBand"
}, },
"eslintConfig": { "eslintConfig": {
"extends": [ "extends": [

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 { v4 as uuidv4 } from "uuid"; import * as uuid from 'uuid';
type SquiggleArgs = { type SquiggleArgs = {
code?: string; code?: string;
@ -17,7 +17,7 @@ type SquiggleArgs = {
const importSourceName = (sourceName: string) => "imports-" + sourceName; const importSourceName = (sourceName: string) => "imports-" + sourceName;
export const useSquiggle = (args: SquiggleArgs) => { export const useSquiggle = (args: SquiggleArgs) => {
const autogenName = useMemo(() => uuidv4(), []); const autogenName = useMemo(() => uuid.v4(), []);
const result = useMemo( const result = useMemo(
() => { () => {
@ -70,9 +70,9 @@ export const useSquiggle = (args: SquiggleArgs) => {
useEffect( useEffect(
() => { () => {
return () => { return () => {
if (result.needsClean) args.project.clean(result.sourceName); if (result.needsClean) args.project.removeSource(result.sourceName);
if (args.project.getSource(importSourceName(result.sourceName))) if (args.project.getSource(importSourceName(result.sourceName)))
args.project.clean(result.sourceName); args.project.removeSource(result.sourceName);
}; };
}, },
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps

View File

@ -3,11 +3,11 @@ import React from "react";
import "@testing-library/jest-dom"; import "@testing-library/jest-dom";
import { SquiggleChart } from "../src/index"; import { SquiggleChart } from "../src/index";
test("Logs no warnings or errors", async () => { test("Logs nothing on render", async () => {
debugger;
const { unmount } = render(<SquiggleChart code={"normal(0, 1)"} />); const { unmount } = render(<SquiggleChart code={"normal(0, 1)"} />);
unmount(); unmount();
expect(console.log).not.toBeCalled();
expect(console.warn).not.toBeCalled(); expect(console.warn).not.toBeCalled();
expect(console.error).not.toBeCalled(); expect(console.error).not.toBeCalled();
}); });

View File

@ -0,0 +1,19 @@
import { render } from "@testing-library/react";
import React from "react";
import "@testing-library/jest-dom";
import { SquiggleChart } from "../src/index";
import { SqProject } from "@quri/squiggle-lang";
test("Creates and cleans up source", async () => {
const project = SqProject.create();
const { unmount } = render(<SquiggleChart code={"normal(0, 1)"} project={project} />);
expect(project.getSourceIds().length).toBe(1)
const sourceId = project.getSourceIds()[0]
expect(project.getSource(sourceId)).toBe("normal(0, 1)")
unmount();
expect(project.getSourceIds().length).toBe(0)
expect(project.getSource(sourceId)).toBe(undefined)
});

View File

@ -5078,10 +5078,10 @@
"@types/history" "^4.7.11" "@types/history" "^4.7.11"
"@types/react" "*" "@types/react" "*"
"@types/react@*", "@types/react@^18.0.1", "@types/react@^18.0.21": "@types/react@*", "@types/react@17.0.43", "@types/react@^18.0.21":
version "18.0.21" version "17.0.43"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.21.tgz#b8209e9626bb00a34c76f55482697edd2b43cc67" resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.43.tgz#4adc142887dd4a2601ce730bc56c3436fdb07a55"
integrity sha512-7QUCOxvFgnD5Jk8ZKlUAhVcRj7GuJRjnjjiY/IUBWKgOlnvDvTMLD4RTF7NPyVmbRhNrbomZiOepg7M/2Kj1mA== integrity sha512-8Q+LNpdxf057brvPu1lMtC5Vn7J119xrP1aq4qiaefNioQUYANF/CYeK4NsKorSZyUGJ66g0IM+4bbjwx45o2A==
dependencies: dependencies:
"@types/prop-types" "*" "@types/prop-types" "*"
"@types/scheduler" "*" "@types/scheduler" "*"
@ -15979,7 +15979,7 @@ react-vega@^7.6.0:
prop-types "^15.8.1" prop-types "^15.8.1"
vega-embed "^6.5.1" vega-embed "^6.5.1"
react@^18.0.0, react@^18.1.0: react@^18.1.0:
version "18.2.0" version "18.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ== integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==