Refactor: Lint test/client (#1602)

* lint test/client
This commit is contained in:
Mukul Jangid
2021-09-16 16:28:16 -04:00
committed by Mayukha Vadari
parent 3cbdbac4f9
commit 10445cff01
10 changed files with 96 additions and 89 deletions

View File

@@ -81,6 +81,13 @@ module.exports = {
'max-nested-callbacks': 'off', 'max-nested-callbacks': 'off',
}, },
}, },
{
files: ['test/client/*.ts'],
rules: {
// Rule does not work with dynamically generated tests.
'mocha/no-setup-in-describe': 'off',
},
},
{ {
files: ['test/models/*.ts'], files: ['test/models/*.ts'],
rules: { rules: {

View File

@@ -25,6 +25,6 @@ describe('client.combine', function () {
request.push(binary.encode(tx)) request.push(binary.encode(tx))
assert.throws(() => { assert.throws(() => {
this.client.combine(request) this.client.combine(request)
}, /txJSON is not the same for all signedTransactions/) }, /txJSON is not the same for all signedTransactions/u)
}) })
}) })

View File

@@ -14,7 +14,7 @@ describe('client constructor', function () {
it('Client valid options', function () { it('Client valid options', function () {
const client = new Client('wss://s:1') const client = new Client('wss://s:1')
const privateConnectionUrl = (client.connection as any).url const privateConnectionUrl = client.connection.getUrl()
assert.deepEqual(privateConnectionUrl, 'wss://s:1') assert.deepEqual(privateConnectionUrl, 'wss://s:1')
}) })

View File

@@ -13,9 +13,8 @@ describe('getBalances', function () {
beforeEach(setupClient) beforeEach(setupClient)
afterEach(teardownClient) afterEach(teardownClient)
// eslint-disable-next-line mocha/no-setup-in-describe -- Rule does not allow dynamic test generation. addressTests.forEach(function (testcase) {
addressTests.forEach(function (testCase) { describe(testcase.type, function () {
describe(testCase.type, function () {
it('getBalances', async function () { it('getBalances', async function () {
this.mockRippled.addResponse( this.mockRippled.addResponse(
'account_info', 'account_info',
@@ -26,45 +25,43 @@ describe('getBalances', function () {
rippledAccountLines.normal, rippledAccountLines.normal,
) )
this.mockRippled.addResponse('ledger', rippled.ledger.normal) this.mockRippled.addResponse('ledger', rippled.ledger.normal)
const result = await this.client.getBalances(testCase.address) const result = await this.client.getBalances(testcase.address)
assertResultMatch(result, responses.getBalances, 'getBalances') assertResultMatch(result, responses.getBalances, 'getBalances')
}) })
// it("getBalances - limit", async function () { // it('getBalances - limit', async function () {
// const request = { // const options = { limit: 3, ledgerVersion: 123456 }
// account: test.address,
// limit: 10,
// ledger_index: 123456,
// };
// this.mockRippled.addResponse( // this.mockRippled.addResponse(
// "account_info", // 'account_info',
// rippled.account_info.normal // rippled.account_info.normal,
// ); // )
// this.mockRippled.addResponse( // this.mockRippled.addResponse(
// "account_lines", // 'account_lines',
// rippledAccountLines.normal // rippledAccountLines.normal,
// ); // )
// this.mockRippled.addResponse("ledger", rippled.ledger.normal); // this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// const expectedResponse = responses.getBalances.slice(0, 10); // const expectedResponse = responses.getBalances.slice(0, 3)
// const result = await this.client.getBalances(request); // const result = await this.client.getBalances(testcase.address, options)
// assertResultMatch(result, expectedResponse, "getBalances"); // assertResultMatch(result, expectedResponse, 'getBalances')
// }); // })
// it("getBalances - limit", async function () { // it('getBalances - limit & currency', async function () {
// const options = { limit: 3, ledgerVersion: 123456 }; // const options = { currency: 'USD', limit: 3 }
// this.mockRippled.addResponse( // this.mockRippled.addResponse(
// "account_info", // 'account_info',
// rippled.account_info.normal // rippled.account_info.normal,
// ); // )
// this.mockRippled.addResponse( // this.mockRippled.addResponse(
// "account_lines", // 'account_lines',
// rippledAccountLines.normal // rippledAccountLines.normal,
// ); // )
// this.mockRippled.addResponse("ledger", rippled.ledger.normal); // this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// const expectedResponse = responses.getBalances.slice(0, 3); // const expectedResponse = responses.getBalances
// const result = await this.client.getBalances(test.address, options); // .filter((item) => item.currency === 'USD')
// assertResultMatch(result, expectedResponse, "getBalances"); // .slice(0, 3)
// }); // const result = await this.client.getBalances(testcase.address, options)
// assertResultMatch(result, expectedResponse, 'getBalances')
// })
// it("getBalances - limit & currency", async function () { // it("getBalances - limit & currency", async function () {
// const options = { currency: "USD", limit: 3 }; // const options = { currency: "USD", limit: 3 };

View File

@@ -40,21 +40,23 @@ import { addressTests } from '../testUtils'
// return true; // return true;
// } // }
function isUSD(currency: string) { function isUSD(currency: string): boolean {
return ( return (
currency === 'USD' || currency === 'USD' ||
currency === '0000000000000000000000005553440000000000' currency === '0000000000000000000000005553440000000000'
) )
} }
function isBTC(currency: string) { function isBTC(currency: string): boolean {
return ( return (
currency === 'BTC' || currency === 'BTC' ||
currency === '0000000000000000000000004254430000000000' currency === '0000000000000000000000004254430000000000'
) )
} }
function normalRippledResponse(request: BookOffersRequest): object { function normalRippledResponse(
request: BookOffersRequest,
): Record<string, unknown> {
if ( if (
isBTC(request.taker_gets.currency) && isBTC(request.taker_gets.currency) &&
isUSD(request.taker_pays.currency) isUSD(request.taker_pays.currency)
@@ -70,57 +72,59 @@ function normalRippledResponse(request: BookOffersRequest): object {
throw new Error('unexpected end') throw new Error('unexpected end')
} }
// function xrpRippledResponse(request: BookOffersRequest): object { // function xrpRippledResponse(
// if (request.taker_pays.issuer === "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw") { // request: BookOffersRequest,
// return rippled.book_offers.xrp_usd; // ): Record<string, unknown> {
// if (request.taker_pays.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
// return rippled.book_offers.xrp_usd
// } // }
// if (request.taker_gets.issuer === "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw") { // if (request.taker_gets.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
// return rippled.book_offers.usd_xrp; // return rippled.book_offers.usd_xrp
// } // }
// throw new Error("unexpected end"); // throw new Error('unexpected end')
// } // }
describe('client.getOrderbook', function () { describe('client.getOrderbook', function () {
beforeEach(setupClient) beforeEach(setupClient)
afterEach(teardownClient) afterEach(teardownClient)
addressTests.forEach(function (test) { addressTests.forEach(function (testcase) {
describe(test.type, function () { describe(testcase.type, function () {
it('normal', async function () { it('normal', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse) this.mockRippled.addResponse('book_offers', normalRippledResponse)
const response = await this.client.getOrderbook( const response = await this.client.getOrderbook(
requests.getOrderbook.normal.taker_pays, requests.getOrderbook.normal.taker_pays,
requests.getOrderbook.normal.taker_gets, requests.getOrderbook.normal.taker_gets,
{ { limit: 1 },
limit: 1,
},
) )
assert.deepEqual(response, responses.getOrderbook.normal) assert.deepEqual(response, responses.getOrderbook.normal)
}) })
// it("invalid options", async function () { // it('invalid options', async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse); // this.mockRippled.addResponse('book_offers', normalRippledResponse)
// assertRejects( // assertRejects(
// this.client.getOrderbook(test.address, requests.getOrderbook.normal, { // this.client.getOrderbook(
// invalid: "options", // testcase.address,
// }), // requests.getOrderbook.normal,
// this.client.errors.ValidationError // {
// ); // invalid: 'options',
// }); // },
// ),
// it("with XRP", async function () { // this.client.errors.ValidationError,
// this.mockRippled.addResponse("book_offers", xrpRippledResponse); // )
// })
// it('with XRP', async function () {
// this.mockRippled.addResponse('book_offers', xrpRippledResponse)
// const response = await this.client.getOrderbook( // const response = await this.client.getOrderbook(
// test.address, // testcase.address,
// requests.getOrderbook.withXRP // requests.getOrderbook.withXRP,
// ); // )
// assertResultMatch( // assertResultMatch(
// response, // response,
// responses.getOrderbook.withXRP, // responses.getOrderbook.withXRP,
// "getOrderbook" // 'getOrderbook',
// ); // )
// }); // })
// 'sample XRP/JPY book has orders sorted correctly', async function () { // 'sample XRP/JPY book has orders sorted correctly', async function () {
// const orderbookInfo = { // const orderbookInfo = {
// base: { // base: {
@@ -137,7 +141,6 @@ describe('client.getOrderbook', function () {
// assert.deepStrictEqual([], response.bids) // assert.deepStrictEqual([], response.bids)
// checkSortingOfOrders(response.asks) // checkSortingOfOrders(response.asks)
// }, // },
// 'sample USD/XRP book has orders sorted correctly', async function () { // 'sample USD/XRP book has orders sorted correctly', async function () {
// const orderbookInfo = { // const orderbookInfo = {
// counter: {currency: 'XRP'}, // counter: {currency: 'XRP'},
@@ -151,7 +154,6 @@ describe('client.getOrderbook', function () {
// checkSortingOfOrders(response.bids) // checkSortingOfOrders(response.bids)
// checkSortingOfOrders(response.asks) // checkSortingOfOrders(response.asks)
// }, // },
// WARNING: This test fails to catch the sorting bug, issue #766 // WARNING: This test fails to catch the sorting bug, issue #766
// it("sorted so that best deals come first [bad test]", async function () { // it("sorted so that best deals come first [bad test]", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse); // this.mockRippled.addResponse("book_offers", normalRippledResponse);
@@ -177,7 +179,6 @@ describe('client.getOrderbook', function () {
// askRates // askRates
// ); // );
// }); // });
// it("currency & counterparty are correct", async function () { // it("currency & counterparty are correct", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse); // this.mockRippled.addResponse("book_offers", normalRippledResponse);
// const response = await this.client.getOrderbook( // const response = await this.client.getOrderbook(
@@ -194,7 +195,6 @@ describe('client.getOrderbook', function () {
// assert.strictEqual(totalPrice.counterparty, counter.counterparty); // assert.strictEqual(totalPrice.counterparty, counter.counterparty);
// }); // });
// }); // });
// it("direction is correct for bids and asks", async function () { // it("direction is correct for bids and asks", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse); // this.mockRippled.addResponse("book_offers", normalRippledResponse);
// const response = await this.client.getOrderbook( // const response = await this.client.getOrderbook(

View File

@@ -15,7 +15,9 @@ describe('client.hasNextPage', function () {
}) })
it('returns false when there are no more pages', async function () { it('returns false when there are no more pages', async function () {
const rippledResponse = function (request: Request): object { const rippledResponse = function (
request: Request,
): Record<string, unknown> {
if ('marker' in request) { if ('marker' in request) {
return rippled.ledger_data.last_page return rippled.ledger_data.last_page
} }

View File

@@ -1,3 +1,4 @@
/* eslint-disable mocha/no-hooks-for-single-case -- Use of hooks is restricted when there is a single test case. */
import { assert } from 'chai' import { assert } from 'chai'
import { setupClient, teardownClient } from '../setupClient' import { setupClient, teardownClient } from '../setupClient'

View File

@@ -7,8 +7,8 @@ describe('client.request', function () {
beforeEach(setupClient) beforeEach(setupClient)
afterEach(teardownClient) afterEach(teardownClient)
addressTests.forEach(function (test) { addressTests.forEach(function (testcase) {
describe(test.type, function () { describe(testcase.type, function () {
it('request account_objects', async function () { it('request account_objects', async function () {
this.mockRippled.addResponse( this.mockRippled.addResponse(
'account_objects', 'account_objects',
@@ -16,7 +16,7 @@ describe('client.request', function () {
) )
const result = await this.client.request({ const result = await this.client.request({
command: 'account_objects', command: 'account_objects',
account: test.address, account: testcase.address,
}) })
assertResultMatch( assertResultMatch(
@@ -33,7 +33,7 @@ describe('client.request', function () {
) )
const result = await this.client.request({ const result = await this.client.request({
command: 'account_objects', command: 'account_objects',
account: test.address, account: testcase.address,
}) })
assertResultMatch( assertResultMatch(

View File

@@ -5,7 +5,7 @@ import rippled from '../fixtures/rippled'
import { setupClient, teardownClient } from '../setupClient' import { setupClient, teardownClient } from '../setupClient'
import { assertRejects } from '../testUtils' import { assertRejects } from '../testUtils'
const rippledResponse = function (request: Request): object { const rippledResponse = function (request: Request): Record<string, unknown> {
if ('marker' in request) { if ('marker' in request) {
return rippled.ledger_data.last_page return rippled.ledger_data.last_page
} }

View File

@@ -88,7 +88,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
const tx = JSON.stringify(binary.decode(result.signedTransaction)) const tx = JSON.stringify(binary.decode(result.signedTransaction))
this.client.sign(tx, keypair) this.client.sign(tx, keypair)
}, /txJSON must not contain "TxnSignature" or "Signers" properties/) }, /txJSON must not contain "TxnSignature" or "Signers" properties/u)
}) })
it('withKeypair EscrowExecution', async function () { it('withKeypair EscrowExecution', async function () {
@@ -122,7 +122,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
const tx = JSON.stringify(binary.decode(result.signedTransaction)) const tx = JSON.stringify(binary.decode(result.signedTransaction))
this.client.sign(tx, secret) this.client.sign(tx, secret)
}, /txJSON must not contain "TxnSignature" or "Signers" properties/) }, /txJSON must not contain "TxnSignature" or "Signers" properties/u)
}) })
it('succeeds - no flags', async function () { it('succeeds - no flags', async function () {
@@ -138,7 +138,7 @@ describe('client.sign', function () {
const decoded = binary.decode(result.signedTransaction) const decoded = binary.decode(result.signedTransaction)
assert( assert(
decoded.Flags == null, decoded.Flags == null,
`Flags = ${decoded.Flags}, should be undefined`, `Flags = ${decoded.Flags as number}, should be undefined`,
) )
assert.deepEqual(result, expectedResult) assert.deepEqual(result, expectedResult)
}) })
@@ -158,7 +158,7 @@ describe('client.sign', function () {
const decoded = binary.decode(result.signedTransaction) const decoded = binary.decode(result.signedTransaction)
assert( assert(
decoded.Flags === 2147614720, decoded.Flags === 2147614720,
`Flags = ${decoded.Flags}, should be 2147614720`, `Flags = ${decoded.Flags as number}, should be 2147614720`,
) )
assert.deepEqual(result, expectedResult) assert.deepEqual(result, expectedResult)
}) })
@@ -177,7 +177,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
this.client.sign(request.txJSON, secret) this.client.sign(request.txJSON, secret)
}, /1\.2 is an illegal amount/) }, /1\.2 is an illegal amount/u)
}) })
it('throws when encoded tx does not match decoded tx - higher fee', async function () { it('throws when encoded tx does not match decoded tx - higher fee', async function () {
@@ -194,7 +194,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
this.client.sign(request.txJSON, secret) this.client.sign(request.txJSON, secret)
}, /1123456\.7 is an illegal amount/) }, /1123456\.7 is an illegal amount/u)
}) })
it('permits fee exceeding 2000000 drops when maxFeeXRP is higher than 2 XRP', async function () { it('permits fee exceeding 2000000 drops when maxFeeXRP is higher than 2 XRP', async function () {
@@ -240,7 +240,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
this.client.sign(request.txJSON, secret) this.client.sign(request.txJSON, secret)
}, /Fee" should not exceed "2000000"\. To use a higher fee, set `maxFeeXRP` in the Client constructor\./) }, /Fee" should not exceed "2000000"\. To use a higher fee, set `maxFeeXRP` in the Client constructor\./u)
}) })
it('throws when Fee exceeds maxFeeXRP (in drops) - custom maxFeeXRP', async function () { it('throws when Fee exceeds maxFeeXRP (in drops) - custom maxFeeXRP', async function () {
@@ -257,7 +257,7 @@ describe('client.sign', function () {
assert.throws(() => { assert.throws(() => {
this.client.sign(request.txJSON, secret) this.client.sign(request.txJSON, secret)
}, /Fee" should not exceed "1900000"\. To use a higher fee, set `maxFeeXRP` in the Client constructor\./) }, /Fee" should not exceed "1900000"\. To use a higher fee, set `maxFeeXRP` in the Client constructor\./u)
}) })
it('sign with paths', async function () { it('sign with paths', async function () {
@@ -329,7 +329,7 @@ describe('client.sign', function () {
const secret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV' const secret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV'
assert.throws(() => { assert.throws(() => {
this.client.sign(JSON.stringify(payment), secret) this.client.sign(JSON.stringify(payment), secret)
}, /^1.1234567 is an illegal amount/) }, /^1.1234567 is an illegal amount/u)
}) })
it('throws when encoded tx does not match decoded tx - prepared order', async function () { it('throws when encoded tx does not match decoded tx - prepared order', async function () {