Skip to content

Commit

Permalink
GFORMS-2946 - Allow summaryValue on InformationMessage to display in …
Browse files Browse the repository at this point in the history
…summary pages (#2330)

* GFORMS-2946 - WIP view info comp on summary

* GFORMS-2946 - First tests added

* GFORMS-2946 - Test to ensure info message renders in summary list

* GFORMS-2946 - Address PR suggestion to simplify expression
  • Loading branch information
cmanson authored Nov 27, 2024
1 parent eac40e4 commit 308f2af
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/uk/gov/hmrc/gform/eval/AllFormComponentExpressions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ object AllFormComponentExpressions extends ExprExtractorHelpers {
hints.fold(List.empty[Expr])(fromNel),
optionHelpText.fold(List.empty[Expr])(fromNel)
)
case IsInformationMessage(InformationMessage(_, infoText)) =>
toPlainExprs(infoText.allInterpolations)
case IsInformationMessage(InformationMessage(_, infoText, valueSummary)) =>
toPlainExprs(infoText.allInterpolations, valueSummary.fold(List.empty[Expr])(_.allInterpolations))
case HasExpr(expr) => toPlainExprs(expr :: Nil)
case IsMiniSummaryList(MiniSummaryList(rows, _, _)) =>
toPlainExprs(
Expand Down
24 changes: 14 additions & 10 deletions app/uk/gov/hmrc/gform/gform/FormComponentUpdater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,20 @@ class FormComponentUpdater(formComponent: FormComponent, index: Int, baseIds: Li
includeIf = formComponent.includeIf.map(expandIncludeIf),
validIf = formComponent.validIf.map(expandValidIf),
`type` = formComponent.`type` match {
case t: Text => expandText(t)
case t: TextArea => t.copy(value = expandExpr(t.value))
case t: HmrcTaxPeriod => t.copy(idNumber = expandExpr(t.idNumber))
case t: Choice => expandChoice(t)
case t: RevealingChoice => expandRevealingChoice(t)
case t: Group => expandGroup(t)
case t: InformationMessage => t.copy(infoText = expandSmartString(t.infoText))
case t: MiniSummaryList => expandSummaryList(t)
case t: TableComp => expandTableComp(t)
case otherwise => otherwise
case t: Text => expandText(t)
case t: TextArea => t.copy(value = expandExpr(t.value))
case t: HmrcTaxPeriod => t.copy(idNumber = expandExpr(t.idNumber))
case t: Choice => expandChoice(t)
case t: RevealingChoice => expandRevealingChoice(t)
case t: Group => expandGroup(t)
case t: InformationMessage =>
t.copy(
infoText = expandSmartString(t.infoText),
summaryValue = t.summaryValue.map(expandSmartString)
)
case t: MiniSummaryList => expandSummaryList(t)
case t: TableComp => expandTableComp(t)
case otherwise => otherwise
},
label = expandSmartString(formComponent.label),
helpText = formComponent.helpText.map(expandSmartString),
Expand Down
6 changes: 3 additions & 3 deletions app/uk/gov/hmrc/gform/gform/SectionRenderingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class SectionRenderingService(
}
}
.map {
case info @ IsInformationMessage(InformationMessage(infoType, infoText)) =>
case info @ IsInformationMessage(InformationMessage(infoType, infoText, _)) =>
htmlForInformationMessage(info, infoType, infoText)
case fc @ IsTableComp(table) =>
htmlForTableComp(fc, table, formModelOptics)
Expand Down Expand Up @@ -365,7 +365,7 @@ class SectionRenderingService(
}
}
.map {
case info @ IsInformationMessage(InformationMessage(infoType, infoText)) =>
case info @ IsInformationMessage(InformationMessage(infoType, infoText, _)) =>
htmlForInformationMessage(info, infoType, infoText)
case fc @ IsTableComp(table) =>
htmlForTableComp(fc, table, formModelOptics)
Expand Down Expand Up @@ -1411,7 +1411,7 @@ class SectionRenderingService(
htmlForFileUploadSingle(formComponent, ei, upscanData, additionalAttributes)
}

case InformationMessage(infoType, infoText) =>
case InformationMessage(infoType, infoText, _) =>
htmlForInformationMessage(formComponent, infoType, infoText)
case htp @ HmrcTaxPeriod(idType, idNumber, regimeType) =>
htmlForHmrcTaxPeriod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ case class Group(
repeatAddAnotherText: Option[SmartString] = None
) extends ComponentType

case class InformationMessage(infoType: InfoType, infoText: SmartString) extends ComponentType
case class InformationMessage(infoType: InfoType, infoText: SmartString, summaryValue: Option[SmartString] = None)
extends ComponentType

case class FileUpload(
fileSizeLimit: Option[Int],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ case class FormComponent(

def hideOnSummary: Boolean =
presentationHint.fold(false)(x => x.contains(InvisibleInSummary)) ||
IsInformationMessage.unapply(this).isDefined || !displayInSummary.getOrElse(true)
IsInformationMessage.unapply(this).fold(false)(info => info.summaryValue.isEmpty) ||
!displayInSummary.getOrElse(true)

def withIndex(index: Int) = copy(id = id.withIndex(index))

Expand Down
67 changes: 65 additions & 2 deletions app/uk/gov/hmrc/gform/summary/FormComponentSummaryRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,20 @@ object FormComponentSummaryRenderer {
keyDisplayWidth
)

case IsInformationMessage(_) =>
List(SummaryListRow())
case IsInformationMessage(infoMessage) =>
getInfoMessageSummaryListRows(
infoMessage,
formComponent,
formTemplateId,
maybeAccessCode,
sectionNumber,
sectionTitle4Ga,
formFieldValidationResult,
envelope,
iterationTitle,
fastForward,
keyDisplayWidth
)

case IsTableComp(table) =>
getTableSummaryListRows(
Expand Down Expand Up @@ -1052,6 +1064,57 @@ object FormComponentSummaryRenderer {
)
}

private def getInfoMessageSummaryListRows[T <: RenderType](
infoMessage: InformationMessage,
formComponent: FormComponent,
formTemplateId: FormTemplateId,
maybeAccessCode: Option[AccessCode],
sectionNumber: SectionNumber,
sectionTitle4Ga: SectionTitle4Ga,
formFieldValidationResult: FormFieldValidationResult,
envelope: EnvelopeWithMapping,
iterationTitle: Option[String],
fastForward: List[FastForward],
keyDisplayWidth: KeyDisplayWidth
)(implicit
messages: Messages,
lise: SmartStringEvaluator,
fcrd: FormComponentRenderDetails[T]
): List[SummaryListRow] = {
val label = fcrd.label(formComponent)
val visuallyHiddenText = getVisuallyHiddenText(formComponent)
val viewLabel = messages("summary.view")
val keyClasses = getKeyClasses(hasErrors = false, keyDisplayWidth)

List(
summaryListRow(
label,
Html(infoMessage.summaryValue.getOrElse(infoMessage.infoText).value()),
visuallyHiddenText,
keyClasses,
"",
"",
List(
(
uk.gov.hmrc.gform.gform.routes.FormController
.form(
formTemplateId,
maybeAccessCode,
sectionNumber,
sectionTitle4Ga,
SuppressErrors.Yes,
fastForward
),
viewLabel,
iterationTitle.fold(viewLabel + " " + label)(it => viewLabel + " " + it + " " + label)
)
),
""
)
)

}

private def getTableSummaryListRows[T <: RenderType](
table: TableComp,
formComponent: FormComponent,
Expand Down
2 changes: 1 addition & 1 deletion app/uk/gov/hmrc/gform/validation/ComponentsValidator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class ComponentsValidator[D <: DataOrigin, F[_]: Monad](
validIf(
new FileUploadChecker[D]().runCheck(checkerDependency)
)
case InformationMessage(_, _) => validationSuccess.pure[F]
case InformationMessage(_, _, _) => validationSuccess.pure[F]
case HmrcTaxPeriod(_, _, _) =>
validIf(new ChoiceChecker[D]().runCheck(checkerDependency))
case t @ Time(_, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package uk.gov.hmrc.gform.sharedmodel.formtemplate

import uk.gov.hmrc.gform.Helpers.toSmartString
import uk.gov.hmrc.gform.Spec
import uk.gov.hmrc.gform.sharedmodel.{ LocalisedString, SmartString }
import uk.gov.hmrc.gform.sharedmodel.formtemplate.generators.FormComponentGen

import org.scalatest.prop.Tables.Table

class FormComponentSpec extends Spec {
/* private val exprText = Text(BasicText, Add(Constant("1"), FormCtx(FormComponentId("other-field-id"))))
* private val exprTextArea = TextArea(BasicText, Value)
Expand All @@ -40,6 +43,58 @@ class FormComponentSpec extends Spec {
}
}

it should "correctly determine hide on summary value" in {
val table = Table(
("formComponent", "expectedResult"),
(
mkInfoMessageComponent(
"id1",
"Some text",
Some(toSmartString("Summary text"))
),
false
),
(
mkInfoMessageComponent(
"id1",
"Some text"
),
true
),
(
mkFormComponent(
"anything",
Text(TextConstraint.default, Value, DisplayWidth.DEFAULT, IsUpperCase),
"anything",
"anything"
),
false
),
(
mkFormComponent(
"anything",
Text(TextConstraint.default, Value, DisplayWidth.DEFAULT, IsUpperCase),
"anything",
"anything"
).copy(displayInSummary = Some(false)),
true
),
(
mkFormComponent(
"anything",
Text(TextConstraint.default, Value, DisplayWidth.DEFAULT, IsUpperCase),
"anything",
"anything"
).copy(presentationHint = Some(List(InvisibleInSummary))),
true
)
)

org.scalatest.prop.TableDrivenPropertyChecks.forAll(table) { (formComponent, expected) =>
formComponent.hideOnSummary shouldBe expected
}
}

/* it should "not expand Text, TextArea, UkSortCode, Data, Address, Choice, InformationMessage, FileUpload" in {
* notExpand(exprText)
* notExpand(exprTextArea)
Expand Down Expand Up @@ -186,4 +241,33 @@ class FormComponentSpec extends Spec {
None,
None
)

def mkInfoMessageComponent(
id: String,
infoText: String,
summaryValue: Option[SmartString] = None,
label: SmartString = SmartString(LocalisedString(Map()), List())
): FormComponent =
FormComponent(
FormComponentId(id),
InformationMessage(
NoFormat,
toSmartString(infoText),
summaryValue
),
label,
false,
None,
None,
None,
None,
true,
false,
false,
false,
false,
None,
None
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import uk.gov.hmrc.gform.models.{ FastForward, FormModelSupport, SectionSelector
import uk.gov.hmrc.gform.models.optics.DataOrigin
import uk.gov.hmrc.gform.sharedmodel.ExampleData.{ buildForm, buildFormComponent, buildFormTemplate, destinationList, envelopeWithMapping, nonRepeatingPageSection }
import uk.gov.hmrc.gform.sharedmodel.form.{ Form, FormData, FormField, FormModelOptics }
import uk.gov.hmrc.gform.sharedmodel.formtemplate.{ Constant, DisplayInSummary, Equals, FormComponent, FormComponentId, FormCtx, FormTemplate, FormTemplateContext, IncludeIf, KeyDisplayWidth, MiniSummaryList, MiniSummaryListValue, SectionNumber, SectionOrSummary, SectionTitle4Ga, Value }
import uk.gov.hmrc.gform.sharedmodel.formtemplate.{ Constant, DisplayInSummary, Equals, FormComponent, FormComponentId, FormCtx, FormTemplate, FormTemplateContext, IncludeIf, InformationMessage, KeyDisplayWidth, MiniSummaryList, MiniSummaryListValue, NoFormat, SectionNumber, SectionOrSummary, SectionTitle4Ga, Value }
import uk.gov.hmrc.gform.sharedmodel.formtemplate.MiniSummaryRow.ValueRow
import uk.gov.hmrc.gform.sharedmodel.{ AccessCode, LangADT, NotChecked }
import uk.gov.hmrc.gform.sharedmodel.{ AccessCode, LangADT, LocalisedString, NotChecked, SmartString }
import uk.gov.hmrc.gform.validation.ValidationResult
import uk.gov.hmrc.govukfrontend.views.Aliases.{ Empty, Text }
import uk.gov.hmrc.govukfrontend.views.viewmodels.content.HtmlContent
Expand Down Expand Up @@ -216,4 +216,90 @@ class FormComponentSummaryRendererSpec extends FunSuite with FormModelSupport {
}
}

test("summaryListRows should return correct list of summary list rows from informationMessage components") {
val table = Table(
("infoMessage", "key", "value", "count"),
(
mkInfoMessageComponent(
"id1",
"information text",
Some(toSmartString("summary information text")),
toSmartString("keyLabel")
),
"keyLabel",
"summary information text",
1
)
)

forAll(table) { (infoMessage, key, value, count) =>
val testFixture: TestFixture = new TestFixture {
override lazy val formTemplate: FormTemplate = buildFormTemplate(
destinationList,
sections = List(nonRepeatingPageSection(title = "page1", fields = List(infoMessage)))
)
}
import testFixture._
val rows: List[SummaryListRow] =
FormComponentSummaryRenderer.summaryListRows[DataOrigin.Mongo, SummaryRender](
infoMessage,
None,
formTemplate._id,
formModelOptics.formModelVisibilityOptics,
None,
SectionNumber.Classic(0),
SectionTitle4Ga("page1"),
NotChecked,
ValidationResult.empty,
envelopeWithMapping,
AddressRecordLookup.from(cache.form.thirdPartyData),
None,
Some(List(FastForward.CYA(SectionOrSummary.FormSummary))),
KeyDisplayWidth.S
)
assertEquals(rows.length, count)

if (rows.nonEmpty) {
assertEquals(
rows.head.key.content,
if (key.isEmpty) { Empty }
else { Text(key) }
)
assertEquals(
rows.head.value.content,
if (value.isEmpty) { Empty }
else { HtmlContent(value) }
)
}
}
}

def mkInfoMessageComponent(
id: String,
infoText: String,
summaryValue: Option[SmartString] = None,
label: SmartString = SmartString(LocalisedString(Map()), List())
): FormComponent =
FormComponent(
FormComponentId(id),
InformationMessage(
NoFormat,
toSmartString(infoText),
summaryValue
),
label,
false,
None,
None,
None,
None,
true,
false,
false,
false,
false,
None,
None
)

}

0 comments on commit 308f2af

Please sign in to comment.