Skip to content

Commit 9bcecb6

Browse files
docs: add testing docs to dapp client
1 parent 198a418 commit 9bcecb6

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

docs/docs/sdks/dapp/javascript/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ This SDK is used to integrate 1ClickConnect into your Javascript browser applica
1111
- [Installation](./installation)
1212
- [Clients](./clients)
1313
- [Errors](./errors)
14+
- [Testing](./testing)
1415
- [Report an issue](./report_an_issue)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# Testing
6+
7+
To enhance the integration experience, this package contains mocks of the [`Client`](./clients#client) and [`RelayerClient`](./clients#relayerclient) to test the integration with a dapp. This package offers 2 different types of mocks:
8+
9+
- `Mock`: A mock to simulate certain behavior of a class instance.
10+
- `GlobalMock`: A global mock to simulate certain behavior of a class across the entire application.
11+
12+
Each client has its own pair of mocks, so you can mock the behavior of the client in your tests as you desire.
13+
14+
## Framework compatibility
15+
16+
This package is compatible with the following frameworks:
17+
18+
| Framework | Status |
19+
| --------- | ------ |
20+
| Jest ||
21+
22+
## Mocks
23+
24+
### Client
25+
26+
#### ClientMock
27+
28+
The `ClientMock` is a specific mock for mocking an instance of [`Client`](./clients#client). It is ideal for use in unit tests where the [`Client`](./clients#client) is injected as a dependency.
29+
30+
To use it, you can import the mock from the package and use it in your test:
31+
32+
```typescript
33+
import { ClientMock } from "@one-click-connect/browser-dapp/mocks";
34+
35+
const mock = new ClientMock();
36+
```
37+
38+
You can modify the mock behavior by overriding the returned values of the methods. For example, if you want to mock the `isSignedIn` method to return a specific boolean value, you can do the following:
39+
40+
```typescript
41+
mock.isSignedIn.mockReturnValue(true);
42+
```
43+
44+
or throwing an error:
45+
46+
```typescript
47+
mock.isSignedIn.mockRejectedValue(new Error("Error signing in"));
48+
```
49+
50+
#### ClientGlobalMock
51+
52+
The `ClientGlobalMock` is a global mock of the `Client` that simulates its behavior across the entire application. It is ideal for testing where you need to test the complete dapp integration flow. Like the `ClientMock`, this mock allows you to modify the dapp behavior throughout the application.
53+
54+
### RelayerClient
55+
56+
#### RelayerClientMock
57+
58+
The `RelayerClientMock` is a specific mock for mocking an instance of [`RelayerClient`](./clients#relayerclient). It is ideal for use in unit tests where the [`RelayerClient`](./clients#relayerclient) is injected as a dependency.
59+
60+
To use it, you can import the mock from the package and use it in your test:
61+
62+
```typescript
63+
import { RelayerClientMock } from "@one-click-connect/browser-dapp/mocks";
64+
65+
const mock = new RelayerClientMock();
66+
```
67+
68+
You can modify the mock behavior by overriding the returned values of the methods. For example, if you want to mock the `isSignedIn` method to return a specific boolean value, you can do the following:
69+
70+
```typescript
71+
mock.isSignedIn.mockReturnValue(true);
72+
```
73+
74+
or throwing an error:
75+
76+
```typescript
77+
mock.isSignedIn.mockRejectedValue(new Error("Error signing in"));
78+
```
79+
80+
#### RelayerClientGlobalMock
81+
82+
The `RelayerClientGlobalMock` is a global mock of the `RelayerClient` that simulates its behavior across the entire application. It is ideal for testing where you need to test the complete dapp integration flow. Like the `RelayerClientMock`, this mock allows you to modify the dapp behavior throughout the application.

docs/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const sidebars: SidebarsConfig = {
2222
"sdks/dapp/javascript/installation",
2323
"sdks/dapp/javascript/clients",
2424
"sdks/dapp/javascript/errors",
25+
"sdks/dapp/javascript/testing",
2526
"sdks/dapp/javascript/report_an_issue",
2627
],
2728
};

0 commit comments

Comments
 (0)