|
| 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. |
0 commit comments