import { EmpathyAdapter , EmpathyAdapterBuilder , NextQueriesRequest , NextQueriesResponse , RelatedTagsRequest , RelatedTagsResponse , SearchByIdRequest , SearchByIdResponse , SearchRequest , SearchResponse , SearchAdapter , SuggestionsRequest , SuggestionsResponse , TopRecommendationsRequest , TopRecommendationsResponse , TrackingRequest } from '@empathyco/x-adapter' ; import { noOp } from '../utils/index' ; import { configureAdapterWithToysrus } from './util' ; declare global { interface Window { __mockedAdapter : MockedAdapterConfig ; } } type NonRequestMethods = 'invalidateCache' | 'setConfig' | 'addConfigChangedListener' | 'removeConfigChangedListener' ; export type AdapterFeatures = Omit < SearchAdapter , NonRequestMethods > ; export type AdapterMockedResponses = { [ Method in keyof AdapterFeatures ] : ReturnType < AdapterFeatures [ Method ] > extends Promise < infer Value > ? Value | Error : never ; } ; export interface MockedAdapterConfig { delayMs : number ; responses : Partial < AdapterMockedResponses > ; } window . __mockedAdapter = { delayMs : 100 , responses : { } } ; class E2ETestsAdapter extends EmpathyAdapter { getNextQueries ( request ) { return mockFetch ( request , 'getNextQueries' ) ; } getTopRecommendations ( request ) { return mockFetch ( request , 'getTopRecommendations' ) ; } getSuggestions ( request ) { return mockFetch ( request , 'getSuggestions' ) ; } getRelatedTags ( request ) { return mockFetch ( request , 'getRelatedTags' ) ; } search ( request ) { return mockFetch ( request , 'search' ) ; } track ( request ) { return fetch ( request . url , { method : 'POST' , body : JSON . stringify ( request . params ) } ) . then ( noOp ) ; } searchById ( request ) { return mockFetch ( request , 'searchById' ) ; } } export const mockedAdapter = configureAdapterWithToysrus ( new EmpathyAdapterBuilder ( undefined , undefined , E2ETestsAdapter ) ) . build ( ) ; param request param path returns function mockFetch < Feature extends keyof MockedAdapterConfig [ 'responses' ] > ( request , path ) { return fetch ( `https://api.empathy.co/${ path }` , { method : 'POST' , body : JSON . stringify ( request ) } ) . then ( response => response . json ( ) ) as ReturnType < AdapterFeatures [ Feature ] > ; }