Skip to content

Commit 0d60c66

Browse files
dmitrycnstrcGull-Devv-i-s-h-n-u-ps
authored
[QUIZ-723]: subtask tests for rest of the files (#180)
* useCioClient test * useConsoleErrors & refactoring * useQueryParams test * useShareResultsLink test * more hook tests * fix * fix linter * added more tests * add useSessionStorageState test * usePrimaryColorStyles test * useCoverQuestionProps * useNextQuestionButtonProps * useAddToFavoritesButtonProps * more tests * small fix * utils * move utils * add constatnts & fixes * add constatnts & fixes * add more test * added tests for jsx files * added coverage * added jest-fail-on-console * removed consoling in terminal * share modal tests * share modal tests * added more tests * added more tests * added more test for utils * removed unwanted packages * removed ununsed helper fn * avoid using act in jsx tests * fix * fix * remove launch * add testcase * add more testcases * move test hooks * small refactoring * small refactoring --------- Co-authored-by: Gull-Dev <[email protected]> Co-authored-by: vishnu <[email protected]>
1 parent 699a88d commit 0d60c66

File tree

114 files changed

+5490
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+5490
-18
lines changed

jest.setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
import '@testing-library/jest-dom';
22
import '@testing-library/jest-dom/jest-globals';
3+
import failOnConsole from 'jest-fail-on-console'
4+
5+
failOnConsole()
6+
global.TextEncoder = require('util').TextEncoder;
7+
global.TextDecoder = require('util').TextDecoder;

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@
9898
"eslint-plugin-react-hooks": "^4.6.0",
9999
"eslint-plugin-storybook": "^0.6.15",
100100
"eslint-plugin-testing-library": "^6.2.0",
101+
"fishery": "^2.2.2",
101102
"husky": "^8.0.1",
102103
"jest": "^29.7.0",
103104
"jest-environment-jsdom": "^29.7.0",
105+
"jest-fail-on-console": "^3.2.0",
104106
"license-checker": "^25.0.1",
105107
"react": "^18.2.0",
106108
"react-dom": "^18.2.0",

spec/__tests__/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ export const defaultContextMocks = {
2323
};
2424

2525
export const DEMO_API_KEY = 'DEMO_API_KEY';
26+
export const QUIZ_SESSION_ID = 'QUIZ_SESSION_ID';
27+
export const QUIZ_SESSION_NAME = 'QUIZ_SESSION_NAME';
28+
export const QUIZ_API_KEY = 'QUIZ_API_KEY';
29+
export const QUIZ_ID = 'QUIZ_ID';
30+
export const QUIZ_VERSION_ID = 'QUIZ_VERSION_ID';
31+
export const QUIZ_SESSION_KEY = 'QUIZ_SESSION_KEY';

spec/__tests__/factories/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './quiz-results';
2+
export * from './questions';

spec/__tests__/factories/questions.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Factory } from 'fishery';
2+
import {
3+
CoverQuestion,
4+
OpenQuestion,
5+
SelectQuestion,
6+
QuestionOption,
7+
QuestionImages,
8+
} from '@constructor-io/constructorio-client-javascript';
9+
10+
export const images = Factory.define<QuestionImages>(() => ({
11+
primary_alt: 'Primary Alt',
12+
primary_url: 'Primary URL',
13+
secondary_alt: 'Secondary Alt',
14+
secondary_url: 'Secondary URL',
15+
}));
16+
17+
export const selectOption = Factory.define<QuestionOption>(({ sequence }) => ({
18+
attribute: null,
19+
id: sequence,
20+
value: 'VALUE',
21+
}));
22+
23+
export const coverQuestion = Factory.define<CoverQuestion>(() => ({
24+
title: 'Title',
25+
description: 'Description',
26+
cta_text: 'CTA Text',
27+
id: 1,
28+
type: 'cover',
29+
images: null,
30+
}));
31+
32+
export const openQuestion = Factory.define<OpenQuestion>(() => ({
33+
title: 'Title',
34+
description: 'Description',
35+
cta_text: 'CTA Text',
36+
id: 1,
37+
type: 'open',
38+
images: null,
39+
}));
40+
41+
export const selectQuestion = Factory.define<SelectQuestion>(() => ({
42+
title: 'Title',
43+
description: 'Description',
44+
cta_text: 'CTA Text',
45+
id: 1,
46+
type: 'single',
47+
options: selectOption.buildList(2),
48+
images: null,
49+
}));
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { Factory } from 'fishery';
2+
import {
3+
BrowseResultData,
4+
QuizResultDataPartial,
5+
QuizResultsConfig,
6+
QuizResultsResponse,
7+
ResultCardOptions,
8+
ResultsPageOptions,
9+
} from '../../../src/types';
10+
11+
export const quizResult = Factory.define<QuizResultDataPartial>(({ sequence }) => ({
12+
matched_terms: [],
13+
data: {
14+
id: `id-${sequence}`,
15+
image_url: 'https://example.com/image.jpg',
16+
rating_count: 2_123,
17+
rating_score: 4,
18+
regular_price: 100,
19+
sale_price: 80,
20+
},
21+
is_slotted: false,
22+
labels: [],
23+
value: 'VALUE',
24+
variations: [],
25+
}));
26+
27+
export const browseResult = Factory.define<BrowseResultData>(({ sequence }) => ({
28+
matched_terms: [],
29+
data: {
30+
id: `id-${sequence}`,
31+
image_url: 'https://example.com/image.jpg',
32+
rating_count: 2_123,
33+
rating_score: 4,
34+
regular_price: 100,
35+
sale_price: 80,
36+
},
37+
is_slotted: false,
38+
labels: [],
39+
value: 'VALUE',
40+
variations: [],
41+
variations_map: [],
42+
}));
43+
44+
export const quizResults = Factory.define<QuizResultsResponse>(() => ({
45+
request: {
46+
fmt_options: {},
47+
num_results_per_page: 10,
48+
page: 1,
49+
section: 'section',
50+
sort_by: 'sort_by',
51+
sort_order: 'asc',
52+
features: {
53+
query_items: false,
54+
auto_generated_refined_query_rules: false,
55+
manual_searchandizing: false,
56+
personalization: false,
57+
filter_items: false,
58+
},
59+
feature_variants: {},
60+
collection_filter_expression: {
61+
value: 'VALUE',
62+
name: 'NAME',
63+
},
64+
term: 'TERM',
65+
},
66+
response: {
67+
result_sources: {
68+
query_items: false,
69+
auto_generated_refined_query_rules: false,
70+
manual_searchandizing: false,
71+
personalization: false,
72+
filter_items: false,
73+
},
74+
facets: [],
75+
groups: [],
76+
results: [quizResult.build()],
77+
sort_options: [],
78+
refined_content: [],
79+
total_num_results: 1,
80+
features: [],
81+
},
82+
result_id: 'result_id',
83+
quiz_version_id: 'quiz_version_id',
84+
quiz_session_id: 'quiz_session_id',
85+
quiz_id: 'quiz_id',
86+
quiz_selected_options: [],
87+
}));
88+
89+
export const resultCardOptions = Factory.define<ResultCardOptions>(() => ({
90+
renderResultCardPriceDetails: jest.fn(),
91+
resultCardRatingCountKey: 'rating_count',
92+
resultCardRatingScoreKey: 'rating_score',
93+
resultCardRegularPriceKey: 'regular_price',
94+
resultCardSalePriceKey: 'sale_price',
95+
resultCardPosition: 0,
96+
}));
97+
98+
export const resultsPageOptions = Factory.define<ResultsPageOptions>(() => ({
99+
favoriteItems: [],
100+
numResultsToDisplay: 10,
101+
requestConfigs: {},
102+
showShareResultsButton: true,
103+
}));
104+
105+
export const quizResultResponseSummary = Factory.define<
106+
QuizResultsConfig['desktop']['response_summary']
107+
>(() => ({
108+
is_active: true,
109+
text: 'Desktop response summary',
110+
items_separator: ', ',
111+
last_separator: ' and ',
112+
}));
113+
114+
export const quizResultsConfig = Factory.define<QuizResultsConfig>(() => ({
115+
desktop: {
116+
description: {
117+
is_active: true,
118+
text: 'Desktop description',
119+
},
120+
title: {
121+
is_active: true,
122+
text: 'Desktop title',
123+
},
124+
response_summary: null,
125+
},
126+
}));

spec/__tests__/utils.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,9 @@ class MockConstructorIO extends ConstructorIO {
1616
}
1717

1818
export const mockConstructorIOClient =
19-
typeof window !== 'undefined' ? new MockConstructorIO({ apiKey: DEMO_API_KEY }) : undefined;
20-
21-
export function withContextWrapper({
22-
contextMocks = {},
23-
}: {
24-
contextMocks?: Partial<QuizContextValue>;
25-
}) {
26-
const value = { cioClient: mockConstructorIOClient, ...defaultContextMocks, ...contextMocks };
27-
function ContextWrapper({ children }: { children: React.ReactNode }) {
28-
return <QuizContext.Provider value={value}>{children}</QuizContext.Provider>;
29-
}
30-
return ContextWrapper;
31-
}
19+
typeof window !== 'undefined'
20+
? new MockConstructorIO({ apiKey: DEMO_API_KEY })
21+
: (undefined as unknown as ConstructorIO);
3222

3323
export function withContext<Props extends {}>(
3424
Component: React.ComponentType<Props>,

0 commit comments

Comments
 (0)