Skip to content

Commit

Permalink
test: creating the TrendingFamous
Browse files Browse the repository at this point in the history
  • Loading branch information
steniowagner committed Nov 1, 2023
1 parent 8da21ba commit c939b8e
Show file tree
Hide file tree
Showing 7 changed files with 770 additions and 10 deletions.
1 change: 1 addition & 0 deletions __mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './utils';
export { MockedNavigator } from './MockedNavigator';
export * from './news';
export * from './quiz-questions';
export * from './trending-famous';
157 changes: 157 additions & 0 deletions __mocks__/trending-famous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { GraphQLError } from 'graphql';

import { QUERY_TRENDING_FAMOUS } from '@/components/stacks/famous/screens/trending-famous/use-trending-famous';
import { DEFAULT_HEIGHT } from '@/components/stacks/famous/screens/trending-famous/components/trending-famous-list-item/TrendingFamousListItem.styles';
import { QueryTrendingFamous_trendingFamous_items } from '@/types/schema';
import metrics from '@styles/metrics';

type Errors = 'network' | 'graphql';

export const TRENDING_FAMOUS_ITEMS_PER_PAGE = Math.floor(
metrics.height / DEFAULT_HEIGHT,
);

export const trendingFamousList = (page: number = 1) =>
Array(TRENDING_FAMOUS_ITEMS_PER_PAGE)
.fill({})
.map((_, index) => ({
profilePath: `page${page}-profilePath-${index}`,
name: `page${page}-name-${index}-${page}`,
id: page * TRENDING_FAMOUS_ITEMS_PER_PAGE + index,
__typename: 'TrendingFamousItem',
})) as QueryTrendingFamous_trendingFamous_items[];

type BaseMockNewsQueryResponseParams = {
items: QueryTrendingFamous_trendingFamous_items[];
page: number;
hasMore: boolean;
};

const baseMockTrendingFamousQueryResponse = (
params: BaseMockNewsQueryResponseParams,
) => {
const request = {
request: {
query: QUERY_TRENDING_FAMOUS,
variables: {
page: params.page,
},
},
};
const result = {
result: {
data: {
trendingFamous: {
hasMore: params.hasMore,
items: params.items,
},
},
},
};

const responseWithNetworkError = {
...request,
error: new Error('A Network error occurred'),
};

const responseWithGraphQLError = {
...request,
errors: [new GraphQLError('A GraphQL error occurred')],
};

return {
responseWithGraphQLError,
responseWithNetworkError,
request,
result,
};
};

export const mockTrendingFamousEntryQuerySuccessResponse = (
hasMore = true,
withEmptyResponse = false,
) => {
const entryQueryResult = baseMockTrendingFamousQueryResponse({
items: withEmptyResponse ? [] : trendingFamousList(),
page: 1,
hasMore,
});
return [
{
...entryQueryResult.request,
...entryQueryResult.result,
},
];
};

export const mockTrendingFamousEntryQueryErrorResponse = (error: Errors) => {
const entryQueryResult = baseMockTrendingFamousQueryResponse({
hasMore: false,
page: 1,
items: [],
});
const errorResponse =
error === 'network'
? entryQueryResult.responseWithNetworkError
: entryQueryResult.responseWithGraphQLError;
return [
{
...entryQueryResult.request,
...errorResponse,
},
];
};

export const mockTrendingFamousPaginationQuerySuccessResponse = (
hasMore = true,
) => {
const entryQueryResult = baseMockTrendingFamousQueryResponse({
items: trendingFamousList(),
page: 1,
hasMore: true,
});
const paginationQueryResult = baseMockTrendingFamousQueryResponse({
items: trendingFamousList(2),
page: 2,
hasMore,
});
return [
{
...entryQueryResult.request,
...entryQueryResult.result,
},
{
...paginationQueryResult.request,
...paginationQueryResult.result,
},
];
};

export const mockTrendingFamousPaginationQueryErrorResponse = (
error: Errors,
) => {
const entryQueryResult = baseMockTrendingFamousQueryResponse({
items: trendingFamousList(),
page: 1,
hasMore: true,
});
const paginationQueryResult = baseMockTrendingFamousQueryResponse({
items: trendingFamousList(),
page: 2,
hasMore: false,
});
const errorResponse =
error === 'network'
? paginationQueryResult.responseWithNetworkError
: paginationQueryResult.responseWithGraphQLError;
return [
{
...entryQueryResult.request,
...entryQueryResult.result,
},
{
...errorResponse.request,
...errorResponse,
},
];
};
3 changes: 2 additions & 1 deletion src/components/stacks/famous/routes/route-params-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { StackNavigationProp } from '@react-navigation/stack';

import { QueryTrendingFamous_trendingFamous_items } from '@schema-types';
import { Routes } from '@navigation';

export type FamousStackRoutes = {
[Routes.Famous.TRENDING_FAMOUS]: undefined;
[Routes.Famous.DETAILS]: undefined;
[Routes.Famous.DETAILS]: QueryTrendingFamous_trendingFamous_items;
[Routes.Famous.SEARCH]: undefined;
};

Expand Down
Loading

0 comments on commit c939b8e

Please sign in to comment.