Skip to content

Commit f0b5d96

Browse files
committed
feat: add new unit tests
1 parent d522202 commit f0b5d96

6 files changed

Lines changed: 404 additions & 16 deletions

File tree

src/components/BuyProcess/EcoAIPlanSelection/Packs/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const ecoAIFieldNames = {
88
quantity: 'packagesQty',
99
};
1010

11-
export const isInvalidInputValue = (values, index) =>
12-
values[`${ecoAIFieldNames.quantity}`] < 0 ||
13-
values[`${ecoAIFieldNames.quantity}`] > MAX_ECOAI_PACKAGE;
11+
export const isInvalidInputValue = (values, index) => {
12+
return values[`${ecoAIFieldNames.packages}`][index][`${ecoAIFieldNames.quantity}`] < 0 ||
13+
values[`${ecoAIFieldNames.packages}`][index][`${ecoAIFieldNames.quantity}`] > MAX_ECOAI_PACKAGE;
14+
}
1415

1516
export const isGreatherToMax = (packsSet) =>
1617
packsSet.findIndex((pack) => pack[ecoAIFieldNames.quantity] > MAX_ECOAI_PACKAGE) > -1;
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
import { render, screen, waitFor, act } from '@testing-library/react';
2+
import '@testing-library/jest-dom/extend-expect';
3+
import userEvent from '@testing-library/user-event';
4+
import {
5+
ButtonLess,
6+
ButtonMore,
7+
DeleteLandingPacksButton,
8+
DeletePacksButton,
9+
Packs,
10+
filterPackagesEqualOrGreatherToZero,
11+
isGreatherToMax,
12+
isInvalidInputValue,
13+
isLessToMin,
14+
} from '.';
15+
import * as Formik from 'formik';
16+
import { MAX_ECOAI_PACKAGE } from '../../../../doppler-types';
17+
import IntlProvider from '../../../../i18n/DopplerIntlProvider.double-with-ids-as-values';
18+
19+
describe('Packs', () => {
20+
it('should to execute handleSave when input value is changed', async () => {
21+
// Arrange
22+
const packs = [
23+
{
24+
packages: 1,
25+
packagesQty: 0,
26+
},
27+
];
28+
const useFormikContextMock = jest.spyOn(Formik, 'useFormikContext');
29+
useFormikContextMock.mockReturnValue({
30+
values: {
31+
packages: packs,
32+
},
33+
setFieldValue: () => null,
34+
});
35+
36+
const handleSave = jest.fn();
37+
render(
38+
<IntlProvider>
39+
<Packs
40+
packs={packs}
41+
handleSave={handleSave}
42+
formRef={() => null}
43+
showRemoveLandings={true}
44+
/>
45+
</IntlProvider>,
46+
);
47+
expect(handleSave).not.toHaveBeenCalled();
48+
const user = userEvent.setup();
49+
50+
// Choose 3 packs of 5 landings
51+
await act(() =>
52+
user.clear(screen.getByRole('spinbutton', { name: /packages.0.packagesQty/i })),
53+
);
54+
await act(() =>
55+
user.type(screen.getByRole('spinbutton', { name: /packages.0.packagesQty/i }), '1'),
56+
);
57+
expect(screen.getByRole('spinbutton', { name: /packages.0.packagesQty/i })).toHaveValue(
58+
1,
59+
);
60+
screen.getByRole('button', { name: /ai_agent_selection.remove_from_cart_button/i });
61+
await waitFor(() => {
62+
expect(handleSave).toHaveBeenCalledWith([
63+
{
64+
packages: 1,
65+
packagesQty: 1,
66+
},
67+
]);
68+
});
69+
});
70+
71+
describe('isInvalidInputValue function', () => {
72+
it('should return "true" when is < 0 or is > allowed limit ', async () => {
73+
// Arrange
74+
const index = 0;
75+
76+
// case < 0
77+
let invalidPackageQty = isInvalidInputValue(
78+
{
79+
packages: [
80+
{
81+
packagesQty: -1,
82+
},
83+
],
84+
},
85+
index,
86+
);
87+
88+
console.log(invalidPackageQty);
89+
90+
expect(invalidPackageQty).toBeTruthy();
91+
92+
// case > MAX_ECOAI_PACKAGE
93+
invalidPackageQty = isInvalidInputValue(
94+
{
95+
packages: [
96+
{
97+
packagesQty: MAX_ECOAI_PACKAGE + 1,
98+
},
99+
],
100+
},
101+
index,
102+
);
103+
expect(invalidPackageQty).toBeTruthy();
104+
});
105+
106+
it('should return "false" when is >= 0 or is <= allowed limit ', async () => {
107+
// Arrange
108+
const index = 0;
109+
110+
// case >= 0 and <= MAX_ECOAI_PACKAGE
111+
let invalidPackageQty = isInvalidInputValue(
112+
{
113+
packages: [
114+
{
115+
packagesQty: 1,
116+
},
117+
],
118+
},
119+
index,
120+
);
121+
expect(invalidPackageQty).toBeFalsy();
122+
});
123+
});
124+
125+
describe('isGreatherToMax function', () => {
126+
it('should return "true" when there is a pack greather to allowed limit', async () => {
127+
// Arrange
128+
const packsSet = [
129+
{
130+
packagesQty: 1,
131+
},
132+
{
133+
packagesQty: MAX_ECOAI_PACKAGE + 1,
134+
},
135+
];
136+
137+
const finded = isGreatherToMax(packsSet);
138+
expect(finded).toBeTruthy();
139+
});
140+
141+
it('should return "false" when there is not a pack greather to allowed limit', async () => {
142+
// Arrange
143+
const packsSet = [
144+
{
145+
packagesQty: 1,
146+
},
147+
{
148+
packagesQty: MAX_ECOAI_PACKAGE,
149+
},
150+
];
151+
152+
const finded = isGreatherToMax(packsSet);
153+
expect(finded).not.toBeTruthy();
154+
});
155+
});
156+
157+
describe('isLessToMin function', () => {
158+
it('should return "true" when there is a pack less to 0', async () => {
159+
// Arrange
160+
const packsSet = [
161+
{
162+
packagesQty: -2,
163+
},
164+
];
165+
166+
const finded = isLessToMin(packsSet);
167+
expect(finded).toBeTruthy();
168+
});
169+
170+
it('should return "false" when there is not a pack less to 0', async () => {
171+
// Arrange
172+
const packsSet = [
173+
{
174+
packagesQty: 0,
175+
},
176+
];
177+
178+
const finded = isLessToMin(packsSet);
179+
expect(finded).not.toBeTruthy();
180+
});
181+
});
182+
183+
184+
describe('filterPackagesEqualOrGreatherToZero function', () => {
185+
it('should return just the landings packs > 0', async () => {
186+
// Arrange
187+
const packsSet = [
188+
{
189+
packagesQty: 0,
190+
},
191+
{
192+
packagesQty: 1,
193+
},
194+
];
195+
196+
const landingPacksFiltered = filterPackagesEqualOrGreatherToZero(packsSet);
197+
expect(landingPacksFiltered).toEqual([
198+
{
199+
packagesQty: 1,
200+
},
201+
]);
202+
});
203+
});
204+
205+
206+
describe('ButtonLess', () => {
207+
const useFormikContextMock = jest.spyOn(Formik, 'useFormikContext');
208+
209+
beforeEach(() => {
210+
useFormikContextMock.mockReturnValue({
211+
values: {
212+
packages: [
213+
{
214+
packagesQty: 1,
215+
},
216+
],
217+
},
218+
setFieldValue: () => null,
219+
});
220+
});
221+
222+
it('should render ButtonLess component', async () => {
223+
const handleInputValue = jest.fn();
224+
expect(handleInputValue).not.toHaveBeenCalled();
225+
render(<ButtonLess handleInputValue={handleInputValue} index={0} />);
226+
const user = userEvent.setup();
227+
228+
await user.click(screen.getByRole('button', { name: /button less/i }));
229+
230+
await waitFor(() => expect(handleInputValue).toHaveBeenCalled());
231+
});
232+
});
233+
234+
describe('ButtonMore', () => {
235+
const useFormikContextMock = jest.spyOn(Formik, 'useFormikContext');
236+
237+
beforeEach(() => {
238+
useFormikContextMock.mockReturnValue({
239+
values: {
240+
packages: [
241+
{
242+
packagesQty: 2,
243+
},
244+
],
245+
},
246+
setFieldValue: () => null,
247+
});
248+
});
249+
250+
it('should render ButtonMore component', async () => {
251+
const handleInputValue = jest.fn();
252+
expect(handleInputValue).not.toHaveBeenCalled();
253+
render(<ButtonMore handleInputValue={handleInputValue} index={0} />);
254+
const user = userEvent.setup();
255+
256+
await user.click(screen.getByRole('button', { name: /button more/i }));
257+
258+
await waitFor(() => expect(handleInputValue).toHaveBeenCalled());
259+
});
260+
});
261+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import '@testing-library/jest-dom/extend-expect';
2+
import { render, screen } from '@testing-library/react';
3+
import IntlProvider from '../../../../i18n/DopplerIntlProvider.double-with-ids-as-values';
4+
import { PlanInformation } from '.';
5+
6+
describe('PlanInformation', () => {
7+
it('should render PlanInformation', async () => {
8+
// Act
9+
render(
10+
<IntlProvider>
11+
<PlanInformation />
12+
</IntlProvider>,
13+
);
14+
15+
// Assert
16+
screen.getByText('ai_agent_selection.ai_agent_plan_info.legend');
17+
screen.getByText('ai_agent_selection.ai_agent_plan_info.section_1.title');
18+
screen.getByText('ai_agent_selection.ai_agent_plan_info.section_1.legend');
19+
screen.getByText('ai_agent_selection.ai_agent_plan_info.section_2.title');
20+
screen.getByText('ai_agent_selection.ai_agent_plan_info.section_2.legend');
21+
});
22+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
2+
import { PLAN_TYPE } from '../../../doppler-types';
3+
import {
4+
fakeAccountPlanDiscounts,
5+
fakePlan,
6+
} from '../../../services/doppler-account-plans-api-client.double';
7+
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
8+
import { EcoAIPlanSelection } from '.';
9+
import { AppServicesProvider } from '../../../services/pure-di';
10+
import IntlProvider from '../../../i18n/DopplerIntlProvider.double-with-ids-as-values';
11+
import '@testing-library/jest-dom/extend-expect';
12+
13+
describe('EcoAIPlansSelection component', () => {
14+
it('should render EcoAIPlansSelection component', async () => {
15+
// Arrange
16+
const selectedPlan = 1;
17+
18+
const forcedServices = {
19+
appSessionRef: {
20+
current: {
21+
userData: {
22+
user: {
23+
addOnPromotions: [],
24+
plan: {
25+
idPlan: 3,
26+
planType: PLAN_TYPE.free,
27+
},
28+
},
29+
},
30+
},
31+
},
32+
dopplerAccountPlansApiClient: {
33+
getPlanData: async (selectedPlan) => ({ success: true, value: fakePlan }),
34+
getDiscountsData: async () => ({ success: true, value: fakeAccountPlanDiscounts }),
35+
},
36+
};
37+
// Act
38+
render(
39+
<AppServicesProvider forcedServices={forcedServices}>
40+
<IntlProvider>
41+
<Router initialEntries={[`/buy-ecoia-plan?buyType=6`]}>
42+
<Routes>
43+
<Route path="/buy-ecoia-plan" element={<EcoAIPlanSelection />} />
44+
</Routes>
45+
</Router>
46+
</IntlProvider>
47+
</AppServicesProvider>,
48+
);
49+
50+
// Assert
51+
const loader = screen.getByTestId('wrapper-loading');
52+
await waitForElementToBeRemoved(loader);
53+
54+
screen.getByText('ai_agent_selection.title');
55+
screen.getByText('ai_agent_selection.ai_agent_plan_info.legend');
56+
});
57+
});

0 commit comments

Comments
 (0)