Skip to content

Commit e490fdb

Browse files
Merge pull request #465 from primeroIMS/trigyn-testcases-record-form-nav
Trigyn testcases record form nav
2 parents 59d9fcd + 3dec094 commit e490fdb

File tree

4 files changed

+48
-230
lines changed

4 files changed

+48
-230
lines changed

app/javascript/components/record-form/nav/component.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const Component = ({
205205
classes={drawerClasses}
206206
>
207207
<CloseButtonNavBar handleToggleNav={handleToggleNav} mobileDisplay={mobileDisplay} />
208-
<List className={css.listRecordNav}>
208+
<List data-testid="nav-list" className={css.listRecordNav}>
209209
<RecordInformation
210210
handleClick={handleClick}
211211
open={open}

app/javascript/components/record-form/nav/component.unit.test.js app/javascript/components/record-form/nav/component.spec.js

+45-228
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
22

33
import { fromJS, Map, OrderedMap } from "immutable";
4-
import Divider from "@material-ui/core/Divider";
5-
import CloseIcon from "@material-ui/icons/Close";
64

7-
import { APPROVALS, INCIDENT_FROM_CASE, REFERRAL, RECORD_INFORMATION_GROUP } from "../../../config";
8-
import { setupMountedComponent } from "../../../test";
5+
import { APPROVALS, REFERRAL } from "../../../config";
6+
import { fireEvent, mountedComponent, screen, setScreenSizeToMobile, waitFor } from "../../../test-utils";
97
import { FormSectionRecord, FieldRecord } from "../records";
10-
import { ConditionalWrapper } from "../../../libs";
118
import Actions from "../actions";
129

13-
import { NavGroup, RecordInformation } from "./components";
1410
import Nav from "./component";
1511

1612
describe("<Nav />", () => {
17-
let component;
18-
1913
const record = fromJS({
2014
case_id: "12345",
2115
case_id_display: "3c9d076",
@@ -186,59 +180,18 @@ describe("<Nav />", () => {
186180
hasForms: true
187181
};
188182

189-
beforeEach(() => {
190-
({ component } = setupMountedComponent(Nav, props, initialState));
183+
beforeAll(() => {
184+
setScreenSizeToMobile(false);
191185
});
192186

193187
it("renders a CloseIcon component />", () => {
194-
expect(component.find(CloseIcon)).to.have.lengthOf(1);
188+
mountedComponent(<Nav {...props} />, initialState);
189+
expect(screen.getByTestId("close-icon")).toBeInTheDocument();
195190
});
196191

197192
it("renders a Nav component />", () => {
198-
expect(component.find(Nav)).to.have.lengthOf(1);
199-
});
200-
201-
it("renders a RecordInformation component />", () => {
202-
expect(component.find(RecordInformation)).to.have.lengthOf(1);
203-
});
204-
205-
it("renders a Divider component />", () => {
206-
expect(component.find(Divider)).to.have.lengthOf(1);
207-
});
208-
209-
it("renders a NavGroup component from record information and another one from the others forms groups />", () => {
210-
expect(component.find(NavGroup)).to.have.lengthOf(2);
211-
});
212-
213-
it("renders the NavGroup component for record information open", () => {
214-
expect(component.find(NavGroup).first().props().open).to.equal("record_information");
215-
});
216-
217-
it("renders a ConditionalWrapper />", () => {
218-
expect(component.find(ConditionalWrapper)).to.have.lengthOf(1);
219-
});
220-
221-
it("should render valid props", () => {
222-
const navProps = { ...component.find(Nav).props() };
223-
224-
expect(component.find(Nav)).to.have.lengthOf(1);
225-
[
226-
"firstTab",
227-
"formNav",
228-
"handleToggleNav",
229-
"isNew",
230-
"mobileDisplay",
231-
"primeroModule",
232-
"recordType",
233-
"selectedForm",
234-
"selectedRecord",
235-
"toggleNav",
236-
"hasForms"
237-
].forEach(property => {
238-
expect(navProps).to.have.property(property);
239-
delete navProps[property];
240-
});
241-
expect(navProps).to.be.empty;
193+
mountedComponent(<Nav {...props} />, initialState);
194+
expect(screen.getByTestId("nav-list")).toBeInTheDocument();
242195
});
243196

244197
describe("when is a new record", () => {
@@ -250,46 +203,9 @@ describe("<Nav />", () => {
250203
isNew: true
251204
};
252205

253-
it("sets the firstTab as selectedForm", () => {
254-
const { component: newComp } = setupMountedComponent(Nav, notSelectedProps, initialState);
255-
256-
const expectedAction = {
257-
type: Actions.SET_SELECTED_FORM,
258-
payload: firstTab.unique_id
259-
};
260-
261-
const setAction = newComp
262-
.props()
263-
.store.getActions()
264-
.find(action => action.type === Actions.SET_SELECTED_FORM);
265-
266-
expect(setAction).to.deep.equal(expectedAction);
267-
});
268-
269-
it("opens the form_group_id of the firstTab", () => {
270-
const { component: newComp } = setupMountedComponent(Nav, notSelectedProps, initialState);
271-
const navGroup = newComp.find(NavGroup).first();
272-
273-
expect(navGroup.props().open).to.equal(firstTab.form_group_id);
274-
});
275-
276206
it("opens the selectedForm and group", () => {
277-
const { component: newComp } = setupMountedComponent(
278-
Nav,
279-
{ ...notSelectedProps, selectedForm: APPROVALS },
280-
initialState
281-
);
282-
283-
const setAction = newComp
284-
.props()
285-
.store.getActions()
286-
.find(action => action.type === Actions.SET_SELECTED_FORM);
287-
288-
expect(setAction).to.not.exist;
289-
290-
const navGroup = newComp.find(NavGroup).first();
291-
292-
expect(navGroup.props().open).to.equal(RECORD_INFORMATION_GROUP);
207+
mountedComponent(<Nav {...{ ...notSelectedProps, selectedForm: APPROVALS }} />, initialState);
208+
expect(screen.getByText("forms.record_types.record_information", { expanded: true })).toBeInTheDocument();
293209
});
294210
});
295211

@@ -303,28 +219,9 @@ describe("<Nav />", () => {
303219
selectedForm: ""
304220
};
305221

306-
beforeEach(() => {
307-
({ component } = setupMountedComponent(Nav, notSelectedProps, initialState));
308-
});
309-
310-
it("sets the firstTab as selectedForm", () => {
311-
const expectedAction = {
312-
type: Actions.SET_SELECTED_FORM,
313-
payload: "basic_identity"
314-
};
315-
316-
const setAction = component
317-
.props()
318-
.store.getActions()
319-
.find(action => action.type === Actions.SET_SELECTED_FORM);
320-
321-
expect(setAction).to.deep.equal(expectedAction);
322-
});
323-
324222
it("opens the form_group_id of the firstTab", () => {
325-
const navGroup = component.find(NavGroup).first();
326-
327-
expect(navGroup.props().open).to.equal(firstTab.form_group_id);
223+
mountedComponent(<Nav {...notSelectedProps} />, initialState);
224+
expect(screen.getByText("Basic Identity", { expanded: true })).toBeInTheDocument();
328225
});
329226
});
330227

@@ -337,23 +234,15 @@ describe("<Nav />", () => {
337234
selectedForm: REFERRAL
338235
};
339236

340-
beforeEach(() => {
341-
({ component } = setupMountedComponent(Nav, notSelectedProps, initialState));
342-
});
343-
344237
it("should not select a different form", () => {
345-
const setAction = component
346-
.props()
347-
.store.getActions()
348-
.find(action => action.type === Actions.SET_SELECTED_FORM);
238+
const { store } = mountedComponent(<Nav {...notSelectedProps} />, initialState);
349239

350-
expect(setAction).to.not.exist;
240+
expect(store.getActions().find(action => action.type === Actions.SET_SELECTED_FORM)).toBeUndefined();
351241
});
352242

353243
it("opens the record_information group if it belongs to that group", () => {
354-
const navGroup = component.find(NavGroup).first();
355-
356-
expect(navGroup.props().open).to.equal(RECORD_INFORMATION_GROUP);
244+
mountedComponent(<Nav {...notSelectedProps} />, initialState);
245+
expect(screen.getByText("forms.record_types.record_information", { expanded: true })).toBeInTheDocument();
357246
});
358247
});
359248

@@ -366,28 +255,9 @@ describe("<Nav />", () => {
366255
selectedForm: "unknown_form"
367256
};
368257

369-
beforeEach(() => {
370-
({ component } = setupMountedComponent(Nav, notSelectedProps, initialState));
371-
});
372-
373-
it("sets the firstTab as selectedForm", () => {
374-
const expectedAction = {
375-
type: Actions.SET_SELECTED_FORM,
376-
payload: "basic_identity"
377-
};
378-
379-
const setAction = component
380-
.props()
381-
.store.getActions()
382-
.find(action => action.type === Actions.SET_SELECTED_FORM);
383-
384-
expect(setAction).to.deep.equal(expectedAction);
385-
});
386-
387258
it("opens the form_group_id form the firstTab", () => {
388-
const navGroup = component.find(NavGroup).first();
389-
390-
expect(navGroup.props().open).to.equal(firstTab.form_group_id);
259+
mountedComponent(<Nav {...notSelectedProps} />, initialState);
260+
expect(screen.getByText("Basic Identity", { expanded: true })).toBeInTheDocument();
391261
});
392262
});
393263

@@ -447,14 +317,13 @@ describe("<Nav />", () => {
447317
primeroModule: "primeromodule-cp"
448318
};
449319

450-
it("should select first form of the form group", () => {
451-
const { component: navComponent } = setupMountedComponent(Nav, navGroupProps, initialState);
452-
const registrationGroup = navComponent.find(".MuiListItem-gutters").at(3);
320+
it("should select first form of the form group", async () => {
321+
mountedComponent(<Nav {...navGroupProps} />, initialState);
322+
fireEvent.click(screen.getByText("Identification / Registration"));
453323

454-
expect(registrationGroup.text()).to.be.equal("Identification / Registration");
455-
456-
registrationGroup.simulate("click");
457-
expect(navComponent.find(".Mui-selected").at(0).text()).to.be.equal("Basic Identity");
324+
await waitFor(() =>
325+
expect(screen.getByText("Basic Identity").closest(".MuiButtonBase-root")).toHaveClass("navSelected")
326+
);
458327
});
459328
});
460329

@@ -469,40 +338,26 @@ describe("<Nav />", () => {
469338
};
470339

471340
it("should open the record_information group if selectedForm belongs to that group ", () => {
472-
const { component: navComp } = setupMountedComponent(Nav, notSelectedProps, initialState);
473-
474-
const navGroup = navComp.find(NavGroup).first();
475-
476-
expect(navGroup.props().open).to.equal(RECORD_INFORMATION_GROUP);
341+
mountedComponent(<Nav {...notSelectedProps} />, initialState);
342+
expect(screen.getByText("forms.record_types.record_information", { expanded: true })).toBeInTheDocument();
477343
});
478344

479345
it("opens the record_information group and sets incidents froms if there is a incidentFromCase", () => {
480346
const stateWithIncidentFromCase = initialState.setIn(
481347
["records", "cases", "incidentFromCase", "data"],
482348
fromJS({ incident_case_id: "case-id-1" })
483349
);
484-
const { component: navComp } = setupMountedComponent(
485-
Nav,
486-
{ ...notSelectedProps, selectedForm: "" },
487-
stateWithIncidentFromCase
488-
);
489-
490-
const navGroup = navComp.find(NavGroup).first();
491-
492-
expect(navGroup.props().open).to.equal(RECORD_INFORMATION_GROUP);
493-
494-
const expectedAction = {
495-
type: Actions.SET_SELECTED_FORM,
496-
payload: INCIDENT_FROM_CASE
497-
};
498350

499-
const setAction = navComp
500-
.props()
501-
.store.getActions()
502-
.filter(action => action.type === Actions.SET_SELECTED_FORM)
503-
.pop();
351+
const tProps = { ...notSelectedProps, selectedForm: "" };
352+
const { store } = mountedComponent(<Nav {...tProps} />, stateWithIncidentFromCase);
504353

505-
expect(setAction).to.deep.equal(expectedAction);
354+
expect(
355+
store
356+
.getActions()
357+
.filter(action => action.type === Actions.SET_SELECTED_FORM)
358+
.pop()
359+
).toStrictEqual({ type: "forms/SET_SELECTED_FORM", payload: "incident_from_case" });
360+
expect(screen.getByText("forms.record_types.record_information", { expanded: true })).toBeInTheDocument();
506361
});
507362

508363
it("opens the firstTab group and form when incident_from_case form is not found", () => {
@@ -511,52 +366,19 @@ describe("<Nav />", () => {
511366
fromJS({ incident_case_id: "case-id-1" })
512367
);
513368

514-
const { component: navComp } = setupMountedComponent(
515-
Nav,
516-
{ ...notSelectedProps, recordType: "incidents", selectedForm: "basic_identity" },
517-
stateWithIncidentFromCase
518-
);
519-
520-
const expectedAction = {
521-
type: Actions.SET_SELECTED_FORM,
522-
payload: firstTab.unique_id
523-
};
524-
525-
const setAction = navComp
526-
.props()
527-
.store.getActions()
528-
.filter(action => action.type === Actions.SET_SELECTED_FORM)
529-
.pop();
530-
531-
const navGroup = navComp.find(NavGroup).first();
369+
const tProps = { ...notSelectedProps, recordType: "incidents", selectedForm: "basic_identity" };
370+
const { store } = mountedComponent(<Nav {...tProps} />, stateWithIncidentFromCase);
532371

533-
expect(setAction).to.deep.equal(expectedAction);
534-
expect(navGroup.props().open).to.equal(firstTab.form_group_id);
372+
expect(store.getActions()).toStrictEqual([{ type: "forms/SET_SELECTED_FORM", payload: "basic_identity" }]);
373+
expect(screen.getByText("Basic Identity", { expanded: true })).toBeInTheDocument();
535374
});
536375

537376
it("opens the form_group_id and sets the selectedForm from the firstTab if the selected form is not found", () => {
538-
const { component: navComp } = setupMountedComponent(
539-
Nav,
540-
{ ...notSelectedProps, selectedForm: "unknown_form" },
541-
initialState
542-
);
543-
544-
const navGroup = navComp.find(NavGroup).first();
545-
546-
expect(navGroup.props().open).to.equal(firstTab.form_group_id);
547-
548-
const expectedAction = {
549-
type: Actions.SET_SELECTED_FORM,
550-
payload: firstTab.unique_id
551-
};
377+
const tProps = { ...notSelectedProps, selectedForm: "unknown_form" };
378+
const { store } = mountedComponent(<Nav {...tProps} />, initialState);
552379

553-
const setAction = navComp
554-
.props()
555-
.store.getActions()
556-
.filter(action => action.type === Actions.SET_SELECTED_FORM)
557-
.pop();
558-
559-
expect(setAction).to.deep.equal(expectedAction);
380+
expect(store.getActions()).toStrictEqual([{ type: "forms/SET_SELECTED_FORM", payload: "basic_identity" }]);
381+
expect(screen.getByText("Basic Identity", { expanded: true })).toBeInTheDocument();
560382
});
561383
});
562384

@@ -569,14 +391,9 @@ describe("<Nav />", () => {
569391
hasForms: false
570392
};
571393

572-
beforeEach(() => {
573-
({ component } = setupMountedComponent(Nav, propsNoFirstTab, initialState));
574-
});
575-
576394
it("should open record information", () => {
577-
const navGroup = component.find(NavGroup).first();
578-
579-
expect(navGroup.props().open).to.equal("record_information");
395+
mountedComponent(<Nav {...propsNoFirstTab} />, initialState);
396+
expect(screen.getByText("forms.record_types.record_information", { expanded: true })).toBeInTheDocument();
580397
});
581398
});
582399
});

app/javascript/components/record-form/nav/components/close-button-nav-bar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const CloseButtonNavBar = ({ handleToggleNav, mobileDisplay }) => {
1212
return (
1313
<div className={css.closeButtonRecordNav}>
1414
<IconButton onClick={handleToggleNav} className={css.closeIconButtonRecordNav}>
15-
<CloseIcon />
15+
<CloseIcon data-testid="close-icon" />
1616
</IconButton>
1717
</div>
1818
);

0 commit comments

Comments
 (0)