Skip to content

Commit

Permalink
fix use outside jest
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed Jun 30, 2023
1 parent f203961 commit 1b90c76
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 63 deletions.
4 changes: 4 additions & 0 deletions packages/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"import": "./lib/index.js"
}
},
"files": [
"lib",
"src"
],
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --clean -d lib/ && yarn cp:data",
"cp:data": "shx rm -rf lib/__data__ && shx cp -r src/__data__ lib/"
Expand Down
23 changes: 21 additions & 2 deletions packages/test/src/_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import { readFileSync } from 'node:fs';

import { decode } from 'cbor-x';

import { TypeRegistry, Metadata } from '@polkadot/types';
import { TypeRegistry, Metadata, GenericCall } from '@polkadot/types';
import metadataStatic from '@polkadot/types-support/metadata/static-polkadot';
import type { SignedBlock, EventRecord, AccountId } from '@polkadot/types/interfaces';
import type { SignedBlock, EventRecord, Event, AccountId, FunctionMetadataLatest } from '@polkadot/types/interfaces';
import { createSignedBlockExtended } from '@polkadot/api-derive';
import type { TxWithEvent } from '@polkadot/api-derive/types';
import type { CallBase, AnyTuple } from '@polkadot/types-codec/types';

import type { BinBlock } from './_types.js';

Expand All @@ -48,3 +50,20 @@ export function testBlocksFrom(file: string, mds: `0x${string}` = metadataStatic
);
});
}

export const testBlocks = testBlocksFrom('blocks.cbor.bin').slice(0, 3);
export const testHeaders = testBlocks.map(tb => tb.block.header);
export const testExtrinsics = testBlocks.reduce((acc: TxWithEvent[], tb) => acc.concat(tb.extrinsics), []);
export const testEventRecords = testBlocks.reduce((acc: EventRecord[], tb) => acc.concat(tb.events), []);
export const testEvents = testExtrinsics.reduce((acc: Event[], txt) => acc.concat(txt.events), []);
export const testBatchExtrinsic = testExtrinsics[5];
export const testBatchCalls = testBatchExtrinsic.extrinsic.args.reduce((flattedTxWithEvent: GenericCall[], arg) => {
const calls = arg as unknown as CallBase<AnyTuple, FunctionMetadataLatest>[];

const flatted = calls.map(call => {
return new GenericCall(testBatchExtrinsic.extrinsic.registry, call);
});

return flattedTxWithEvent.concat(flatted);
}, []);

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import type { TxWithEvent } from '@polkadot/api-derive/types';
import type { EventRecord, Event } from '@polkadot/types/interfaces';
import metadataStatic from '../__data__/metadata/rococoContracts-hex.js';
import metadataStatic from './__data__/metadata/rococoContracts-hex.js';

import { testBlocksFrom } from '../_blocks.js';
import { testMetadataFrom } from '../_abi.js';
import { testBlocksFrom } from './_blocks.js';
import { testMetadataFrom } from './_abi.js';

export const testContractBlocks = testBlocksFrom('contracts2841323.cbor.bin', metadataStatic);
export const testContractExtrinsics = testContractBlocks.reduce((acc: TxWithEvent[], tb) => acc.concat(tb.extrinsics), []);
Expand Down
1 change: 1 addition & 0 deletions packages/test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export * from './_types.js';
export * from './_blocks.js';
export * from './_abi.js';
export * from './_contracts.js';
export * from './mocks/index.js';
1 change: 0 additions & 1 deletion packages/test/src/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './mock_apiPromise.js';
export * from './mock_apiRx.js';
export * from './mock_contracts.js';
export * from './mock_default.js';
2 changes: 1 addition & 1 deletion packages/test/src/mocks/mock_apiPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const mockPromiseApi = {
},
query: {
contracts: {
contractInfoOf: jest.fn()
contractInfoOf: () => {}
}
}
} as unknown as ApiPromise;
25 changes: 1 addition & 24 deletions packages/test/src/mocks/mock_apiRx.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
/* istanbul ignore file */

import type { TxWithEvent } from '@polkadot/api-derive/types';
import type { EventRecord, Event } from '@polkadot/types/interfaces';
import type { FunctionMetadataLatest } from '@polkadot/types/interfaces';
import type { CallBase, AnyTuple } from '@polkadot/types-codec/types';
import { GenericCall } from '@polkadot/types';

import { BN } from '@polkadot/util';
import { ApiRx } from '@polkadot/api';

import { Observable, from, of } from 'rxjs';

import { testBlocksFrom } from '../_blocks.js';

export const testBlocks = testBlocksFrom('blocks.cbor.bin').slice(0, 3);
export const testHeaders = testBlocks.map(tb => tb.block.header);
export const testExtrinsics = testBlocks.reduce((acc: TxWithEvent[], tb) => acc.concat(tb.extrinsics), []);
export const testEventRecords = testBlocks.reduce((acc: EventRecord[], tb) => acc.concat(tb.events), []);
export const testEvents = testExtrinsics.reduce((acc: Event[], txt) => acc.concat(txt.events), []);
export const testBatchExtrinsic = testExtrinsics[5];
export const testBatchCalls = testBatchExtrinsic.extrinsic.args.reduce((flattedTxWithEvent: GenericCall[], arg) => {
const calls = arg as unknown as CallBase<AnyTuple, FunctionMetadataLatest>[];

const flatted = calls.map(call => {
return new GenericCall(testBatchExtrinsic.extrinsic.registry, call);
});

return flattedTxWithEvent.concat(flatted);
}, []);
import { testBlocks, testEventRecords, testHeaders } from '../_blocks.js';

const apiMock = {
rpc: {
Expand Down
66 changes: 34 additions & 32 deletions packages/test/src/mocks/mock_polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,42 @@
* limitations under the License.
*/

if (typeof jest !== 'undefined') {
// Nested packages cannot be mocked by other means.
// @see https://github.com/jestjs/jest/issues/462
jest.mock('@polkadot/util', () => {
const original = jest.requireActual('@polkadot/util');
jest.mock('@polkadot/util', () => {
const original = jest.requireActual('@polkadot/util');

return {
...original,
logger: jest.fn(() => {
return {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
noop: jest.fn(),
}; })
};
});
return {
...original,
logger: jest.fn(() => {
return {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
noop: jest.fn(),
}; })
};
});

jest.mock('@polkadot/api', () => {
const original = jest.requireActual('@polkadot/api');
jest.mock('@polkadot/api', () => {
const original = jest.requireActual('@polkadot/api');

return {
...original,
WsProvider: jest.fn(() => {
return {
hasSubscriptions: jest.fn(() => {
return true;
}),
on: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
send: jest.fn(),
subscribe: jest.fn(),
unsubscribe: jest.fn()
}; })
};
});
return {
...original,
WsProvider: jest.fn(() => {
return {
hasSubscriptions: jest.fn(() => {
return true;
}),
on: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
send: jest.fn(),
subscribe: jest.fn(),
unsubscribe: jest.fn()
}; })
};
});
}

0 comments on commit 1b90c76

Please sign in to comment.