Skip to content

Commit

Permalink
Merge pull request #49 from cloudflightio/48
Browse files Browse the repository at this point in the history
48: preserve the order of templates inside the HtmxResponse
  • Loading branch information
wimdeblauwe authored Jul 20, 2023
2 parents 4b0bd2b + 64f3902 commit 79f21bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
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");

}
}

0 comments on commit 79f21bd

Please sign in to comment.