Skip to content

Commit 9e50873

Browse files
katspaughiamacook
andauthored
Chore: TypeScript in tests (#49)
* Chore: TypeScript in tests * Unskip test * test: Dynamic enum export check * fix: Add omission check * fix: Unused test var * chore: Add prettier to pre-commit * fix: Tweak pre-commit * chore: Run prettier * fix: Unused vars Co-authored-by: iamacook <aaron.cook@gnosis.pm>
1 parent b86e6f0 commit 9e50873

File tree

10 files changed

+127
-48
lines changed

10 files changed

+127
-48
lines changed

.github/workflows/typedoc.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: typedoc
22

3-
43
on:
54
push:
65
branches:

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Safe Gateway TypeScript SDK
2+
23
[![npm](https://img.shields.io/npm/v/@gnosis.pm/safe-react-gateway-sdk?label=%40gnosis.pm%2Fsafe-react-gateway-sdk)](https://www.npmjs.com/package/@gnosis.pm/safe-react-gateway-sdk)
34

45
A TypeScript SDK for the [Safe Gateway](https://github.com/gnosis/safe-client-gateway)
56

67
## Links
7-
* [Gateway API docs](https://gnosis.github.io/safe-client-gateway/docs/routes/index.html)
8-
* [SDK typedoc](https://gnosis.github.io/safe-react-gateway-sdk/modules.html#getBalances)
8+
9+
- [Gateway API docs](https://gnosis.github.io/safe-client-gateway/docs/routes/index.html)
10+
- [SDK typedoc](https://gnosis.github.io/safe-react-gateway-sdk/modules.html#getBalances)
911

1012
## Adding an endpoint
13+
1114
Endpoint types are defined in `src/types/gateway.ts`.
1215

1316
Each endpoint consists of:

e2e/propose-transaction.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import config from './config'
33

44
describe('proposeTransaction tests', () => {
55
// Skipping this test, see https://github.com/gnosis/safe-client-gateway/issues/745
6-
it.skip('should propose a transaction and fail', async () => {
6+
it('should propose a transaction and fail', async () => {
77
const req = proposeTransaction(config.baseUrl, '4', '0x4f9BD57BCC68Bf7770429F137922B3afD23d83E7', {
88
to: '0x49d4450977E2c95362C13D3a31a09311E0Ea26A6',
99
value: '0',
@@ -20,7 +20,7 @@ describe('proposeTransaction tests', () => {
2020
origin: null,
2121
})
2222
await expect(req).rejects.toThrow(
23-
'1337: {"nonFieldErrors":["Tx with safe-tx-hash=0x98798b6d9400b25397e85eb79c444a06f93d153555c1d7fd026176f02055a824 for safe=0x4f9BD57BCC68Bf7770429F137922B3afD23d83E7 was already executed in tx-hash=0x1baa941b8696ff3b0a8831f11da243a02f028122b6298ee61e3a1c0d5eeb171a"]}',
23+
'1337: {"nonFieldErrors":["Contract-transaction-hash=0xdf2c4db07ee705e94135d92f835cc241b1d4c20e10d481a4e445265851448cb3 does not match provided contract-tx-hash=0x98798b6d9400b25397e85eb79c444a06f93d153555c1d7fd026176f02055a824"]}',
2424
)
2525
})
2626
})

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gnosis.pm/safe-react-gateway-sdk",
3-
"version": "2.7.4",
3+
"version": "2.7.5",
44
"main": "dist/index.min.js",
55
"types": "dist/index.d.ts",
66
"files": [
@@ -16,7 +16,7 @@
1616
"@babel/core": "^7.15.0",
1717
"@babel/preset-env": "^7.15.0",
1818
"@babel/preset-typescript": "^7.15.0",
19-
"@types/jest": "^27.0.1",
19+
"@types/jest": "^27.0.3",
2020
"@typescript-eslint/eslint-plugin": "^4.31.0",
2121
"@typescript-eslint/parser": "^4.31.0",
2222
"babel-jest": "^27.2.0",
@@ -34,13 +34,15 @@
3434
"webpack-cli": "^4.7.2"
3535
},
3636
"scripts": {
37-
"lint:fix": "eslint './src/**/*.ts' --fix",
37+
"lint": "eslint './src/**/*.ts'",
38+
"lint:fix": "yarn lint --fix",
3839
"types": "yarn tsc",
3940
"start": "rm -rf dist && webpack --mode production --watch",
4041
"build": "rm -rf dist && webpack --mode production",
41-
"prepare": "yarn build",
42-
"prettier": "prettier -w",
42+
"prepublishOnly": "yarn types && yarn lint && yarn test:check && yarn build",
43+
"prettier": "prettier -w .",
4344
"test": "jest --watch --coverage .",
45+
"test:check": "jest --testPathPattern=tests",
4446
"test:ci": "jest --ci --coverage --json --watchAll=false --testLocationInResults --runInBand --testPathPattern=tests"
4547
},
4648
"husky": {
@@ -51,7 +53,7 @@
5153
"lint-staged": {
5254
"src/**/*.{js,jsx,ts,tsx}": [
5355
"eslint --fix",
54-
"prettier --write"
56+
"prettier --w ."
5557
]
5658
}
5759
}

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export * from './types/decoded-data'
1919
* Get basic information about a Safe. E.g. owners, modules, version etc
2020
*/
2121
export function getSafeInfo(baseUrl: string, chainId: string, address: string): Promise<SafeInfo> {
22-
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/', { path: { chainId, address } })
22+
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}', { path: { chainId, address } })
2323
}
2424

2525
/**
@@ -32,7 +32,7 @@ export function getBalances(
3232
currency = 'usd',
3333
query: operations['safes_balances_list']['parameters']['query'] = {},
3434
): Promise<SafeBalanceResponse> {
35-
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/balances/{currency}/', {
35+
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/balances/{currency}', {
3636
path: { chainId, address, currency },
3737
query,
3838
})
@@ -61,7 +61,7 @@ export function getCollectibles(
6161
address: string,
6262
query: operations['safes_collectibles_list']['parameters']['query'] = {},
6363
): Promise<SafeCollectibleResponse[]> {
64-
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/collectibles/', {
64+
return callEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/collectibles', {
6565
path: { chainId, address },
6666
query,
6767
})
@@ -151,7 +151,7 @@ export function getChainsConfig(
151151
baseUrl: string,
152152
query?: operations['chains_list']['parameters']['query'],
153153
): Promise<ChainListResponse> {
154-
return callEndpoint(baseUrl, '/v1/chains/', {
154+
return callEndpoint(baseUrl, '/v1/chains', {
155155
query,
156156
})
157157
}
@@ -160,7 +160,7 @@ export function getChainsConfig(
160160
* Returns a chain config
161161
*/
162162
export function getChainConfig(baseUrl: string, chainId: string): Promise<ChainInfo> {
163-
return callEndpoint(baseUrl, '/v1/chains/{chainId}/', {
163+
return callEndpoint(baseUrl, '/v1/chains/{chainId}', {
164164
path: { chainId: chainId },
165165
})
166166
}

src/types/api.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { DecodedDataRequest, DecodedDataResponse } from './decoded-data'
1212
import { MasterCopyReponse } from './master-copies'
1313

1414
export interface paths {
15-
'/v1/chains/{chainId}/safes/{address}/': {
15+
'/v1/chains/{chainId}/safes/{address}': {
1616
/** Get status of the safe */
1717
get: operations['safes_read']
1818
parameters: {
@@ -22,7 +22,7 @@ export interface paths {
2222
}
2323
}
2424
}
25-
'/v1/chains/{chainId}/safes/{address}/balances/{currency}/': {
25+
'/v1/chains/{chainId}/safes/{address}/balances/{currency}': {
2626
get: operations['safes_balances_list']
2727
parameters: {
2828
path: {
@@ -36,7 +36,7 @@ export interface paths {
3636
get: operations['get_supported_fiat']
3737
parameters: null
3838
}
39-
'/v1/chains/{chainId}/safes/{address}/collectibles/': {
39+
'/v1/chains/{chainId}/safes/{address}/collectibles': {
4040
/** Get collectibles (ERC721 tokens) and information about them */
4141
get: operations['safes_collectibles_list']
4242
parameters: {
@@ -102,7 +102,7 @@ export interface paths {
102102
}
103103
}
104104
}
105-
'/v1/chains/': {
105+
'/v1/chains': {
106106
get: operations['chains_list']
107107
parameters: {
108108
query: {
@@ -112,7 +112,7 @@ export interface paths {
112112
}
113113
}
114114
}
115-
'/v1/chains/{chainId}/': {
115+
'/v1/chains/{chainId}': {
116116
get: operations['chains_read']
117117
parameters: {
118118
path: {

tests/endpoint.test.js renamed to tests/endpoint.test.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,94 @@ describe('callEndpoint', () => {
2525

2626
it('should accept a path param', async () => {
2727
await expect(
28-
callEndpoint('https://safe-client.staging.gnosisdev.com', '/v1/chains/4/safe/{address}', {
29-
path: { address: '0x123' },
28+
callEndpoint('https://safe-client.staging.gnosisdev.com', '/v1/chains/{chainId}/safes/{address}', {
29+
path: { chainId: '4', address: '0x123' },
3030
}),
3131
).resolves.toEqual({ success: true })
3232

3333
expect(fetchData).toHaveBeenCalledWith(
34-
'https://safe-client.staging.gnosisdev.com/v1/chains/4/safe/0x123',
34+
'https://safe-client.staging.gnosisdev.com/v1/chains/4/safes/0x123',
3535
undefined,
3636
)
3737
})
3838

3939
it('should accept several path params', async () => {
4040
await expect(
41-
callEndpoint('https://safe-client.staging.gnosisdev.com', '/v1/chains/4/balances/{address}/{currency}', {
42-
path: { address: '0x123', currency: 'usd' },
43-
}),
41+
callEndpoint(
42+
'https://safe-client.staging.gnosisdev.com',
43+
'/v1/chains/{chainId}/safes/{address}/balances/{currency}',
44+
{
45+
path: { chainId: '4', address: '0x123', currency: 'usd' },
46+
query: {},
47+
},
48+
),
4449
).resolves.toEqual({ success: true })
4550

4651
expect(fetchData).toHaveBeenCalledWith(
47-
'https://safe-client.staging.gnosisdev.com/v1/chains/4/balances/0x123/usd',
52+
'https://safe-client.staging.gnosisdev.com/v1/chains/4/safes/0x123/balances/usd',
4853
undefined,
4954
)
5055
})
5156

5257
it('should accept query params', async () => {
5358
await expect(
54-
callEndpoint('https://safe-client.staging.gnosisdev.com', '/v1/chains/4/balances/{address}/{currency}', {
55-
path: { address: '0x123', currency: 'usd' },
56-
query: { exclude_spam: true },
57-
}),
59+
callEndpoint(
60+
'https://safe-client.staging.gnosisdev.com',
61+
'/v1/chains/{chainId}/safes/{address}/balances/{currency}',
62+
{
63+
path: { chainId: '4', address: '0x123', currency: 'usd' },
64+
query: { exclude_spam: true },
65+
},
66+
),
5867
).resolves.toEqual({ success: true })
5968

6069
expect(fetchData).toHaveBeenCalledWith(
61-
'https://safe-client.staging.gnosisdev.com/v1/chains/4/balances/0x123/usd?exclude_spam=true',
70+
'https://safe-client.staging.gnosisdev.com/v1/chains/4/safes/0x123/balances/usd?exclude_spam=true',
6271
undefined,
6372
)
6473
})
6574

6675
it('should accept body', async () => {
76+
const body = {
77+
to: '0x123',
78+
value: 'test',
79+
data: null,
80+
nonce: '1',
81+
operation: 1,
82+
safeTxGas: '',
83+
baseGas: '100',
84+
gasPrice: '1',
85+
gasToken: '',
86+
refundReceiver: null,
87+
safeTxHash: '0x123',
88+
sender: '0x123',
89+
signature: null,
90+
origin: null,
91+
}
92+
6793
await expect(
68-
callEndpoint('https://safe-client.staging.gnosisdev.com', '/v1/chains/4/transactions/{safe_address}/propose', {
69-
path: { safe_address: '0x123' },
70-
body: { test: 'test' },
71-
}),
94+
callEndpoint(
95+
'https://safe-client.staging.gnosisdev.com',
96+
'/v1/chains/{chainId}/transactions/{safe_address}/propose',
97+
{
98+
path: { chainId: '4', safe_address: '0x123' },
99+
body,
100+
},
101+
),
72102
).resolves.toEqual({ success: true })
73103

74104
expect(fetchData).toHaveBeenCalledWith(
75105
'https://safe-client.staging.gnosisdev.com/v1/chains/4/transactions/0x123/propose',
76-
{ test: 'test' },
106+
body,
77107
)
78108
})
79109

80110
it('should accept a raw URL', async () => {
81111
await expect(
82112
callEndpoint(
83113
'https://safe-client.staging.gnosisdev.com',
84-
'/v1/chains/4/balances/{address}/{currency}',
85-
{ path: { address: '0x123', currency: 'usd' }, query: { exclude_spam: true } },
114+
'/v1/chains/{chainId}/safes/{address}/balances/{currency}',
115+
{ path: { chainId: '4', address: '0x123', currency: 'usd' }, query: { exclude_spam: true } },
86116
'/test-url?raw=true',
87117
),
88118
).resolves.toEqual({ success: true })

tests/types.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
SafeAppData,
3+
MultisigTransactionRequest,
4+
BlockExplorerUriTemplate,
5+
FiatCurrencies,
6+
MasterCopyReponse,
7+
DecodedDataParameterValue,
8+
} from '../src'
9+
10+
describe('Types are exported from index correctly', () => {
11+
it('Random type check', () => {
12+
const val = {} as unknown as
13+
| SafeAppData
14+
| MultisigTransactionRequest
15+
| BlockExplorerUriTemplate
16+
| FiatCurrencies
17+
| MasterCopyReponse
18+
| DecodedDataParameterValue
19+
expect(val).toBe(val)
20+
})
21+
22+
it('Dynamic enum export check', () => {
23+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
24+
const { default: _ = null, ...rest } = require('../src/index')
25+
const exportedNames = Object.keys(rest)
26+
27+
const fs = require('fs')
28+
const path = require('path')
29+
30+
const typesDir = path.join(__dirname, '..', 'src', 'types')
31+
32+
const exportedTypeNames = fs
33+
.readdirSync(typesDir)
34+
.filter((file) => file !== 'api.ts')
35+
.flatMap((file) => {
36+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
37+
const { default: _ = null, ...rest } = require(`../src/types/${file}`)
38+
return Object.keys(rest)
39+
})
40+
41+
exportedTypeNames.every((type) => expect(exportedNames.includes(type)).toBe(true))
42+
})
43+
})

tests/utils.test.js renamed to tests/utils.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { fetchData, insertParams, stringifyQuery } from '../src/utils'
33

44
jest.mock('isomorphic-unfetch')
55

6+
const fetchMock = fetch as typeof fetch & jest.Mock
7+
68
describe('utils', () => {
79
describe('insertParams', () => {
810
it('should insert a param into a string', () => {
@@ -32,7 +34,7 @@ describe('utils', () => {
3234

3335
describe('fetchData', () => {
3436
it('should fetch a simple url', async () => {
35-
fetch.mockImplementation(() => {
37+
fetchMock.mockImplementation(() => {
3638
return Promise.resolve({
3739
ok: true,
3840
text: () => Promise.resolve('{"success": "true"}'),
@@ -45,7 +47,7 @@ describe('utils', () => {
4547
})
4648

4749
it('should make a post request', async () => {
48-
fetch.mockImplementation(() => {
50+
fetchMock.mockImplementation(() => {
4951
return Promise.resolve({
5052
ok: true,
5153
text: () => Promise.resolve('{"success": "true"}'),
@@ -65,7 +67,7 @@ describe('utils', () => {
6567
})
6668

6769
it('should throw if response is not OK', async () => {
68-
fetch.mockImplementation(() => {
70+
fetchMock.mockImplementation(() => {
6971
return Promise.resolve({
7072
ok: false,
7173
statusText: 'Failed',
@@ -78,7 +80,7 @@ describe('utils', () => {
7880
})
7981

8082
it('should throw the response text for 50x errors', async () => {
81-
fetch.mockImplementation(() => {
83+
fetchMock.mockImplementation(() => {
8284
return Promise.resolve({
8385
ok: false,
8486
statusText: 'Failed',

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,10 +1258,10 @@
12581258
dependencies:
12591259
"@types/istanbul-lib-report" "*"
12601260

1261-
"@types/jest@^27.0.1":
1262-
version "27.0.1"
1263-
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.1.tgz#fafcc997da0135865311bb1215ba16dba6bdf4ca"
1264-
integrity sha512-HTLpVXHrY69556ozYkcq47TtQJXpcWAWfkoqz+ZGz2JnmZhzlRjprCIyFnetSy8gpDWwTTGBcRVv1J1I1vBrHw==
1261+
"@types/jest@^27.0.3":
1262+
version "27.0.3"
1263+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.3.tgz#0cf9dfe9009e467f70a342f0f94ead19842a783a"
1264+
integrity sha512-cmmwv9t7gBYt7hNKH5Spu7Kuu/DotGa+Ff+JGRKZ4db5eh8PnKS4LuebJ3YLUoyOyIHraTGyULn23YtEAm0VSg==
12651265
dependencies:
12661266
jest-diff "^27.0.0"
12671267
pretty-format "^27.0.0"

0 commit comments

Comments
 (0)