Skip to content

Commit

Permalink
48: preserve the order of templates inside the HtmxResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
klu2 committed Jul 17, 2023
1 parent 5811d4f commit b67954b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 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 TreeSet<>();
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
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 Down Expand Up @@ -80,4 +80,20 @@ public void extraHxHeaders() {
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 myTemplate = "myTemplate";
String myTemplateAndFragment = "myTemplate :: andFragment";
String myTemplateAndFragment2 = "myTemplate :: andFragment2";
sut.addTemplate(myTemplate);
sut.addTemplate(myTemplateAndFragment);
sut.addTemplate(myTemplateAndFragment2);

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

}

0 comments on commit b67954b

Please sign in to comment.