Skip to content

Commit 6880087

Browse files
feat: tests:
1 parent c8eef83 commit 6880087

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { init } from "../index";
2+
import * as torii from "@dojoengine/torii-client";
3+
import { subscribeQuery } from "../subscribeQuery";
4+
import { getEntities } from "../getEntities";
5+
import { describe, it, expect, beforeEach, vi } from "vitest";
6+
7+
// Mock dependencies
8+
vi.mock("@dojoengine/torii-client", () => ({
9+
createClient: vi.fn(),
10+
}));
11+
vi.mock("../subscribeQuery", () => ({
12+
subscribeQuery: vi.fn(),
13+
}));
14+
vi.mock("../getEntities", () => ({
15+
getEntities: vi.fn(),
16+
}));
17+
18+
describe("init function", () => {
19+
const mockConfig: torii.ClientConfig = {
20+
rpcUrl: "mock-rpc-url",
21+
toriiUrl: "mock-torii-url",
22+
relayUrl: "mock-relay-url",
23+
worldAddress: "mock-world-address",
24+
};
25+
26+
const mockClient = {} as torii.ToriiClient;
27+
28+
beforeEach(() => {
29+
vi.clearAllMocks();
30+
(torii.createClient as any).mockResolvedValue(mockClient);
31+
});
32+
33+
it("should create a client with the provided config", async () => {
34+
await init(mockConfig);
35+
expect(torii.createClient).toHaveBeenCalledWith(mockConfig);
36+
});
37+
38+
it("should return an object with client, subscribeQuery, and getEntities", async () => {
39+
const result = await init(mockConfig);
40+
expect(result).toHaveProperty("client", mockClient);
41+
expect(result).toHaveProperty("subscribeQuery");
42+
expect(result).toHaveProperty("getEntities");
43+
});
44+
45+
it("should call subscribeQuery with correct parameters", async () => {
46+
const { subscribeQuery: subscribeFn } = await init(mockConfig);
47+
const mockQuery = { todos: { done: true } };
48+
const mockCallback = vi.fn();
49+
50+
await subscribeFn(mockQuery, mockCallback);
51+
expect(subscribeQuery).toHaveBeenCalledWith(
52+
mockClient,
53+
mockQuery,
54+
mockCallback
55+
);
56+
});
57+
58+
it("should call getEntities with correct parameters", async () => {
59+
const { getEntities: getEntitiesFn } = await init(mockConfig);
60+
const mockQuery = { todos: { done: true } };
61+
const mockCallback = vi.fn();
62+
63+
await getEntitiesFn(mockQuery, mockCallback);
64+
expect(getEntities).toHaveBeenCalledWith(
65+
mockClient,
66+
mockQuery,
67+
mockCallback
68+
);
69+
});
70+
});

packages/sdk/src/subscribeQuery.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export async function subscribeQuery<T extends SchemaType, K extends keyof T>(
88
callback: (response: { entities?: torii.Entities; error?: Error }) => void
99
): Promise<torii.Subscription> {
1010
const clauses = convertQueryToClauses(query);
11+
1112
return client.onEntityUpdated(
1213
clauses,
1314
(entities: string, data: torii.Entities) => {

0 commit comments

Comments
 (0)