-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configured testing environment (#20)
* Configured vitest to work with snarkyjs and worker * Added NPM license * Defined basic tests, improved worker for tests
- Loading branch information
1 parent
e767089
commit ff3d316
Showing
10 changed files
with
365 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import matchers from "@testing-library/jest-dom/matchers"; | ||
import { expect, vi } from "vitest"; | ||
import createFetchMock from "vitest-fetch-mock"; | ||
import "vitest-canvas-mock"; | ||
|
||
expect.extend(matchers); | ||
|
||
const fetchMocker = createFetchMock(vi); | ||
|
||
// sets globalThis.fetch and globalThis.fetchMock to our mocked version | ||
fetchMocker.enableMocks(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from "./store"; | ||
export * from "./hooks"; | ||
export { initZKWorker } from "./zkAppWorker"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,71 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { it } from "vitest"; | ||
import "@vitest/web-worker"; | ||
import { render, waitFor } from "@testing-library/react"; | ||
import type { JsonProof } from "snarkyjs"; | ||
import { describe, expect, it } from "vitest"; | ||
import { createZKState } from "zk-states"; | ||
|
||
// TODO: fix wasm error when importing zk-states library | ||
it("initializes state in a react component", async () => { | ||
const Component = () => { | ||
return <>hello world!</>; | ||
}; | ||
interface ZKState { | ||
num: number; | ||
incNum: () => void; | ||
} | ||
|
||
const { findByText } = render(<Component />); | ||
const { | ||
useInitZKStore, | ||
useGetLatestProof, | ||
useZKStore, | ||
useProof, | ||
useIsInitialized, | ||
} = createZKState<ZKState>( | ||
new Worker(new URL("./worker.ts", import.meta.url), { | ||
type: "module", | ||
}), | ||
(set) => ({ | ||
num: 0, | ||
incNum: () => set((state) => ({ num: state.num + 1 })), | ||
}), | ||
); | ||
|
||
await findByText("hello world!"); | ||
describe("createZKState", () => { | ||
it("returns the expected hooks", () => { | ||
expect(useInitZKStore).toBeDefined(); | ||
expect(useGetLatestProof).toBeDefined(); | ||
expect(useProof).toBeDefined(); | ||
expect(useIsInitialized).toBeDefined(); | ||
}); | ||
|
||
it("renders the correct initial global state value in a React component", async () => { | ||
const expectedValue = 0; | ||
|
||
const Component = () => { | ||
const num = useZKStore((state) => state.num); | ||
return <div>num: {num}</div>; | ||
}; | ||
|
||
const { findByText } = render(<Component />); | ||
|
||
await findByText(`num: ${expectedValue}`); | ||
}); | ||
|
||
it("correctly initializes ZK store with useInitZKStore", async () => { | ||
let proof: JsonProof | undefined; | ||
|
||
const Component = () => { | ||
const isInitialized = useIsInitialized(); | ||
proof = useProof(); | ||
useInitZKStore(); | ||
return <div>isInitialized: {isInitialized ? "true" : "false"}</div>; | ||
}; | ||
|
||
const { getByText } = render(<Component />); | ||
|
||
await waitFor( | ||
() => { | ||
expect(getByText("isInitialized: true")).toBeInTheDocument(); | ||
}, | ||
{ timeout: 300000 }, | ||
); | ||
|
||
expect(proof).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["./**/*"] | ||
"include": ["./**/*"], | ||
"compilerOptions": { | ||
"types": ["@testing-library/jest-dom"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { initZKWorker } from "zk-states"; | ||
|
||
initZKWorker(self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.