Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48: preserve the order of templates inside the HtmxResponse #49

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ final public class HtmxResponse {
private String headerPushHistory;

public HtmxResponse() {
this.templates = new HashSet<>();
this.templates = new LinkedHashSet<>();
this.triggers = new HashMap<>();
this.triggersAfterSettle = new HashMap<>();
this.triggersAfterSwap = new HashMap<>();
}

/**
* Append the rendered template or fragment.
*
Expand Down Expand Up @@ -179,8 +179,8 @@ private void mergeMapAndLog(HxTriggerLifecycle receive, Map<String, String> trig
}


Set<String> getTemplates() {
return new HashSet<>(templates);
Collection<String> getTemplates() {
return Collections.unmodifiableCollection(templates);
}

Map<String, String> getTriggers() {
Expand Down
43 changes: 37 additions & 6 deletions src/test/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void addingTheSameTemplateASecondTimeShouldIgnoreDuplicates() {
sut.addTemplate(myTemplateAndFragment);
sut.addTemplate(myTemplate);

assertThat(sut.getTemplates()).containsExactlyInAnyOrder(myTemplate, myTemplateAndFragment);
assertThat(sut.getTemplates()).containsExactly(myTemplate, myTemplateAndFragment);
}

@Test
Expand All @@ -60,8 +60,8 @@ public void addingAResponseToExistingMergesTemplatesAndTriggers() {

sut.and(new HtmxResponse().addTemplate("myTemplate2")
.addTrigger("myEvent2")
.addTrigger(myTrigger)
.addTemplate(myTemplate));
.addTrigger(myTrigger)
.addTemplate(myTemplate));

assertThat(sut.getTriggers()).hasSize(2);
assertThat(sut.getTemplates()).hasSize(2);
Expand All @@ -70,14 +70,45 @@ public void addingAResponseToExistingMergesTemplatesAndTriggers() {
@Test
public void extraHxHeaders() {
sut.pushHistory("/a/history")
.browserRedirect("/a/new/page")
.browserRefresh(true)
.retarget("#theThing");
.browserRedirect("/a/new/page")
.browserRefresh(true)
.retarget("#theThing");

assertThat(sut.getHeaderPushHistory()).isEqualTo("/a/history");
assertThat(sut.getHeaderRedirect()).isEqualTo("/a/new/page");
assertThat(sut.getHeaderRefresh()).isTrue();
assertThat(sut.getHeaderRetarget()).isEqualTo("#theThing");
}

/**
* The order of templates can play a role in some scenarios in HTMX,
* see https://github.com/bigskysoftware/htmx/issues/1198
*/
@Test
public void addedTemplatesPreserveTheirOrder() {
String template1 = "form-validation-fragments :: person-list-item";
String template2 = "form-validation-fragments :: form";
sut.addTemplate(template1);
sut.addTemplate(template2);

assertThat(sut.getTemplates()).containsExactly(template1, template2);
}

@Test
public void mergingResponsesPreserveTemplateOrder() {
HtmxResponse response1 = new HtmxResponse().addTemplate("response1 :: template 11")
.addTemplate("response1 :: template 12");
HtmxResponse response2 = new HtmxResponse().addTemplate("response2 :: template 21")
.addTemplate("response2 :: template 22");

response1.and(response2);

assertThat(response1.getTemplates())
.hasSize(4)
.containsExactly("response1 :: template 11",
"response1 :: template 12",
"response2 :: template 21",
"response2 :: template 22");

}
}