Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Jul 16, 2024
1 parent 944d6a5 commit fa45bd4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
17 changes: 9 additions & 8 deletions tests/mocks/createAPIResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
SearchResponses,
SearchForFacetValuesResponse,
RecommendResponse,
RecommendResponses,
} from 'instantsearch.js';

export const defaultRenderingContent: SearchResponse<any>['renderingContent'] =
Expand Down Expand Up @@ -88,14 +89,6 @@ export const createSFFVResponse = (
...args,
});

export const createRecommendResponse = (
requests: any[]
): { results: Array<RecommendResponse<any>> } => {
return {
results: requests.map(createSingleSearchResponse),
} as unknown as { results: Array<RecommendResponse<any>> };
};

export const createSingleRecommendResponse = (
subset: Partial<RecommendResponse<any>> = {}
): RecommendResponse<any> => {
Expand Down Expand Up @@ -127,3 +120,11 @@ export const createSingleRecommendResponse = (
...rest,
};
};

export const createRecommendResponse = (
requests: any[]
): RecommendResponses<any> => {
return {
results: requests.map(createSingleRecommendResponse),
};
};
25 changes: 4 additions & 21 deletions tests/mocks/createAlgoliaSearchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,10 @@ type OverrideKeys<TTarget, TOptions> = TOptions extends Record<string, never>
? TTarget
: Omit<TTarget, keyof TOptions> & TOptions;

type IfAny<TTest, TFallback> = (0 extends 1 & TTest ? true : false) extends true
? IfAny<TFallback, never>
: TTest;

function ifActualFunction<TTest, TFallback>(
test: TTest,
fallback: TFallback
): IfAny<TTest, TFallback> {
if (typeof test === 'function') {
return test as IfAny<TTest, TFallback>;
}
if (typeof fallback === 'function') {
return fallback as IfAny<TTest, TFallback>;
}
throw new Error('Neither test nor fallback is a function');
}

const algoliasearch = ifActualFunction(
namedConstructor,
defaultConstructor
) as unknown as (appId: string, apiKey: string) => SearchClient;
const algoliasearch = (namedConstructor || defaultConstructor) as unknown as (
appId: string,
apiKey: string
) => SearchClient;

export type MockSearchClient = OverrideKeys<
SearchClient,
Expand Down

0 comments on commit fa45bd4

Please sign in to comment.