Skip to content

Commit f0e1be6

Browse files
committed
fix tests
1 parent 237d9d5 commit f0e1be6

File tree

2 files changed

+85
-68
lines changed

2 files changed

+85
-68
lines changed

packages/near-social/src/__tests__/index.test.ts

Lines changed: 85 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import NearSocialPlugin from "../index";
3-
4-
// Mock the Social class
5-
vi.mock("@builddao/near-social-js", () => {
6-
return {
7-
Social: vi.fn().mockImplementation(() => {
8-
return {
9-
get: vi.fn().mockResolvedValue({ "test.near": { profile: { name: "Test User" } } }),
10-
set: vi.fn().mockResolvedValue({
11-
actions: [{ type: "FunctionCall", params: { method_name: "set", args: {} } }]
12-
})
13-
};
14-
}),
15-
transformActions: vi.fn().mockReturnValue([
16-
{ type: "FunctionCall", params: { method_name: "set", args: {} } }
17-
])
18-
};
19-
});
3+
import * as nearAPI from "near-api-js";
204

215
describe("NearSocialPlugin", () => {
226
let plugin: NearSocialPlugin;
@@ -26,85 +10,123 @@ describe("NearSocialPlugin", () => {
2610
// Spy on console methods
2711
vi.spyOn(console, "log").mockImplementation(() => {});
2812
vi.spyOn(console, "error").mockImplementation(() => {});
13+
14+
// Reset mocks
15+
vi.clearAllMocks();
2916
});
3017

3118
it("should initialize with valid config", async () => {
32-
await expect(
33-
plugin.initialize({
19+
// Create a mock implementation for the Near constructor
20+
// Mock the methods using spyOn with more complete implementations
21+
const fromStringSpy = vi
22+
.spyOn(nearAPI.KeyPair, "fromString")
23+
.mockReturnValue({
24+
sign: vi.fn(),
25+
verify: vi.fn(),
26+
getPublicKey: vi
27+
.fn()
28+
.mockReturnValue({ toString: () => "test-public-key" }),
29+
} as any);
30+
31+
try {
32+
await plugin.initialize({
3433
accountId: "test.near",
3534
privateKey: "ed25519:privatekey",
36-
networkId: "testnet"
37-
})
38-
).resolves.not.toThrow();
35+
networkId: "testnet",
36+
});
37+
38+
// Verify that the plugin was initialized with the correct values
39+
expect(plugin["accountId"]).toBe("test.near");
40+
expect(plugin["privateKey"]).toBe("ed25519:privatekey");
41+
expect(plugin["networkId"]).toBe("testnet");
42+
} finally {
43+
// Restore the original methods
44+
fromStringSpy.mockRestore();
45+
}
3946
});
4047

4148
it("should throw error when initializing without config", async () => {
4249
await expect(plugin.initialize()).rejects.toThrow(
43-
"NEAR Social plugin requires configuration."
50+
"NEAR Social plugin requires configuration.",
4451
);
4552
});
4653

4754
it("should throw error when initializing without accountId", async () => {
4855
await expect(
4956
plugin.initialize({
50-
privateKey: "ed25519:privatekey"
51-
} as any)
57+
privateKey: "ed25519:privatekey",
58+
} as any),
5259
).rejects.toThrow("NEAR Social plugin requires accountId");
5360
});
5461

5562
it("should throw error when initializing without privateKey", async () => {
5663
await expect(
5764
plugin.initialize({
58-
accountId: "test.near"
59-
} as any)
65+
accountId: "test.near",
66+
} as any),
6067
).rejects.toThrow("NEAR Social plugin requires privateKey");
6168
});
6269

6370
it("should distribute content successfully", async () => {
64-
// Initialize the plugin
65-
await plugin.initialize({
66-
accountId: "test.near",
67-
privateKey: "ed25519:privatekey",
68-
networkId: "testnet"
69-
});
70-
71-
// Mock the signAndSendTransaction method
72-
const signAndSendTransactionSpy = vi.spyOn(
73-
plugin as any,
74-
"signAndSendTransaction"
75-
).mockResolvedValue(undefined);
76-
77-
// Distribute content
78-
await expect(
79-
plugin.distribute({
71+
// Create a mock implementation for the Near constructor
72+
const mockNear = {
73+
connection: {},
74+
};
75+
76+
// Mock the methods using spyOn with more complete implementations
77+
const fromStringSpy = vi
78+
.spyOn(nearAPI.KeyPair, "fromString")
79+
.mockReturnValue({
80+
sign: vi.fn(),
81+
verify: vi.fn(),
82+
getPublicKey: vi
83+
.fn()
84+
.mockReturnValue({ toString: () => "test-public-key" }),
85+
} as any);
86+
87+
const nearSpy = vi
88+
.spyOn(nearAPI, "Near")
89+
.mockImplementation(() => mockNear as any);
90+
91+
try {
92+
// Initialize the plugin
93+
await plugin.initialize({
94+
accountId: "test.near",
95+
privateKey: "ed25519:privatekey",
96+
networkId: "testnet",
97+
});
98+
99+
// Mock the callMethod method
100+
const callMethodSpy = vi
101+
.spyOn(plugin as any, "callMethod")
102+
.mockResolvedValue(undefined);
103+
104+
// Distribute content
105+
await plugin.distribute({
80106
input: "Hello, NEAR Social!",
81107
config: {
82108
accountId: "test.near",
83109
privateKey: "ed25519:privatekey",
84-
networkId: "testnet"
85-
}
86-
})
87-
).resolves.not.toThrow();
88-
89-
// Verify signAndSendTransaction was called
90-
expect(signAndSendTransactionSpy).toHaveBeenCalled();
91-
92-
// Verify console.log was called with success message
93-
expect(console.log).toHaveBeenCalledWith("Successfully posted to NEAR Social");
110+
networkId: "testnet",
111+
},
112+
});
113+
114+
// Verify callMethod was called
115+
expect(callMethodSpy).toHaveBeenCalled();
116+
117+
// Verify console.log was called with success message
118+
expect(console.log).toHaveBeenCalledWith(
119+
"Successfully posted to NEAR Social",
120+
);
121+
} finally {
122+
// Restore the original methods
123+
fromStringSpy.mockRestore();
124+
nearSpy.mockRestore();
125+
}
94126
});
95127

96128
it("should throw error when distributing without initialization", async () => {
97129
await expect(
98-
plugin.distribute({
99-
input: "Hello, NEAR Social!",
100-
config: {
101-
accountId: "test.near",
102-
privateKey: "ed25519:privatekey"
103-
}
104-
})
105-
).rejects.toThrow("NEAR Social plugin not initialized");
106-
});
107-
});
108130
plugin.distribute({
109131
input: "Hello, NEAR Social!",
110132
config: {

packages/near-social/src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ export default class NearSocialPlugin
125125
}
126126
}
127127

128-
/**
129-
* Makes a call to a contract
130-
* @param options - the options for the call
131-
* @returns - the resulting transaction
132-
*/
133128
/**
134129
* Helper method to get an Account instance
135130
* @returns Account instance

0 commit comments

Comments
 (0)