Skip to content

Commit 59d9fcd

Browse files
Merge pull request #464 from primeroIMS/trigyn-testcases-record-form-form-field-types-attachments
Trigyn testcases record form form field types attachments
2 parents 4854e28 + bc17e93 commit 59d9fcd

10 files changed

+144
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { mountedComponent, screen, fireEvent } from "../../../../../test-utils";
2+
3+
import { ATTACHMENT_TYPES } from "./constants";
4+
import AttachmentField from "./attachment-field";
5+
6+
describe("<AttachmentField />", () => {
7+
const props = {
8+
arrayHelpers: {},
9+
attachment: ATTACHMENT_TYPES.document,
10+
disabled: false,
11+
mode: {
12+
isShow: false,
13+
isEdit: true
14+
},
15+
name: "attachment_field_test",
16+
index: 0,
17+
value: {}
18+
};
19+
20+
const formProps = {
21+
initialValues: {}
22+
};
23+
24+
it("should render component", () => {
25+
mountedComponent(<AttachmentField {...props} />, {}, [], {}, formProps);
26+
expect(document.querySelector(".uploadBox")).toBeInTheDocument();
27+
expect(screen.getByText("fields.file_upload_box.select_file_button_text")).toBeInTheDocument();
28+
});
29+
30+
it("should render ActionButton", () => {
31+
mountedComponent(<AttachmentField {...props} />, {}, [], {}, formProps);
32+
expect(screen.getByRole("button")).toBeInTheDocument();
33+
});
34+
35+
describe("when value contains attachmentUrl", () => {
36+
const newProps = {
37+
...props,
38+
value: {
39+
attachment_url: "random-string"
40+
}
41+
};
42+
43+
it("should render ActionDialog", () => {
44+
mountedComponent(<AttachmentField {...newProps} />, {}, [], {}, formProps);
45+
fireEvent.click(screen.getByRole("button"));
46+
expect(screen.getByText("fields.remove attachment_field_test")).toBeInTheDocument();
47+
});
48+
49+
it("should not render the AttachmentInput", () => {
50+
mountedComponent(<AttachmentField {...newProps} />, {}, [], {}, formProps);
51+
expect(screen.queryByRole("textbox")).toBeNull();
52+
});
53+
54+
it("should render the AttachmentPreview", () => {
55+
mountedComponent(<AttachmentField {...newProps} />, {}, [], {}, formProps);
56+
expect(screen.getByRole("img")).toBeInTheDocument();
57+
});
58+
});
59+
60+
describe("when value doesn't contains attachmentUrl", () => {
61+
it("should render the AttachmentInput", () => {
62+
mountedComponent(<AttachmentField {...props} />, {}, [], {}, formProps);
63+
expect(screen.getByTestId("input-file")).toBeInTheDocument();
64+
});
65+
66+
it("should not render the AttachmentPreview", () => {
67+
mountedComponent(<AttachmentField {...props} />, {}, [], {}, formProps);
68+
expect(screen.queryByTestId("attachment")).toBeNull();
69+
});
70+
});
71+
});

app/javascript/components/record-form/form/field-types/attachments/attachment-field.unit.test.js

-71
This file was deleted.

app/javascript/components/record-form/form/field-types/attachments/attachment-input.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const AttachmentInput = ({ attachment, fields, name, value, deleteButton }) => {
9595
name={fields.attachment}
9696
onChange={handleOnChange}
9797
disabled={fieldDisabled()}
98+
data-testid="input-file"
9899
type="file"
99100
accept={acceptedType}
100101
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { mountedComponent, screen } from "../../../../../test-utils";
2+
3+
import { ATTACHMENT_TYPES } from "./constants";
4+
import AttachmentInput from "./attachment-input";
5+
6+
describe("<AttachmentInput />", () => {
7+
const props = {
8+
fields: {},
9+
attachment: ATTACHMENT_TYPES.document,
10+
name: "attachment_field_test",
11+
deleteButton: <></>,
12+
value: "test"
13+
};
14+
15+
const formProps = {
16+
initialValues: {}
17+
};
18+
19+
it("should render FastField", () => {
20+
mountedComponent(<AttachmentInput {...props} />, {}, [], {}, formProps);
21+
expect(screen.getByTestId("input-file")).toBeInTheDocument();
22+
});
23+
24+
it("should render ActionButton", () => {
25+
mountedComponent(<AttachmentInput {...props} />, {}, [], {}, formProps);
26+
expect(screen.getByText("fields.file_upload_box.select_file_button_text")).toBeInTheDocument();
27+
});
28+
});

app/javascript/components/record-form/form/field-types/attachments/attachment-input.unit.test.js

-37
This file was deleted.

app/javascript/components/record-form/form/field-types/attachments/attachment-preview.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const AttachmentPreview = ({ name, attachment, attachmentUrl }) => {
2020
if (isAudioAttachment) {
2121
return (
2222
// eslint-disable-next-line jsx-a11y/media-has-caption
23-
<audio id={name} controls>
23+
<audio id={name} controls data-testid="audio">
2424
<source src={attachmentUrl} />
2525
</audio>
2626
);
2727
}
2828

29-
return <img src={attachmentUrl} alt="" className={css.editImg} />;
29+
return <img data-testid="attachment" src={attachmentUrl} alt="" className={css.editImg} />;
3030
};
3131

3232
AttachmentPreview.displayName = "AttachmentPreview";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { mountedComponent, screen } from "../../../../../test-utils";
2+
3+
import { ATTACHMENT_TYPES } from "./constants";
4+
import AttachmentPreview from "./attachment-preview";
5+
6+
describe("<AttachmentPreview />", () => {
7+
const props = {
8+
name: "attachment_field_test",
9+
attachment: ATTACHMENT_TYPES.photo,
10+
attachmentUrl: "abcd"
11+
};
12+
13+
const formProps = {
14+
initialValues: {}
15+
};
16+
17+
it("should render an audio", () => {
18+
const audioProps = { ...props, attachment: ATTACHMENT_TYPES.audio };
19+
20+
mountedComponent(<AttachmentPreview {...audioProps} />);
21+
expect(screen.getByTestId("audio")).toBeInTheDocument();
22+
});
23+
24+
it("should render an img", () => {
25+
mountedComponent(<AttachmentPreview {...props} />, {}, [], {}, formProps);
26+
expect(screen.getByRole("img")).toBeInTheDocument();
27+
expect(screen.getByTestId("attachment")).toBeInTheDocument();
28+
});
29+
});

app/javascript/components/record-form/form/field-types/attachments/attachment-preview.unit.test.js

-30
This file was deleted.

app/javascript/components/record-form/form/field-types/attachments/document-field.unit.test.js app/javascript/components/record-form/form/field-types/attachments/document-field.spec.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
2-
3-
import { FastField } from "formik";
4-
5-
import { setupMountedComponent } from "../../../../../test";
1+
import { mountedComponent, screen } from "../../../../../test-utils";
62
import { FieldRecord } from "../../../records";
7-
import TickField from "../tick-field";
8-
import DateField from "../date-field";
93
import { MODULES } from "../../../../../config";
104

115
import DocumentField from "./document-field";
@@ -56,11 +50,10 @@ describe("<DocumentField />", () => {
5650
};
5751

5852
it("should render a DocumentField", () => {
59-
const { component } = setupMountedComponent(DocumentField, props, {}, [], formProps);
60-
61-
expect(component.find(DateField)).to.have.lengthOf(1);
62-
expect(component.find(FastField)).to.have.lengthOf(4);
63-
expect(component.find(TickField)).to.have.lengthOf(1);
53+
mountedComponent(<DocumentField {...props} />, {}, [], {}, formProps);
54+
expect(screen.getByText(/fields.document.is_current/i)).toBeInTheDocument();
55+
expect(screen.getAllByRole("textbox")).toHaveLength(3);
56+
expect(screen.getByText(/yes_label/i)).toBeInTheDocument();
6457
});
6558

6659
it("should NOT render render a TickField", () => {
@@ -74,8 +67,8 @@ describe("<DocumentField />", () => {
7467
}
7568
}
7669
};
77-
const { component } = setupMountedComponent(DocumentField, propsMRM, {}, [], formProps);
7870

79-
expect(component.find(TickField)).to.have.lengthOf(0);
71+
mountedComponent(<DocumentField {...propsMRM} />, {}, [], {}, formProps);
72+
expect(screen.queryByText(/yes_label/i)).toBeNull();
8073
});
8174
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
1+
import { screen, simpleMountedComponent } from "../../../../../test-utils";
22

3-
import { createSimpleMount } from "../../../../../test";
4-
5-
import AttachmentPreview from "./attachment-preview";
63
import { ATTACHMENT_TYPES } from "./constants";
74
import renderPreview from "./render-preview";
85

@@ -11,7 +8,7 @@ describe("renderPreview", () => {
118
const deleteButton = null;
129

1310
it("should return false if there is no data", () => {
14-
expect(renderPreview(ATTACHMENT_TYPES.photo, {}, css, deleteButton)).to.be.false;
11+
expect(renderPreview(ATTACHMENT_TYPES.photo, {}, css, deleteButton)).toBeFalsy();
1512
});
1613

1714
it("should return a span with the file name if attachment it's document", () => {
@@ -20,10 +17,8 @@ describe("renderPreview", () => {
2017
fileName: "test.txt"
2118
};
2219

23-
const component = createSimpleMount(renderPreview(ATTACHMENT_TYPES.document, file, css, deleteButton));
24-
25-
expect(component.find("span")).to.have.lengthOf(1);
26-
expect(component.text()).to.be.equal("test.txt");
20+
simpleMountedComponent(renderPreview(ATTACHMENT_TYPES.document, file, css, deleteButton));
21+
expect(screen.getByText(/test.txt/i)).toBeInTheDocument();
2722
});
2823

2924
it("should return an AttachmentPreview", () => {
@@ -32,8 +27,7 @@ describe("renderPreview", () => {
3227
fileName: "test.png"
3328
};
3429

35-
const component = createSimpleMount(renderPreview(ATTACHMENT_TYPES.photo, file, css, deleteButton));
36-
37-
expect(component.find(AttachmentPreview)).to.have.lengthOf(1);
30+
simpleMountedComponent(renderPreview(ATTACHMENT_TYPES.photo, file, css, deleteButton));
31+
expect(screen.getByTestId("attachment")).toBeInTheDocument();
3832
});
3933
});

0 commit comments

Comments
 (0)