Skip to content

Commit 4ec8faa

Browse files
VIA-608 AJ Remove extra margin from last subsection in a section
1 parent 34d0d46 commit 4ec8faa

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/services/content-api/parsers/content-styling-service.test.tsx

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ describe("ContentStylingService", () => {
3030
headline: "Headline",
3131
};
3232

33+
const mockMarkdown2Subsection: VaccinePageSubsection = {
34+
type: "simpleElement",
35+
text: "<h2>This is second styled paragraph markdown subsection</h2>",
36+
name: "markdown",
37+
headline: "Headline2",
38+
};
39+
40+
const mockMarkdown3Subsection: VaccinePageSubsection = {
41+
type: "simpleElement",
42+
text: "<h2>This is third styled paragraph markdown subsection</h2>",
43+
name: "markdown",
44+
headline: "Headline3",
45+
};
46+
3347
const mockHowToGetMarkdownSubsection: VaccinePageSubsection = {
3448
type: "simpleElement",
3549
text: "<p>para</p><h3>If you're aged 75 to 79</h3><p>para1</p><p>para2</p><h3>If you're pregnant</h3><p>para3</p><p>para4</p>",
@@ -66,7 +80,7 @@ describe("ContentStylingService", () => {
6680

6781
describe("styleSubsection", () => {
6882
it("should return styled markdown component for subsection beginning with headline", async () => {
69-
const styledSubsection: JSX.Element | undefined = styleSubsection(mockMarkdownSubsection, 1);
83+
const styledSubsection: JSX.Element | undefined = styleSubsection(mockMarkdownSubsection, 1, false);
7084
render(styledSubsection);
7185

7286
const heading1: HTMLElement = screen.getByRole("heading", {
@@ -87,7 +101,7 @@ describe("ContentStylingService", () => {
87101
headline: "",
88102
};
89103

90-
const styledSubsection: JSX.Element | undefined = styleSubsection(mockMarkdownSubsection, 1);
104+
const styledSubsection: JSX.Element | undefined = styleSubsection(mockMarkdownSubsection, 1, false);
91105
render(styledSubsection);
92106

93107
const div: HTMLElement = screen.getByRole("section");
@@ -105,7 +119,7 @@ describe("ContentStylingService", () => {
105119
headline: "",
106120
};
107121

108-
const styledSubsection: JSX.Element = styleSubsection(mockInformationSubsection, 1);
122+
const styledSubsection: JSX.Element = styleSubsection(mockInformationSubsection, 1, false);
109123
render(styledSubsection);
110124

111125
const text: HTMLElement = screen.getByText("This is a styled paragraph information subsection");
@@ -117,7 +131,7 @@ describe("ContentStylingService", () => {
117131
});
118132

119133
it("should return styled non-urgent component for subsection", async () => {
120-
const styledSubsection: JSX.Element = styleSubsection(mockNonUrgentSubsection, 1);
134+
const styledSubsection: JSX.Element = styleSubsection(mockNonUrgentSubsection, 1, false);
121135
render(styledSubsection);
122136

123137
const text: HTMLElement = screen.getByText("This is a styled paragraph non-urgent subsection");
@@ -130,7 +144,7 @@ describe("ContentStylingService", () => {
130144
});
131145

132146
it("should return styled urgent component for subsection", async () => {
133-
const styledSubsection: JSX.Element = styleSubsection(mockUrgentSubsection, 1);
147+
const styledSubsection: JSX.Element = styleSubsection(mockUrgentSubsection, 1, false);
134148
render(styledSubsection);
135149

136150
const text: HTMLElement = screen.getByText("This is a styled paragraph urgent subsection");
@@ -143,7 +157,7 @@ describe("ContentStylingService", () => {
143157
});
144158

145159
it("should return table component for subsection", async () => {
146-
const styledSubsection: JSX.Element = styleSubsection(mockTableSubsection, 1);
160+
const styledSubsection: JSX.Element = styleSubsection(mockTableSubsection, 1, false);
147161
render(styledSubsection);
148162

149163
const column1: HTMLElement = screen.getByText("Name");
@@ -158,7 +172,7 @@ describe("ContentStylingService", () => {
158172
});
159173

160174
it("should return styled callout component for subsection", async () => {
161-
const styledSubsection: JSX.Element = styleSubsection(mockCalloutSubsection, 1);
175+
const styledSubsection: JSX.Element = styleSubsection(mockCalloutSubsection, 1, false);
162176
render(styledSubsection);
163177

164178
const text: HTMLElement = screen.getByText("This is a styled paragraph callout subsection");
@@ -193,6 +207,23 @@ describe("ContentStylingService", () => {
193207
expect(urgentSubsection).toBeInTheDocument();
194208
expect(columnOfTable).toBeInTheDocument();
195209
});
210+
211+
it("should display last subsection with zero margin in a section", async () => {
212+
const mockSection: VaccinePageSection = {
213+
headline: "This is a heading",
214+
subsections: [mockMarkdownSubsection, mockMarkdown2Subsection, mockMarkdown3Subsection],
215+
};
216+
217+
const styledSection: StyledPageSection = styleSection(mockSection);
218+
render(styledSection.component);
219+
const markdownSubsection: HTMLElement = screen.getByText("This is a styled paragraph markdown subsection");
220+
const markdown2Subsection: HTMLElement = screen.getByText("This is second styled paragraph markdown subsection");
221+
const markdown3Subsection: HTMLElement = screen.getByText("This is third styled paragraph markdown subsection");
222+
223+
expect(markdownSubsection.parentNode).not.toHaveClass("zeroMarginBottom");
224+
expect(markdown2Subsection.parentNode).not.toHaveClass("zeroMarginBottom");
225+
expect(markdown3Subsection.parentNode).toHaveClass("zeroMarginBottom");
226+
});
196227
});
197228

198229
describe("getStyledContentForVaccine", () => {
@@ -237,7 +268,7 @@ describe("ContentStylingService", () => {
237268
const expectedRsvHowToGetSection = "<div><p>para1</p><p>para2</p></div>";
238269
const expectedRsvPregnancyHowToGetSection = `<div><div><p>para3</p><p>para4</p></div></div>`;
239270
const expectedGenericVaccineHowToGetSection =
240-
"<div><h3>How To Get Headline</h3><p>para</p><h3>If you're aged 75 to 79</h3><p>para1</p><p>para2</p><h3>If you're pregnant</h3><p>para3</p><p>para4</p></div>";
271+
"<div class=\"zeroMarginBottom\"><h3>How To Get Headline</h3><p>para</p><h3>If you're aged 75 to 79</h3><p>para1</p><p>para2</p><h3>If you're pregnant</h3><p>para3</p><p>para4</p></div>";
241272
const { container } = render(styledVaccineContent.howToGetVaccine.component);
242273
if (vaccine === VaccineType.RSV) {
243274
expect(container).toContainHTML(expectedRsvHowToGetSection);

src/services/content-api/parsers/content-styling-service.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const _getDivWithSanitisedHtml = (html: string) => {
3636
return <div className={styles.zeroMarginBottom} dangerouslySetInnerHTML={{ __html: sanitiseHtml(html) }} />;
3737
};
3838

39-
const styleSubsection = (subsection: VaccinePageSubsection, id: number): JSX.Element => {
39+
const styleSubsection = (subsection: VaccinePageSubsection, id: number, isLastSubsection: boolean): JSX.Element => {
4040
if (subsection.type === "expanderElement") {
4141
const content = `<h3>${subsection.headline}</h3>`.concat(subsection.mainEntity);
4242
return <div key={id} dangerouslySetInnerHTML={{ __html: sanitiseHtml(content) }} />;
@@ -87,19 +87,28 @@ const styleSubsection = (subsection: VaccinePageSubsection, id: number): JSX.Ele
8787
</WarningCallout>
8888
);
8989
} else {
90-
return <div key={id} dangerouslySetInnerHTML={{ __html: sanitiseHtml(text) }}></div>;
90+
return (
91+
<div
92+
className={isLastSubsection ? styles.zeroMarginBottom : undefined}
93+
key={id}
94+
dangerouslySetInnerHTML={{ __html: sanitiseHtml(text) }}
95+
></div>
96+
);
9197
}
9298
};
9399

94100
const styleSection = (section: VaccinePageSection): StyledPageSection => {
95101
const heading = section.headline;
102+
const indexOfLastChild: number = section.subsections.length - 1;
103+
96104
const styledComponent = (
97105
<>
98106
{section.subsections.map((subsection: VaccinePageSubsection, index: number) =>
99-
styleSubsection(subsection, index),
107+
styleSubsection(subsection, index, index == indexOfLastChild),
100108
)}
101109
</>
102110
);
111+
103112
return {
104113
heading,
105114
component: styledComponent,

0 commit comments

Comments
 (0)