Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts-sdk] generate TS type guards (replace superstruct) #1509

Merged
merged 17 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ts-sdk] Add unit tests for JSON-RPC Client
  • Loading branch information
666lcz committed Apr 20, 2022
commit b671e70ece84dbd53bccedd56c5b2941ccb0d523
2 changes: 2 additions & 0 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@size-limit/preset-small-lib": "^7.0.8",
"@types/bn.js": "^5.1.0",
"husky": "^7.0.4",
"mockttp": "^2.7.0",
"size-limit": "^7.0.8",
"tsdx": "^0.14.1",
"tslib": "^2.3.1",
Expand All @@ -56,6 +57,7 @@
"dependencies": {
"bn.js": "^5.2.0",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"jayson": "^3.6.6",
"superstruct": "^0.15.4",
"tweetnacl": "^1.0.3",
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
any,
is,
} from 'superstruct';
import fetch from 'cross-fetch';

/**
* An object defining headers to be passed to the RPC server
Expand Down
39 changes: 39 additions & 0 deletions sdk/typescript/test/mocks/rpc-http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import * as mockttp from 'mockttp';

export const mockServer: mockttp.Mockttp = mockttp.getLocal();

export const MOCK_PORT = 9999;
export const MOCK_URL = 'http://127.0.0.1';
export const MOCK_ENDPOINT = `${MOCK_URL}:${MOCK_PORT}/`;

export const mockRpcResponse = async ({
method,
params,
value,
error,
}: {
method: string;
params: Array<any>;
value?: any;
error?: any;
}) => {
await mockServer
.forPost('/')
.withJsonBodyIncluding({
jsonrpc: '2.0',
method,
params,
})
.thenReply(
200,
JSON.stringify({
jsonrpc: '2.0',
id: '',
error,
result: value,
})
);
};
50 changes: 50 additions & 0 deletions sdk/typescript/test/rpc/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { type as pick, array } from 'superstruct';
import { JsonRpcClient } from '../../src/rpc/client';
import { ObjectRef } from '../../src';
import {
mockRpcResponse,
mockServer,
MOCK_ENDPOINT,
MOCK_PORT,
} from '../mocks/rpc-http';

const EXAMPLE_OBJECT = {
objectId: '8dc6a6f70564e29a01c7293a9c03818fda2d049f',
version: 0,
digest: 'CI8Sf+t3Xrt5h9ENlmyR8bbMVfg6df3vSDc08Gbk9/g=',
};

describe('JSON-RPC Client', () => {
const server = mockServer;
let client: JsonRpcClient;

beforeEach(() => {
server.start(MOCK_PORT);
client = new JsonRpcClient(MOCK_ENDPOINT);
});

afterEach(() => {
server.stop();
});

it('requestWithType', async () => {
await mockRpcResponse({
method: 'sui_getOwnedObjects',
params: [],
value: {
objects: [EXAMPLE_OBJECT],
},
});

const resp = await client.requestWithType(
'sui_getOwnedObjects',
[],
pick({ objects: array(ObjectRef) })
);
expect(resp.objects.length).toEqual(1);
expect(resp.objects[0]).toEqual(EXAMPLE_OBJECT);
});
});
Loading