Skip to content
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 @@ -15,6 +15,10 @@
import org.immutables.value.Value.Default;
import org.immutables.value.Value.Immutable;

/**
* Slack's section block displays text, optionally alongside fields and an accessory element.
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/section-block">Section block Docs</a>
*/
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
Expand All @@ -38,6 +42,8 @@ default Text getText() {

Optional<BlockElement> getAccessory();

Optional<Boolean> getExpand();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please leave the link to Slack API documentation about this element: https://docs.slack.dev/reference/block-kit/blocks/section-block?
Like it's done for the other elements, like TaskCardBlockIF, MarkdownBlockIF, this makes it easier to check the parameters and structure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the link to the section block docs on SectionIF in 834446b, following the same pattern as TaskCardBlockIF and MarkdownBlockIF.


@Check
default void check() {
boolean hasNonEmptyTextField =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.hubspot.slack.client.models.blocks;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.slack.client.jackson.ObjectMapperUtils;
import com.hubspot.slack.client.models.blocks.objects.Text;
import com.hubspot.slack.client.models.blocks.objects.TextType;
import java.io.IOException;
import org.junit.Test;

public class SectionBlockTest {

private static final ObjectMapper MAPPER = ObjectMapperUtils.mapper();
private static final String SECTION_TEXT = "A long section that should never collapse";

@Test
public void itSerializesExpandWhenSet() {
Section section = Section
.builder()
.setText(Text.of(TextType.MARKDOWN, SECTION_TEXT))
.setExpand(true)
.build();

JsonNode json = MAPPER.valueToTree(section);

assertThat(json.get("expand").asBoolean()).isTrue();
}

@Test
public void itSerializesExpandFalseWhenSet() {
Section section = Section
.builder()
.setText(Text.of(TextType.MARKDOWN, SECTION_TEXT))
.setExpand(false)
.build();

JsonNode json = MAPPER.valueToTree(section);

assertThat(json.has("expand")).isTrue();
assertThat(json.get("expand").isBoolean()).isTrue();
assertThat(json.get("expand").booleanValue()).isFalse();
}

@Test
public void itOmitsExpandWhenNotSet() {
Section section = Section.of(Text.of(TextType.MARKDOWN, SECTION_TEXT));

JsonNode json = MAPPER.valueToTree(section);

assertThat(json.has("expand")).isFalse();
}

@Test
public void itDeserializesExpand() throws IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't cover expand(false). Optional.of(false) should serialize as "expand": false, not be dropped, but nothing asserts it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added itSerializesExpandFalseWhenSet in 2d8d54a. It builds a section with expand(false) and asserts that the JSON has an expand key set to the boolean false, so an explicit false isn't dropped.

String rawJson =
"{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"" +
SECTION_TEXT +
"\"},\"expand\":true}";

Block block = MAPPER.readValue(rawJson, Block.class);

assertThat(block)
.isInstanceOfSatisfying(
Section.class,
section -> assertThat(section.getExpand()).hasValue(true)
);
}
}
Loading