diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 93a45731..e1c13170 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,6 +38,9 @@ jobs: - name: Build dependencies run: bun run build && bun run build-examples + - name: Run tests + run: bun vitest + # - name: Upload coverage reports to Codecov # uses: codecov/codecov-action@v3 # env: diff --git a/bun.lockb b/bun.lockb index 81d08cf9..1fed86bf 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/examples/node/torii-bot/tsconfig.json b/examples/node/torii-bot/tsconfig.json index 448ce88a..b3e05b02 100644 --- a/examples/node/torii-bot/tsconfig.json +++ b/examples/node/torii-bot/tsconfig.json @@ -16,12 +16,12 @@ "rootDir": "/Users/os/Documents/code/dojo.js/examples", "allowJs": true, "outDir": "dist", - "types": ["bun-types"], + "types": ["bun-types"] }, "include": [ "./src/**/*", "dojoConfig.ts", - "../../dojo-starter/target/dev/manifest.json", + "../../dojo-starter/target/dev/manifest.json" ], - "exclude": ["examples/dojo-starter/target/dev/manifest.json"], + "exclude": ["examples/dojo-starter/target/dev/manifest.json"] } diff --git a/examples/react/react-app/tsconfig.json b/examples/react/react-app/tsconfig.json index 54fa5cd9..7f1be29a 100644 --- a/examples/react/react-app/tsconfig.json +++ b/examples/react/react-app/tsconfig.json @@ -15,12 +15,12 @@ // "noUnusedLocals": true, // "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "allowSyntheticDefaultImports": true, + "allowSyntheticDefaultImports": true }, "include": ["src", "dojoConfig.ts"], "references": [ { - "path": "./tsconfig.node.json", - }, - ], + "path": "./tsconfig.node.json" + } + ] } diff --git a/examples/react/react-phaser-example/tsconfig.json b/examples/react/react-phaser-example/tsconfig.json index 59d60959..db13e438 100644 --- a/examples/react/react-phaser-example/tsconfig.json +++ b/examples/react/react-phaser-example/tsconfig.json @@ -17,16 +17,16 @@ // "noUnusedLocals": true, // "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "allowSyntheticDefaultImports": true, + "allowSyntheticDefaultImports": true }, "include": ["src"], "baseUrl": ".", "paths": { - "@/*": ["./src/*"], + "@/*": ["./src/*"] }, "references": [ { - "path": "./tsconfig.node.json", - }, - ], + "path": "./tsconfig.node.json" + } + ] } diff --git a/examples/react/react-threejs/tsconfig.json b/examples/react/react-threejs/tsconfig.json index ed753b1e..cfc2ec48 100644 --- a/examples/react/react-threejs/tsconfig.json +++ b/examples/react/react-threejs/tsconfig.json @@ -18,13 +18,13 @@ "allowSyntheticDefaultImports": true, "baseUrl": ".", "paths": { - "@/*": ["./src/*"], - }, + "@/*": ["./src/*"] + } }, "include": ["src", "dojoConfig.ts"], "references": [ { - "path": "./tsconfig.node.json", - }, - ], + "path": "./tsconfig.node.json" + } + ] } diff --git a/package.json b/package.json index 4afcd00d..10d4ab09 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,8 @@ "build-watch": "bun run build-core --watch & bun run build-create-burner --watch & bun run build-create-dojo --watch & bun run build-utils --watch & bun run build-react --watch & bun run build-torii-wasm --watch & bun run build-torii-client --watch & bun run build-state --watch", "build": "bun run build-torii-wasm && bun run build-utils-wasm && bun run build-torii-client && bun run build-state && bun run build-core && bun run build-create-burner && bun run build-create-dojo && bun run build-utils && bun run build-react", "build-examples": "bun run build-phaser && bun run build-react-app", + "test-create-burner": "bun --cwd packages/create-burner vitest", + "test-core": "bun --cwd packages/core vitest", "clean": "rm -rf node_modules packages/create-burner/node_modules node_modules packages/create-dojo/node_modules packages/core/node_modules packages/utils/node_modules packages/torii-client/node_modules packages/torii-wasm/node_modules packages/react/node_modules bun.lockb packages/create-burner/bun.lockb packages/core/bun.lockb packages/utils/bun.lockb packages/torii-client/bun.lockb packages/torii-wasm/bun.lockb packages/react/bun.lockb && rm -rf examples/react/react-app/node_modules examples/react/react-app/bun.lockb examples/react/react-phaser-example/node_modules examples/react/react-phaser-example/bun.lockb", "prettier-check": "bun x prettier --check .", "prettier": "bun x prettier --write .", diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 4d63fc7f..052ff43e 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -14,9 +14,9 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["src/**/*.ts", "bin/generateComponents.cjs"], "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/create-burner/package.json b/packages/create-burner/package.json index a4f89837..884680d1 100644 --- a/packages/create-burner/package.json +++ b/packages/create-burner/package.json @@ -9,7 +9,7 @@ "type": "module", "scripts": { "build": "tsup --dts-resolve", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "vitest" }, "peerDependencies": { "react": "^18.2.0", @@ -24,14 +24,18 @@ "devDependencies": { "@babel/core": "^7.21.4", "@babel/preset-env": "^7.21.4", + "@testing-library/react": "^14.1.2", + "@testing-library/react-hooks": "^8.0.1", "@types/js-cookie": "^3.0.3", "@types/node": "^18.15.11", "@types/react": "^18.2.33", "@types/react-dom": "^18.0.11", "@types/web": "^0.0.114", "bun-types": "^1.0.1", + "jsdom": "^24.0.0", "tsup": "^8.0.1", - "typescript": "^5.0.3" + "typescript": "^5.0.3", + "vitest": "^1.2.2" }, "dependencies": { "@dojoengine/core": "^0.5.6", diff --git a/packages/create-burner/test/connectors/burner.test.ts b/packages/create-burner/test/connectors/burner.test.ts new file mode 100644 index 00000000..49e581ad --- /dev/null +++ b/packages/create-burner/test/connectors/burner.test.ts @@ -0,0 +1,69 @@ +import { describe, it, expect } from "vitest"; +import { BurnerConnector } from "../../src/connectors/burner"; +import { getBurnerConnector } from "../mocks/mocks"; +import { KATANA_PREFUNDED_ADDRESS } from "@dojoengine/core"; + +describe("BurnerConnector", () => { + const burnerObj = new BurnerConnector({}, null); + + it("should test available method", async () => { + expect(burnerObj.available()).toBe(true); + }), + it("should test ready method", async () => { + expect(await burnerObj.ready()).toBe(true); + expect(burnerObj.ready()).toBeTypeOf("object"); + }), + it("should test connect method", async () => { + expect(() => burnerObj.connect()).rejects.toThrowError( + "account not found" + ); + }), + it("should test disconnect method", async () => { + expect(await burnerObj.disconnect()).toBeUndefined(); + }), + it("should test account method", async () => { + expect(await burnerObj.account()).toBeNull(); + }), + it("should test id method", async () => { + expect(burnerObj.id).toBe("Burner Account"); + }), + it("should test name method", async () => { + expect(burnerObj.name).toBe("Burner Connector"); + expect(burnerObj.name).toBeTypeOf("string"); + }), + it("should test icon method", async () => { + expect(burnerObj.icon).toBe("my-icon-url"); + }); +}); + +describe("BurnerConnector2", () => { + const burnerObj = getBurnerConnector(); + + it("should test available method", async () => { + expect(burnerObj.available()).toBe(true); + }), + it("should test ready method", async () => { + expect(await burnerObj.ready()).toBe(true); + expect(burnerObj.ready()).toBeTypeOf("object"); + }), + it("should test connect method", async () => { + expect(await burnerObj.connect()).toBeTypeOf("object"); + }), + it("should test disconnect method", async () => { + expect(await burnerObj.disconnect()).toBeUndefined(); + }), + it("should test account method", async () => { + expect(await burnerObj.account()).not.toBeNull(); + }), + it("should test id method", async () => { + console.log(burnerObj.id); + expect(burnerObj.id).toEqual(KATANA_PREFUNDED_ADDRESS); + }), + it("should test name method", async () => { + expect(burnerObj.name).toBe("Burner Connector"); + expect(burnerObj.name).toBeTypeOf("string"); + }), + it("should test icon method", async () => { + expect(burnerObj.icon).toBe("my-icon-url"); + }); +}); diff --git a/packages/create-burner/test/hooks/useBurner.test.ts b/packages/create-burner/test/hooks/useBurner.test.ts new file mode 100644 index 00000000..0f6abafe --- /dev/null +++ b/packages/create-burner/test/hooks/useBurner.test.ts @@ -0,0 +1,10 @@ +//@vitest-environment jsdom +import { describe, it, expect } from "vitest"; +import { renderHook } from "@testing-library/react-hooks"; +import { useBurner } from "../../src/hooks/useBurner"; + +describe("useBurner", () => { + it("testing", () => { + const { result } = renderHook(() => useBurner()); + }); +}); diff --git a/packages/create-burner/test/manager/burnerManager.test.ts b/packages/create-burner/test/manager/burnerManager.test.ts new file mode 100644 index 00000000..ecb56fe1 --- /dev/null +++ b/packages/create-burner/test/manager/burnerManager.test.ts @@ -0,0 +1,40 @@ +import { describe, it, afterEach, expect, vi } from "vitest"; +import { getBurnerManager } from "../mocks/mocks"; + +describe("BurnerManager", () => { + const burnerManager = getBurnerManager(); + + it("should init burner account", async () => { + burnerManager.init(); + }), + it("should update isDeploying", async () => { + burnerManager.updateIsDeploying(false); + expect(burnerManager.isDeploying).toBeFalsy(); + }), + it("should list burner accounts", async () => { + expect(burnerManager.list()).toStrictEqual([]); + }), + it("should select burner accounts", async () => { + expect(() => burnerManager.select("test")).toThrowError( + "burner not found" + ); + }), + it("should get burner accounts", async () => { + expect(() => burnerManager.get("test")).toThrowError( + "burner not found" + ); + }), + it("should create burner accounts", async () => { + expect(burnerManager.create()).rejects.toThrowError(); + }), + it("should copy burner to clipboard", async () => { + expect( + burnerManager.copyBurnersToClipboard() + ).rejects.toThrowError(); + }), + it("should set burner from clipboard", async () => { + expect( + burnerManager.setBurnersFromClipboard() + ).rejects.toThrowError(); + }); +}); diff --git a/packages/create-burner/test/mocks/mocks.ts b/packages/create-burner/test/mocks/mocks.ts new file mode 100644 index 00000000..5bfb7d99 --- /dev/null +++ b/packages/create-burner/test/mocks/mocks.ts @@ -0,0 +1,34 @@ +import { BurnerManager } from "../../src/manager/burnerManager"; +import { Account, RpcProvider } from "starknet"; +import { BurnerConnector } from "../../src/connectors/burner"; +import { + KATANA_PREFUNDED_ADDRESS, + KATANA_PREFUNDED_PRIVATE_KEY, + LOCAL_KATANA, +} from "@dojoengine/core"; + +export const getBurnerManager = (): BurnerManager => { + const burnerManager = new BurnerManager({ + masterAccount: new Account( + new RpcProvider({ nodeUrl: LOCAL_KATANA }), + KATANA_PREFUNDED_ADDRESS, + KATANA_PREFUNDED_PRIVATE_KEY + ), + accountClassHash: KATANA_PREFUNDED_PRIVATE_KEY, + rpcProvider: new RpcProvider({ nodeUrl: LOCAL_KATANA }), + }); + return burnerManager; +}; + +export const getBurnerConnector = (): BurnerConnector => { + const publicKey = KATANA_PREFUNDED_ADDRESS; + const burnerObj = new BurnerConnector( + {}, + new Account( + new RpcProvider({ nodeUrl: LOCAL_KATANA }), + publicKey, + KATANA_PREFUNDED_PRIVATE_KEY + ) + ); + return burnerObj; +}; diff --git a/packages/create-burner/test/utils/storage.test.ts b/packages/create-burner/test/utils/storage.test.ts new file mode 100644 index 00000000..32ef5c3f --- /dev/null +++ b/packages/create-burner/test/utils/storage.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi } from "vitest"; +import Storage from "../../src/utils/storage"; +import Cookies from "js-cookie"; +import { BurnerStorage } from "../../src/types"; +import { + KATANA_PREFUNDED_ADDRESS, + KATANA_PREFUNDED_PRIVATE_KEY, + KATANA_CLASS_HASH, + KATANA_ETH_CONTRACT_ADDRESS, +} from "@dojoengine/core"; + +vi.mock("js-cookie"); + +describe("storage", () => { + it("should return null", async () => { + Cookies.get = vi.fn().mockReturnValue({}); + expect(Storage.keys()).toStrictEqual([]); + }), + it("should return a json", async () => { + const storageObj: BurnerStorage = { + KATANA_ETH_CONTRACT_ADDRESS: { + privateKey: KATANA_PREFUNDED_PRIVATE_KEY, + publicKey: KATANA_PREFUNDED_ADDRESS, + deployTx: KATANA_CLASS_HASH, + active: true, + }, + }; + Cookies.get = vi.fn().mockReturnValue(JSON.stringify(storageObj)); + expect(Storage.get("test")).toStrictEqual(storageObj); + }), + it("should set successfully", async () => { + Storage.set("test", 10); + }), + it("should remove key", async () => { + Storage.remove("test"); + }), + it("should clear all", async () => { + Storage.clear(); + }); +}); diff --git a/packages/create-burner/tsconfig.json b/packages/create-burner/tsconfig.json index fdf219bd..421b7c7f 100644 --- a/packages/create-burner/tsconfig.json +++ b/packages/create-burner/tsconfig.json @@ -22,9 +22,9 @@ "baseUrl": ".", "rootDir": "src", "paths": { - "~/*": ["src/*"], - }, + "~/*": ["src/*"] + } }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "include": ["src/**/*.ts", "test/**/*.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/react/tsconfig.json b/packages/react/tsconfig.json index b0b2f811..339c445b 100644 --- a/packages/react/tsconfig.json +++ b/packages/react/tsconfig.json @@ -22,9 +22,9 @@ "baseUrl": ".", "rootDir": "src", "paths": { - "~/*": ["src/*"], - }, + "~/*": ["src/*"] + } }, "include": ["src/**/*.ts"], - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/state/tsconfig.json b/packages/state/tsconfig.json index 4593aef8..033de84d 100644 --- a/packages/state/tsconfig.json +++ b/packages/state/tsconfig.json @@ -14,9 +14,9 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["src/**/*.ts"], "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/torii-client/tsconfig.json b/packages/torii-client/tsconfig.json index bd35e237..bd6b7959 100644 --- a/packages/torii-client/tsconfig.json +++ b/packages/torii-client/tsconfig.json @@ -12,9 +12,9 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["src/**/*.ts"], "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/torii-wasm/tsconfig.json b/packages/torii-wasm/tsconfig.json index b9d5fe63..8213ea66 100644 --- a/packages/torii-wasm/tsconfig.json +++ b/packages/torii-wasm/tsconfig.json @@ -12,8 +12,8 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/utils-wasm/tsconfig.json b/packages/utils-wasm/tsconfig.json index b9d5fe63..8213ea66 100644 --- a/packages/utils-wasm/tsconfig.json +++ b/packages/utils-wasm/tsconfig.json @@ -12,8 +12,8 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json index 4593aef8..033de84d 100644 --- a/packages/utils/tsconfig.json +++ b/packages/utils/tsconfig.json @@ -14,9 +14,9 @@ "strict": true, "strictNullChecks": true, "types": ["bun-types"], - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["src/**/*.ts"], "skipLibCheck": true, - "exclude": ["node_modules", "dist", "**/*.test.ts"], + "exclude": ["node_modules", "dist", "**/*.test.ts"] } diff --git a/tsconfig.json b/tsconfig.json index 2bae4aa3..d1adaefa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,13 +23,13 @@ "skipLibCheck": true /* Skip type checking all .d.ts files. */, "resolveJsonModule": true, "allowImportingTsExtensions": true, - "noImplicitAny": false, + "noImplicitAny": false }, "include": ["./packages"], "exclude": [ "node_modules", "./node_modules", "./node_modules/*", - "./node_modules/@types/node/index.d.ts", - ], + "./node_modules/@types/node/index.d.ts" + ] }