From d7dedb6b46bcdbe8ab8f71ff78aad9fae6e0ceb3 Mon Sep 17 00:00:00 2001 From: Vladimir Volek Date: Mon, 5 Jul 2021 17:29:34 +0200 Subject: [PATCH] tests: generate tests (#69) * tests: generate tests * tests: add more tests --- example/index.ts | 1 + jest.config.js | 5 +- package.json | 2 +- src/endpoints/ledger/index.ts | 2 +- src/index.ts | 4 +- test/fixtures/addresses.ts | 15 + test/fixtures/assets.ts | 507 ++++++++++++++++++++++++++++ test/fixtures/blocks.ts | 77 +++++ test/fixtures/epochs.ts | 49 +++ test/fixtures/health.ts | 12 + test/fixtures/ledger.ts | 19 ++ test/fixtures/root.ts | 11 + test/setup.ts | 26 ++ test/tests/endpoints/index.ts | 24 ++ test/{unit => }/tests/utils.ts | 72 +++- test/unit/fixtures/blocks.ts | 29 -- test/unit/tests/endpoints/blocks.ts | 10 - test/{unit => }/utils.ts | 2 +- tsconfig.json | 2 +- 19 files changed, 818 insertions(+), 51 deletions(-) create mode 100644 test/fixtures/addresses.ts create mode 100644 test/fixtures/assets.ts create mode 100644 test/fixtures/blocks.ts create mode 100644 test/fixtures/epochs.ts create mode 100644 test/fixtures/health.ts create mode 100644 test/fixtures/ledger.ts create mode 100644 test/fixtures/root.ts create mode 100644 test/setup.ts create mode 100644 test/tests/endpoints/index.ts rename test/{unit => }/tests/utils.ts (72%) delete mode 100644 test/unit/fixtures/blocks.ts delete mode 100644 test/unit/tests/endpoints/blocks.ts rename test/{unit => }/utils.ts (69%) diff --git a/example/index.ts b/example/index.ts index 784a9efb..a4d45662 100644 --- a/example/index.ts +++ b/example/index.ts @@ -1,4 +1,5 @@ import { BlockFrostAPI } from '../src/index'; + async function run() { const API = new BlockFrostAPI({ projectId: 'YOUR API KEY HERE', // see: https://blockfrost.io diff --git a/jest.config.js b/jest.config.js index 0d4870c0..4d6a9a40 100644 --- a/jest.config.js +++ b/jest.config.js @@ -5,9 +5,7 @@ module.exports = { moduleFileExtensions: ['ts', 'js'], collectCoverage: true, coveragePathIgnorePatterns: ['/node_modules/'], - setupFiles: ['dotenv/config'], - setupFilesAfterEnv: ['/jest.config.js'], - testMatch: ['/test/unit/tests/**/*.ts'], + testMatch: ['/test/tests/**/*.ts'], coverageReporters: ['json', 'lcov', 'text', 'text-summary'], collectCoverageFrom: ['/src/**/*.ts'], moduleNameMapper: { @@ -15,5 +13,6 @@ module.exports = { '^(utils)(.*)$': '/src/utils/$2', '^(sql)(.*)$': '/src/sql/$2', }, + setupFilesAfterEnv: ['/test/setup.ts'], preset: 'ts-jest', }; diff --git a/package.json b/package.json index 44e373f8..a5a498df 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "generate-types": "pnpify openapi-typescript ./node_modules/@blockfrost/openapi/swagger.yaml --output ./src/types/OpenApi.ts", "lint": "eslint ./src/**/*.ts", "run-example": "ts-node-dev --transpile-only ./example/index.ts", - "test": "jest -c jest.config.js" + "test": "jest -c ./jest.config.js" }, "dependencies": { "@blockfrost/openapi": "^0.1.22", diff --git a/src/endpoints/ledger/index.ts b/src/endpoints/ledger/index.ts index 1b604040..073d834d 100644 --- a/src/endpoints/ledger/index.ts +++ b/src/endpoints/ledger/index.ts @@ -3,7 +3,7 @@ import { getHeaders, handleError } from '../../utils'; import { components } from '../../types/OpenApi'; import { BlockFrostAPI } from '../../index'; -export function ledger( +export function genesis( this: BlockFrostAPI, ): Promise { return new Promise((resolve, reject) => { diff --git a/src/index.ts b/src/index.ts index 3d5c1ccd..1e8df9dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,7 +72,7 @@ import { poolsRetiring, } from './endpoints/pools'; -import { ledger } from './endpoints/ledger'; +import { genesis } from './endpoints/ledger'; import { root } from './endpoints/root'; import { metadataTxsLabel, @@ -505,7 +505,7 @@ class BlockFrostAPI { * @returns xxx * */ - ledger = ledger; + genesis = genesis; /** * metadataTxsLabel diff --git a/test/fixtures/addresses.ts b/test/fixtures/addresses.ts new file mode 100644 index 00000000..935f22f1 --- /dev/null +++ b/test/fixtures/addresses.ts @@ -0,0 +1,15 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => + SDK.addresses( + 'addr1q8zu4smzyf2r2mfqjd6tc6vxf2p8rccdfk82ye3eut2udkw9etpkygj5x4kjpym5h35cvj5zw83s6nvw5fnrnck4cmvshkfm4y', + ), + response: { + amount: [{ unit: 'lovelace', quantity: expect.any(String) }], + stake_address: expect.toBeTypeOrNull(String), + type: 'shelley', + }, + }, +] as const; diff --git a/test/fixtures/assets.ts b/test/fixtures/assets.ts new file mode 100644 index 00000000..24cae863 --- /dev/null +++ b/test/fixtures/assets.ts @@ -0,0 +1,507 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.assets(), + response: [ + { + asset: + '00000002df633853f6a47465c9496721d2d5b1291b8398016c0e87ae6e7574636f696e', + quantity: '1', + }, + { + asset: + '3a9241cd79895e3a8d65261b40077d4437ce71e9d7c8c6c00e3f658e4669727374636f696e', + quantity: '1', + }, + { + asset: + '02f68378e37af4545d027d0a9fa5581ac682897a3fc1f6d8f936ed2b4154414441', + quantity: '1000000', + }, + { + asset: + 'e8e62d329e73190190c3e323fb5c9fb98ee55f0676332ba949f29d724649525354', + quantity: '1', + }, + { + asset: + 'ac3f4224723e2ed9d166478662f6e48bae9ddf0fc5ee58f54f6c322943454e54', + quantity: '10000000', + }, + { + asset: + '12e65fa3585d80cba39dcf4f59363bb68b77f9d3c0784734427b151754534c41', + quantity: '425839369', + }, + { + asset: + 'e12ab5cf12f95cd57b739282d06af9dd61e1b1dde1e06f0c31f0251167696d62616c', + quantity: '42', + }, + { + asset: 'da8c30857834c6ae7203935b89278c532b3995245295456f993e1d244c51', + quantity: '21000000000000', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279416c6261', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279416d657468797374', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417175616d6172696e65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417368', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727941756275726e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417572656c6961', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417572656f6c696e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417765736f6d65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279417a756c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794265696765', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279426572796c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279426c61636b', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279426c7565', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279426f6e65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279427269636b', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727942726f776e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727942797a616e74696e65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794361646574', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727943616d656c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794361707269', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727943617264696e616c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436572756c65616e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727943686172747265757365', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436865727279', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727943696e6e616d6f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436c656d656e74696e65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436f616c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436f62616c74', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436f66666565', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279436f72616c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794379616e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727945626f6e79', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279456d6572616c64', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727946756368736961', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279476f6c64', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727947726179', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279477265656e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727948617a656c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727948756d62726f6c', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279496e6469676f', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727949726973', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794a616465', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794a657474', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794b656c6c79', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794b68616b69', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794c617661', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794c6176656e646572', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794c656d6f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794c696d65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794c757374', + quantity: '1', + }, + { + asset: 'ca079f39e33ca1b6ec7b4f4eedffa4f40d7d111f96bde855ba224dc2444558', + quantity: '10000000', + }, + { + asset: + '12e65fa3585d80cba39dcf4f59363bb68b77f9d3c0784734427b15174141504c', + quantity: '9648589196', + }, + { + asset: + 'b93822810dbd56a5b5b815b1946da690312913da74c94e019e028af97065646572', + quantity: '5', + }, + { + asset: + '12e65fa3585d80cba39dcf4f59363bb68b77f9d3c0784734427b15174150504c45', + quantity: '9648589196', + }, + { + asset: + 'd63b50fe629b69f1da4897aee9f381fe197c30c06bf35be4355360bd6d617279', + quantity: '1', + }, + { + asset: + '12e65fa3585d80cba39dcf4f59363bb68b77f9d3c0784734427b15175445534c41', + quantity: '425839369', + }, + { + asset: + 'd3501d9531fcc25e3ca4b6429318c2cc374dbdbcf5e99c1c1e5da1ff444f4e545350414d', + quantity: '10000000', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d61726f6f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d61757665', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d656c616e6965', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d656c6f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d696e74', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d6f7261646f', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d6f7373', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d757374617264', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794d7972746c65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794e617679', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794f6c697665', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794f6e7978', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794f72616e6765', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272794f7263686964', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272795065616368', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727950656172', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727950696e6b', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727950697374616368696f', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279506c6174696e756d', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279507572706c65', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272795261636b6c6579', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279526173706265727279', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279526176656e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279526564', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279526f7365', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279526f7578', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727952756279', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279527573736574', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272795275737479', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727953616666726f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727953616765', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727953616c6d6f6e', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f426572727953616e64', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f42657272795361707068697265', + quantity: '1', + }, + { + asset: + 'b863bc7369f46136ac1048adb2fa7dae3af944c3bbb2be2f216a8d4f4265727279536361726c657474', + quantity: '1', + }, + ], + }, +] as const; diff --git a/test/fixtures/blocks.ts b/test/fixtures/blocks.ts new file mode 100644 index 00000000..6f34560d --- /dev/null +++ b/test/fixtures/blocks.ts @@ -0,0 +1,77 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.blocksLatest(), + response: { + time: expect.any(Number), + height: expect.any(Number), + hash: expect.any(String), + slot: expect.any(Number), + epoch: expect.any(Number), + epoch_slot: expect.any(Number), + slot_leader: expect.any(String), + size: expect.any(Number), + tx_count: expect.any(Number), + output: expect.toBeTypeOrNull(String), + fees: expect.toBeTypeOrNull(String), + block_vrf: expect.any(String), + previous_block: expect.any(String), + next_block: null, + confirmations: expect.any(Number), + }, + }, + { + command: () => + SDK.blocks( + '5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb', + ), + response: { + time: 1506203091, + height: null, + hash: '5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb', + slot: null, + epoch: null, + epoch_slot: null, + slot_leader: 'Genesis slot leader', + size: 0, + tx_count: 14505, + output: '31112484745000000', + fees: '0', + block_vrf: null, + previous_block: null, + next_block: + '89d9b5a5b8ddc8d7e5a6795e9774d97faf1efea59b2caf7eaf9f8c5b32059df4', + confirmations: expect.any(Number), + }, + }, + { + command: () => SDK.blocksTxs('5360435'), + response: [ + '055f9d4c86733997a9b8b12e3422feee8ef95d79ccbde20abdb6ff9222bb79c6', + 'e66077bac8d32cf16aa991d793a3e5840d3eee1ad3dfd04f1ee609d46a936dff', + '70cf3046cd12879a9c36cafeddec17b3b22da619d7998b64c556fd9c70c62903', + '79993d55b7e49bb588c83d9f19bf03c10fe18e6b52c5684766c436e870921e88', + 'd49776db1423a229fee5403dfc1fc0c6512a1cb0cbc01c4fcd040db82329c125', + '0672c94a748c8ab06d99496bba73007871eb8d43766dc0c74ed326e0cc3f473c', + 'a14476ee69f5ec35331200e94da50ccad1529aa8ba22d5793f3d0f4fa31e8352', + 'b3ce7f3a4f9d680fc5de83200f436792ed0aec02e8e39bca4c255c5a50100906', + '2cf6eb3c194d9deb93e5219d4371f2f09fa90af5393b814f939f9b8f393a6889', + '08ae724058cd840350417153a432ff3d2fad5f8f8469f64742019b36794df519', + '8ed65e234e2db82a6b67e1982fd6e627b44e9abdce8cfea6d1d05f8bb786d8f5', + '783aefd86ba442d0574beb81b5d1db6b5c0dca40fa464d893aa5f849403842ba', + '0f750962b3a039ec1ea5065c98cf083fb737decfb54ba04bdfa2b107fef65b45', + '4f45923f709234b52b5ddb75f70b37b039519623dbb55da0916773912391fd32', + 'eb27af616f701cdf7ad81b4e9c503b2e26059859fff8b37c0f267272028c24fb', + '86f77e854e485135878eef867cf98469dc1c5a90ceb9dda33a981ac1dc183b85', + '8e7cd4e261631ca6b5b0ae588cf7c07617657e3ed563896e8e2c0226b7b7d152', + '849e2475ec8f2a35bf62d3a8cc70b87326f5509987bbb7e7fc36fb8e9b5c59d5', + '3f98fca4978cc2ac86380453538b1ab020dfa8aa34506827c7032540d9dc30b2', + 'cfe0a45709522edc07a0725cd7a99d65d5ce8f5b62365d9a3f9660bc79d25202', + '2a6bb221aac6192e3fdb05a1c82c1c2236fc6b226363021ec8036ff6f9475ed0', + 'b29bd7a9e79345639d359f549bbf0b668d6177c90a105c66186d8b98835d187f', + '9a5bdc512e9df8b20f486b38a190168c1b84fa250005eac35038019f1fd47039', + '3e7721720db366599ae5ce53dc3a48b56e1ffaaa2572ae0ad66eb6c8467b8eb7', + ], + }, +] as const; diff --git a/test/fixtures/epochs.ts b/test/fixtures/epochs.ts new file mode 100644 index 00000000..74afb309 --- /dev/null +++ b/test/fixtures/epochs.ts @@ -0,0 +1,49 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.epochsLatest(), + response: { + epoch: expect.any(Number), + start_time: expect.any(Number), + end_time: expect.any(Number), + first_block_time: expect.any(Number), + last_block_time: expect.any(Number), + block_count: expect.any(Number), + tx_count: expect.any(Number), + output: expect.any(String), + fees: expect.any(String), + active_stake: expect.any(String), + }, + }, + { + command: () => SDK.epochs(0), + response: { + epoch: 0, + start_time: 1506203091, + end_time: 1506635091, + first_block_time: 1506203091, + last_block_time: 1506635071, + block_count: 21587, + tx_count: 33, + output: '10378568796482912', + fees: '3458053', + active_stake: null, + }, + }, + { + command: () => SDK.epochs(242), + response: { + epoch: 242, + start_time: 1610747091, + end_time: 1611179091, + first_block_time: 1610747091, + last_block_time: 1611179076, + block_count: 21418, + tx_count: 74057, + output: '63148817438049616', + fees: '16905060417', + active_stake: '21755094259019945', + }, + }, +] as const; diff --git a/test/fixtures/health.ts b/test/fixtures/health.ts new file mode 100644 index 00000000..d8249885 --- /dev/null +++ b/test/fixtures/health.ts @@ -0,0 +1,12 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.health(), + response: { is_healthy: true }, + }, + { + command: () => SDK.healthClock(), + response: { server_time: expect.any(Number) }, + }, +] as const; diff --git a/test/fixtures/ledger.ts b/test/fixtures/ledger.ts new file mode 100644 index 00000000..683b25b1 --- /dev/null +++ b/test/fixtures/ledger.ts @@ -0,0 +1,19 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.genesis(), + response: { + active_slots_coefficient: 0.05, + update_quorum: 5, + max_lovelace_supply: '45000000000000000', + network_magic: 764824073, + epoch_length: 432000, + system_start: 1506203091, + slots_per_kes_period: 129600, + slot_length: 1, + max_kes_evolutions: 62, + security_param: 2160, + }, + }, +] as const; diff --git a/test/fixtures/root.ts b/test/fixtures/root.ts new file mode 100644 index 00000000..214fe879 --- /dev/null +++ b/test/fixtures/root.ts @@ -0,0 +1,11 @@ +import { SDK } from '../utils'; + +export default [ + { + command: () => SDK.root(), + response: { + url: 'https://blockfrost.io/', + version: '0.6.0', + }, + }, +] as const; diff --git a/test/setup.ts b/test/setup.ts new file mode 100644 index 00000000..b6ff251a --- /dev/null +++ b/test/setup.ts @@ -0,0 +1,26 @@ +jest.setTimeout(30000); + +expect.extend({ + toBeTypeOrNull(received, classTypeOrNull) { + try { + expect(received).toEqual(expect.any(classTypeOrNull)); + return { + message: () => `Ok`, + pass: true, + }; + } catch (error) { + return received === null + ? { + message: () => `Ok`, + pass: true, + } + : { + message: () => + `expected ${received} to be ${classTypeOrNull} type or null`, + pass: false, + }; + } + }, +}); + +import 'jest-extended'; diff --git a/test/tests/endpoints/index.ts b/test/tests/endpoints/index.ts new file mode 100644 index 00000000..9cc0658a --- /dev/null +++ b/test/tests/endpoints/index.ts @@ -0,0 +1,24 @@ +import fs from 'fs'; +import path from 'path'; + +const fixturesFolder = path.resolve(__dirname, '../../fixtures'); +const files = fs.readdirSync(fixturesFolder); + +interface Fixture { + command: () => any; + response: any; +} + +files.forEach(file => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const fileContent = require(path.resolve(__dirname, '../../fixtures', file)); + + describe(file, () => { + fileContent.default.forEach((fixture: Fixture) => { + test(fixture.command.toString(), async () => { + const response = await fixture.command(); + expect(response).toMatchObject(fixture.response); + }); + }); + }); +}); diff --git a/test/unit/tests/utils.ts b/test/tests/utils.ts similarity index 72% rename from test/unit/tests/utils.ts rename to test/tests/utils.ts index e0f6c1b0..2f936ff1 100644 --- a/test/unit/tests/utils.ts +++ b/test/tests/utils.ts @@ -1,7 +1,7 @@ -import { BlockFrostAPI } from '../../../src/index'; -import * as utils from '../../../src/utils'; +import { BlockFrostAPI } from '../../src/index'; +import * as utils from '../../src/utils'; // eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJson = require('../../../package.json'); +const packageJson = require('../../package.json'); describe('utils', () => { test('no options', () => { @@ -117,6 +117,72 @@ describe('utils', () => { }); }); + test('version', () => { + const api = new BlockFrostAPI({ + projectId: 'xxx', + version: 2, + }); + + expect(api.apiUrl).toEqual('https://cardano-mainnet.blockfrost.io/api/v2'); + }); + + test('version parameter error', () => { + try { + // @ts-ignore + new BlockFrostAPI({}); + } catch (err) { + expect(err.message).toBe('Missing customBackend or projectId option'); + } + }); + + test('version parameter error', () => { + try { + new BlockFrostAPI({ + projectId: 'xxx', + // @ts-ignore + version: 's', + }); + } catch (err) { + expect(err.message).toBe('Param version is not a number'); + } + }); + + test('requestTimeout parameter error', () => { + try { + new BlockFrostAPI({ + projectId: 'xxx', + // @ts-ignore + requestTimeout: 's', + }); + } catch (err) { + expect(err.message).toBe('Param requestTimeout is not a number'); + } + }); + + test('retryDelay parameter error', () => { + try { + new BlockFrostAPI({ + projectId: 'xxx', + // @ts-ignore + retryDelay: 's', + }); + } catch (err) { + expect(err.message).toBe('Param retryDelay is not a number'); + } + }); + + test('retryCount parameter error', () => { + try { + new BlockFrostAPI({ + projectId: 'xxx', + // @ts-ignore + retryCount: 's', + }); + } catch (err) { + expect(err.message).toBe('Param retryCount is not a number'); + } + }); + test('requestTimeout', () => { const api = new BlockFrostAPI({ projectId: 'xxx', diff --git a/test/unit/fixtures/blocks.ts b/test/unit/fixtures/blocks.ts deleted file mode 100644 index a4e5b4a4..00000000 --- a/test/unit/fixtures/blocks.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { SDK } from '../utils'; - -export default [ - { - testName: 'fetch latest block', - command: (): any => - SDK.blocks( - '5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb', - ), - response: { - time: 1506203091, - height: null, - hash: '5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb', - slot: null, - epoch: null, - epoch_slot: null, - slot_leader: 'Genesis slot leader', - size: 0, - tx_count: 14505, - output: '31112484745000000', - fees: '0', - block_vrf: null, - previous_block: null, - next_block: - '89d9b5a5b8ddc8d7e5a6795e9774d97faf1efea59b2caf7eaf9f8c5b32059df4', - confirmations: expect.any(Number), - }, - }, -]; diff --git a/test/unit/tests/endpoints/blocks.ts b/test/unit/tests/endpoints/blocks.ts deleted file mode 100644 index db267fe8..00000000 --- a/test/unit/tests/endpoints/blocks.ts +++ /dev/null @@ -1,10 +0,0 @@ -import fixtures from '../../fixtures/blocks'; - -describe('blocks', () => { - fixtures.forEach(fixture => { - test(fixture.testName, async () => { - const response = await fixture.command(); - expect(response).toMatchObject(fixture.response); - }); - }); -}); diff --git a/test/unit/utils.ts b/test/utils.ts similarity index 69% rename from test/unit/utils.ts rename to test/utils.ts index a5e096cf..1ab21999 100644 --- a/test/unit/utils.ts +++ b/test/utils.ts @@ -1,4 +1,4 @@ -import { BlockFrostAPI } from '../../src'; +import { BlockFrostAPI } from '../src'; const SDK = new BlockFrostAPI({ projectId: `${process.env.PROJECT_ID}`, diff --git a/tsconfig.json b/tsconfig.json index ca65cf39..10464352 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,6 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, - "include": ["src/**/*"], + "include": ["src/**/*", "test/index.d.ts"], "exclude": ["./src/types/OpenApi.ts"] }