Skip to content

Commit

Permalink
Merge pull request #90 from xhaggi/fix/htmx-response-builder-and
Browse files Browse the repository at this point in the history
[fix] HtmxResponse.Builder#and() does not consider all properties
  • Loading branch information
wimdeblauwe authored Oct 18, 2023
2 parents 6f32c2e + 9a2187c commit 9f73899
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ public Builder and(HtmxResponse otherResponse) {
mergeTriggers(this.triggersAfterSettle, otherResponse.triggersAfterSettle);
mergeTriggers(this.triggersAfterSwap, otherResponse.triggersAfterSwap);

if (otherResponse.location != null) {
this.location = otherResponse.location;
}
if (otherResponse.pushUrl != null) {
this.pushUrl = otherResponse.pushUrl;
}
Expand All @@ -436,12 +439,18 @@ public Builder and(HtmxResponse otherResponse) {
if (otherResponse.refresh) {
this.refresh = true;
}
if (otherResponse.retarget != null) {
this.retarget = otherResponse.retarget;
if (otherResponse.replaceUrl != null) {
this.replaceUrl = otherResponse.replaceUrl;
}
if (otherResponse.reswap != null) {
this.reswap = otherResponse.reswap;
}
if (otherResponse.retarget != null) {
this.retarget = otherResponse.retarget;
}
if (otherResponse.reselect != null) {
this.reselect = otherResponse.reselect;
}

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,32 @@ public void testAddingResponseToExistingOneShouldMergeTemplatesAndTriggers() {
@Test
public void testAddingResponseToExistingOneShouldOverrideProperties() {
var response1 = HtmxResponse.builder()
.retarget("selector1")
.reswap(HtmxReswap.innerHtml())
.location("location1")
.pushUrl("url1")
.redirect("url1")
.pushUrl("url1");
.replaceUrl("url1")
.reswap(HtmxReswap.innerHtml())
.retarget("selector1");

var response2 = HtmxResponse.builder()
.retarget("selector2")
.reswap(HtmxReswap.outerHtml())
.location("location2")
.pushUrl("url2")
.redirect("url2")
.refresh()
.pushUrl("url2");
.replaceUrl("url2")
.reswap(HtmxReswap.outerHtml())
.retarget("selector2")
.refresh();

response1.and(response2.build());

assertThat(response1.build()).satisfies(response -> {
assertThat(response.getRetarget()).isEqualTo("selector2");
assertThat(response.getReswap()).isEqualTo(HtmxReswap.outerHtml());
assertThat(response.getLocation()).isEqualTo(new HtmxLocation("location2"));
assertThat(response.getPushUrl()).isEqualTo(null);
assertThat(response.getRedirect()).isEqualTo("url2");
assertThat(response.getReplaceUrl()).isEqualTo("url2");
assertThat(response.getReswap()).isEqualTo(HtmxReswap.outerHtml());
assertThat(response.getRetarget()).isEqualTo("selector2");
assertThat(response.isRefresh()).isEqualTo(true);
assertThat(response.getPushUrl()).isEqualTo("url2");
});
}

Expand Down

0 comments on commit 9f73899

Please sign in to comment.