diff --git a/.gitignore b/.gitignore index 8f538f09..0712e779 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ yarn-error.log .vscode todo.txt result +shell.nix diff --git a/packages/components/package.json b/packages/components/package.json index cbb83043..6e0cba3f 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -44,6 +44,7 @@ "@types/node": "^18.8.0", "@types/react": "^18.0.21", "@types/styled-components": "^5.1.26", + "@types/uuid": "^8.3.4", "@types/webpack": "^5.28.0", "canvas": "^2.10.1", "cross-env": "^7.0.3", @@ -84,7 +85,8 @@ "format": "prettier --write .", "prepack": "yarn run build:cjs && yarn run bundle", "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": { "extends": [ diff --git a/packages/components/src/lib/hooks/useSquiggle.ts b/packages/components/src/lib/hooks/useSquiggle.ts index eba85eaf..c291976f 100644 --- a/packages/components/src/lib/hooks/useSquiggle.ts +++ b/packages/components/src/lib/hooks/useSquiggle.ts @@ -1,7 +1,7 @@ import { environment, SqProject, SqValue } from "@quri/squiggle-lang"; import { useEffect, useMemo } from "react"; import { JsImports, jsImportsToSquiggleCode } from "../jsImports"; -import { v4 as uuidv4 } from "uuid"; +import * as uuid from 'uuid'; type SquiggleArgs = { code?: string; @@ -17,7 +17,7 @@ type SquiggleArgs = { const importSourceName = (sourceName: string) => "imports-" + sourceName; export const useSquiggle = (args: SquiggleArgs) => { - const autogenName = useMemo(() => uuidv4(), []); + const autogenName = useMemo(() => uuid.v4(), []); const result = useMemo( () => { @@ -70,9 +70,9 @@ export const useSquiggle = (args: SquiggleArgs) => { useEffect( () => { return () => { - if (result.needsClean) args.project.clean(result.sourceName); + if (result.needsClean) args.project.removeSource(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 diff --git a/packages/components/test/basic.test.tsx b/packages/components/test/basic.test.tsx index 9eb4973a..e77f104b 100644 --- a/packages/components/test/basic.test.tsx +++ b/packages/components/test/basic.test.tsx @@ -3,11 +3,11 @@ import React from "react"; import "@testing-library/jest-dom"; import { SquiggleChart } from "../src/index"; -test("Logs no warnings or errors", async () => { - debugger; +test("Logs nothing on render", async () => { const { unmount } = render(); unmount(); + expect(console.log).not.toBeCalled(); expect(console.warn).not.toBeCalled(); expect(console.error).not.toBeCalled(); }); diff --git a/packages/components/test/cleanup.test.tsx b/packages/components/test/cleanup.test.tsx new file mode 100644 index 00000000..081bfe4f --- /dev/null +++ b/packages/components/test/cleanup.test.tsx @@ -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(); + 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) +}); diff --git a/yarn.lock b/yarn.lock index 2292e288..f8636023 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5078,10 +5078,10 @@ "@types/history" "^4.7.11" "@types/react" "*" -"@types/react@*", "@types/react@^18.0.1", "@types/react@^18.0.21": - version "18.0.21" - resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.21.tgz#b8209e9626bb00a34c76f55482697edd2b43cc67" - integrity sha512-7QUCOxvFgnD5Jk8ZKlUAhVcRj7GuJRjnjjiY/IUBWKgOlnvDvTMLD4RTF7NPyVmbRhNrbomZiOepg7M/2Kj1mA== +"@types/react@*", "@types/react@17.0.43", "@types/react@^18.0.21": + version "17.0.43" + resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.43.tgz#4adc142887dd4a2601ce730bc56c3436fdb07a55" + integrity sha512-8Q+LNpdxf057brvPu1lMtC5Vn7J119xrP1aq4qiaefNioQUYANF/CYeK4NsKorSZyUGJ66g0IM+4bbjwx45o2A== dependencies: "@types/prop-types" "*" "@types/scheduler" "*" @@ -15979,7 +15979,7 @@ react-vega@^7.6.0: prop-types "^15.8.1" vega-embed "^6.5.1" -react@^18.0.0, react@^18.1.0: +react@^18.1.0: version "18.2.0" resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==