Skip to content

Commit

Permalink
CCMSPUI-468 | Refactor and Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar461 committed Jan 4, 2025
1 parent d037c09 commit d17965d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.gov.laa.ccms.springboot.dialect;

/**
* DatePickerAttributes.
*/
public record DatePickerAttributes(
String id,
String name,
String label,
String hint,
String errorMessage,
String value,
String minDate,
String maxDate) {

public boolean hasError() {
return !errorMessage.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,77 @@ protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
IElementTagStructureHandler structureHandler) {

Map<String, String> attributes = ProcessorUtils.parseAttributes(context, tag);

String id = attributes.getOrDefault("id", "date");
String name = attributes.getOrDefault("name", "date");
String value = attributes.get("value");
String label = attributes.getOrDefault("label", "Date");
String hint = attributes.getOrDefault("hint", "For example, 17/5/2024.");
String errorMessage = attributes.getOrDefault("errorMessage", "Enter or select a date");
String minDate = attributes.get("minDate");
String maxDate = attributes.get("maxDate");

boolean hasError = !errorMessage.isEmpty();

String datePickerHtml =
buildDatePickerHtml(id, name, label, hint, errorMessage, hasError, minDate, maxDate);

DatePickerAttributes datePickerAttributes = new DatePickerAttributes(
attributes.getOrDefault("id", "date"),
attributes.getOrDefault("name", "date"),
attributes.getOrDefault("label", "Date"),
attributes.get("hint"),
attributes.get("errorMessage"),
attributes.get("value"),
attributes.get("minDate"),
attributes.get("maxDate")
);

String datePickerHtml = buildDatePickerHtml(datePickerAttributes);
final IModelFactory modelFactory = context.getModelFactory();
final IModel model = modelFactory.parse(context.getTemplateData(), datePickerHtml);
structureHandler.replaceWith(model, false);

}

private String buildDatePickerHtml(String id, String name, String label, String hint,
String errorMessage, boolean hasError, String minDate,
String maxDate) {
private String buildDatePickerHtml(DatePickerAttributes datePickerAttributes) {
StringBuilder html = new StringBuilder();
html.append("<div class=\"moj-datepicker\" data-module=\"moj-date-picker\"");

if (!minDate.isEmpty()) {
html.append(" data-min-date=\"").append(minDate).append("\"");
if (!datePickerAttributes.minDate().isEmpty()) {
html.append(" data-min-date=\"").append(datePickerAttributes.minDate()).append("\"");
}
if (!maxDate.isEmpty()) {
html.append(" data-max-date=\"").append(maxDate).append("\"");
if (!datePickerAttributes.maxDate().isEmpty()) {
html.append(" data-max-date=\"").append(datePickerAttributes.maxDate()).append("\"");
}

html.append(">")
.append("<div class=\"govuk-form-group");

if (hasError) {
if (datePickerAttributes.hasError()) {
html.append(" govuk-form-group--error");
}

html.append("\">")
.append("<label class=\"govuk-label\" for=\"").append(id).append("\">")
.append(label)
.append("<label class=\"govuk-label\" for=\"").append(datePickerAttributes.id())
.append("\">")
.append(datePickerAttributes.label())
.append("</label>")
.append("<div id=\"").append(id).append("-hint\" class=\"govuk-hint\">")
.append(hint)
.append("<div id=\"").append(datePickerAttributes.id())
.append("-hint\" class=\"govuk-hint\">")
.append(datePickerAttributes.hint())
.append("</div>");

if (hasError) {
html.append("<p id=\"").append(id).append("-error\" class=\"govuk-error-message\">")
if (datePickerAttributes.hasError()) {
html.append("<p id=\"").append(datePickerAttributes.id())
.append("-error\" class=\"govuk-error-message\">")
.append("<span class=\"govuk-visually-hidden\">Error:</span> ")
.append(errorMessage)
.append(datePickerAttributes.errorMessage())
.append("</p>");
}

html.append("<input class=\"govuk-input moj-js-datepicker-input");

if (hasError) {
if (datePickerAttributes.hasError()) {
html.append(" govuk-input--error");
}

html.append("\" id=\"").append(id)
.append("\" name=\"").append(name)
.append("\" type=\"text\" aria-describedby=\"").append(id).append("-hint");
html.append("\" id=\"").append(datePickerAttributes.id())
.append("\" name=\"").append(datePickerAttributes.name())
.append("\" type=\"text\" aria-describedby=\"").append(datePickerAttributes.id())
.append("-hint");

if (datePickerAttributes.hasError()) {
html.append(" ").append(datePickerAttributes.id()).append("-error");
}

if (hasError) {
html.append(" ").append(id).append("-error");
if (!datePickerAttributes.value().isEmpty()) {
html.append("\" value=\"").append(datePickerAttributes.value());
}

html.append("\" autocomplete=\"off\">")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
package uk.gov.laa.ccms.springboot.dialect;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

@SpringBootTest(classes = ThymeleafTestConfig.class)
class MoJDatePickerElementTagProcessorTest {

@Autowired
private SpringTemplateEngine templateEngine;

@Test
void shouldRenderGovukButton() {

Context context = new Context();
String renderedHtml = templateEngine.process("test-datepicker", context);
assertThat(renderedHtml)
.contains(
"<div class=\"moj-datepicker\" data-module=\"moj-date-picker\" data-min-date=\"2000-01-01\" " +
"data-max-date=\"2025-12-31\"><div class=\"govuk-form-group govuk-form-group--error\">" +
"<label class=\"govuk-label\" for=\"dob\">Date of Birth</label><div id=\"dob-hint\" " +
"class=\"govuk-hint\">For example, 01/01/2000.</div><p id=\"dob-error\" " +
"class=\"govuk-error-message\"><span class=\"govuk-visually-hidden\">Error:</span> " +
"Please enter a valid date of birth.</p><input class=\"govuk-input moj-js-datepicker-input " +
"govuk-input--error\" id=\"dob\" name=\"dateOfBirth\" type=\"text\" " +
"aria-describedby=\"dob-hint dob-error\" value=\"2024-01-01\" autocomplete=\"off\">" +
"</div></div>");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ThymeleafTestConfig {
public SpringTemplateEngine testTemplateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new GovUkDialect());
templateEngine.addDialect(new MojCustomDialect());
templateEngine.addTemplateResolver(templateResolver());
return templateEngine;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns:moj="http://www.thymeleaf.org" lang="EN">
<head>
<title>Datepicker Test</title>
</head>
<body>

<moj:datepicker
id="dob"
name="dateOfBirth"
label="Date of Birth"
hint="For example, 01/01/2000."
errorMessage="Please enter a valid date of birth."
minDate="2000-01-01"
maxDate="2025-12-31"
value="2024-01-01">
</moj:datepicker>

</body>
</html>

0 comments on commit d17965d

Please sign in to comment.