From a2f92459ff9611cf9f367ff0886b5ee12c29fd8b Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Wed, 23 Nov 2022 10:19:28 +0100 Subject: [PATCH] move helpers to utils --- .../data-transfer/lib/__tests__/test-utils.ts | 144 ++++++++++++++++++ .../lib/engine/__tests__/engine.test.ts | 144 +----------------- 2 files changed, 150 insertions(+), 138 deletions(-) diff --git a/packages/core/data-transfer/lib/__tests__/test-utils.ts b/packages/core/data-transfer/lib/__tests__/test-utils.ts index b5d4431827..2ac554b843 100644 --- a/packages/core/data-transfer/lib/__tests__/test-utils.ts +++ b/packages/core/data-transfer/lib/__tests__/test-utils.ts @@ -1,4 +1,5 @@ import { Readable } from 'stream'; +import type { ITransferEngine, ISourceProvider, IDestinationProvider } from '../../types'; /** * Collect every entity in a Readable stream @@ -76,3 +77,146 @@ export const createMockedQueryBuilder = (data: { }, }; }); + +export const providerStages = ['bootstrap', 'close']; + +export const sourceStages = [ + ...providerStages, + 'streamEntities', + 'streamLinks', + // 'streamMedia', + 'streamConfiguration', + 'streamSchemas', +]; + +export const destinationStages = [ + ...providerStages, + 'getEntitiesStream', + 'getLinksStream', + // 'getMediaStream', + 'getConfigurationStream', + 'getSchemasStream', +]; + +/** + * Add jest expect helpers + */ +export const extendExpectForDataTransferTests = () => { + expect.extend({ + toBeValidTransferEngine(engine: ITransferEngine) { + try { + expect(engine).toBeDefined(); + + expect(engine.sourceProvider).toBeValidSourceProvider(); + expect(engine.destinationProvider).toBeValidDestinationProvider(); + + // All required methods exists + expect(engine.integrityCheck).toBeInstanceOf(Function); + expect(engine.transfer).toBeInstanceOf(Function); + expect(engine.bootstrap).toBeInstanceOf(Function); + expect(engine.close).toBeInstanceOf(Function); + expect(engine.transferSchemas).toBeInstanceOf(Function); + expect(engine.transferEntities).toBeInstanceOf(Function); + expect(engine.transferLinks).toBeInstanceOf(Function); + expect(engine.transferMedia).toBeInstanceOf(Function); + expect(engine.transferConfiguration).toBeInstanceOf(Function); + expect(engine.close).toBeInstanceOf(Function); + expect(engine.transfer).toBeInstanceOf(Function); + } catch (e) { + return { + pass: false, + message: () => `Expected engine to be valid: ${e.message}`, + }; + } + return { + pass: true, + message: () => `Expected engine not to be valid`, + }; + }, + toHaveSourceStagesCalledTimes(provider: ISourceProvider, times: number) { + const missing = sourceStages.filter((stage) => { + if (provider[stage]) { + try { + // TODO: why is mock.calls an empty array? maybe an async function call that doesn't resolve? + // expect(provider[stage]).toHaveBeenCalledOnce(); + expect(provider[stage].mock.results.length).toEqual(times); + return false; + } catch (e) { + return true; + } + } + }); + + if (missing.length) { + return { + pass: false, + message: () => + `Expected source provider to have stages called ${times} times: ${missing.join(',')}`, + }; + } + return { + pass: true, + message: () => `Expected source provider not to have all stages called`, + }; + }, + toHaveDestinationStagesCalledTimes(provider: IDestinationProvider, times: number) { + const missing = destinationStages.filter((stage) => { + if (provider[stage]) { + try { + // expect(provider[stage]).toHaveBeenCalledOnce(); + expect(provider[stage].mock.results.length).toEqual(times); + return false; + } catch (e) { + return true; + } + } + }); + + if (missing.length) { + return { + pass: false, + message: () => + `Expected destination provider to have stages called ${times} times: ${missing.join( + ',' + )}`, + }; + } + return { + pass: true, + message: () => `Expected destination provider not to have all stages called`, + }; + }, + toBeValidSourceProvider(provider: ISourceProvider) { + try { + expect(provider.getMetadata).toBeDefined(); + expect(provider.type).toEqual('source'); + expect(provider.name.length).toBeGreaterThan(0); + } catch (e) { + return { + pass: false, + message: () => `Expected source provider to be valid: ${e.message}`, + }; + } + return { + pass: true, + message: () => `Expected source provider not to be valid`, + }; + }, + toBeValidDestinationProvider(provider: IDestinationProvider) { + try { + expect(provider.getMetadata).toBeDefined(); + expect(provider.type).toEqual('destination'); + expect(provider.name.length).toBeGreaterThan(0); + } catch (e) { + return { + pass: false, + message: () => `Expected destination provider to be valid: ${e.message}`, + }; + } + return { + pass: true, + message: () => `Expected destination provider not to be valid`, + }; + }, + }); +}; diff --git a/packages/core/data-transfer/lib/engine/__tests__/engine.test.ts b/packages/core/data-transfer/lib/engine/__tests__/engine.test.ts index 2d279220ec..808905a693 100644 --- a/packages/core/data-transfer/lib/engine/__tests__/engine.test.ts +++ b/packages/core/data-transfer/lib/engine/__tests__/engine.test.ts @@ -6,8 +6,11 @@ import { ITransferEngine, ITransferEngineOptions, } from '../../../types'; - -const providerStages = ['bootstrap', 'close']; +import { + extendExpectForDataTransferTests, + providerStages, + sourceStages, +} from '../../__tests__/test-utils'; const getMockSourceStream = (data: Iterable = ['foo', 'bar']) => { const stream = Readable.from(data); @@ -25,142 +28,7 @@ const getMockDestinationStream = () => { return stream; }; -const sourceStages = [ - ...providerStages, - 'streamEntities', - 'streamLinks', - // 'streamMedia', - 'streamConfiguration', - 'streamSchemas', -]; - -const destinationStages = [ - ...providerStages, - 'getEntitiesStream', - 'getLinksStream', - // 'getMediaStream', - 'getConfigurationStream', - 'getSchemasStream', -]; - -// add some helpers to jest -expect.extend({ - toBeValidTransferEngine(engine: ITransferEngine) { - try { - expect(engine).toBeDefined(); - - expect(engine.sourceProvider).toBeValidSourceProvider(); - expect(engine.destinationProvider).toBeValidDestinationProvider(); - - // All required methods exists - expect(engine.integrityCheck).toBeInstanceOf(Function); - expect(engine.transfer).toBeInstanceOf(Function); - expect(engine.bootstrap).toBeInstanceOf(Function); - expect(engine.close).toBeInstanceOf(Function); - expect(engine.transferSchemas).toBeInstanceOf(Function); - expect(engine.transferEntities).toBeInstanceOf(Function); - expect(engine.transferLinks).toBeInstanceOf(Function); - expect(engine.transferMedia).toBeInstanceOf(Function); - expect(engine.transferConfiguration).toBeInstanceOf(Function); - expect(engine.close).toBeInstanceOf(Function); - expect(engine.transfer).toBeInstanceOf(Function); - } catch (e) { - return { - pass: false, - message: () => `Expected engine to be valid: ${e.message}`, - }; - } - return { - pass: true, - message: () => `Expected engine not to be valid`, - }; - }, - toHaveSourceStagesCalledTimes(provider: ISourceProvider, times: number) { - const missing = sourceStages.filter((stage) => { - if (provider[stage]) { - try { - // TODO: why is mock.calls an empty array? maybe an async function call that doesn't resolve? - // expect(provider[stage]).toHaveBeenCalledOnce(); - expect(provider[stage].mock.results.length).toEqual(times); - return false; - } catch (e) { - return true; - } - } - }); - - if (missing.length) { - return { - pass: false, - message: () => - `Expected source provider to have stages called ${times} times: ${missing.join(',')}`, - }; - } - return { - pass: true, - message: () => `Expected source provider not to have all stages called`, - }; - }, - toHaveDestinationStagesCalledTimes(provider: IDestinationProvider, times: number) { - const missing = destinationStages.filter((stage) => { - if (provider[stage]) { - try { - // expect(provider[stage]).toHaveBeenCalledOnce(); - expect(provider[stage].mock.results.length).toEqual(times); - return false; - } catch (e) { - return true; - } - } - }); - - if (missing.length) { - return { - pass: false, - message: () => - `Expected destination provider to have stages called ${times} times: ${missing.join( - ',' - )}`, - }; - } - return { - pass: true, - message: () => `Expected destination provider not to have all stages called`, - }; - }, - toBeValidSourceProvider(provider: ISourceProvider) { - try { - expect(provider.getMetadata).toBeDefined(); - expect(provider.type).toEqual('source'); - expect(provider.name.length).toBeGreaterThan(0); - } catch (e) { - return { - pass: false, - message: () => `Expected source provider to be valid: ${e.message}`, - }; - } - return { - pass: true, - message: () => `Expected source provider not to be valid`, - }; - }, - toBeValidDestinationProvider(provider: IDestinationProvider) { - try { - expect(provider.getMetadata).toBeDefined(); - expect(provider.type).toEqual('destination'); - expect(provider.name.length).toBeGreaterThan(0); - } catch (e) { - return { - pass: false, - message: () => `Expected destination provider to be valid: ${e.message}`, - }; - } - return { - pass: true, - message: () => `Expected destination provider not to be valid`, - }; - }, -}); +extendExpectForDataTransferTests(); const createSource = (streamData?) => { return {