-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some golden tests to ensure the encoding works properly
Co-authored-by: Violet <violet@mercury.com>
- Loading branch information
Showing
5 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ cabal.project.local | |
# generated by flake.nix | ||
.pre-commit-config.yaml | ||
*.actual | ||
*.actual.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module Web.Slack.Experimental.BlocksSpec where | ||
|
||
import Control.Monad.Writer.Strict | ||
import Data.StringVariants (mkNonEmptyText) | ||
import JSONGolden (oneGoldenTestEncode) | ||
import TestImport | ||
import Web.Slack.Experimental.Blocks | ||
import Web.Slack.Experimental.Blocks.Types | ||
|
||
-- FIXME(jadel, violet): These builder things should probably be put into a | ||
-- module to make them available, but that's later work | ||
|
||
headerBlock :: SlackText -> WriterT [SlackBlock] Identity () | ||
headerBlock = tell . pure . SlackBlockHeader . SlackPlainTextOnly | ||
|
||
sectionBlock :: SlackText -> WriterT [SlackBlock] Identity () | ||
sectionBlock text = tell . pure $ SlackBlockSection text Nothing | ||
|
||
sectionBlockWithAccessory :: | ||
SlackText -> | ||
SlackAction -> | ||
WriterT [SlackBlock] Identity () | ||
sectionBlockWithAccessory a b = tell . pure $ SlackBlockSection a (Just . SlackButtonAccessory $ b) | ||
|
||
dividerBlock :: WriterT [SlackBlock] Identity () | ||
dividerBlock = tell . pure $ SlackBlockDivider | ||
|
||
contextBlock :: [SlackContent] -> WriterT [SlackBlock] Identity () | ||
contextBlock = tell . pure . SlackBlockContext . SlackContext | ||
|
||
actionsBlock :: | ||
Maybe SlackBlockId -> | ||
SlackActionList -> | ||
WriterT [SlackBlock] Identity () | ||
actionsBlock a b = tell . pure $ SlackBlockActions a b | ||
|
||
slackActionDoNothing :: SlackActionId | ||
slackActionDoNothing = SlackActionId . fromJust . mkNonEmptyText $ "doNothing" | ||
|
||
slackActionDoNothing2 :: SlackActionId | ||
slackActionDoNothing2 = SlackActionId . fromJust . mkNonEmptyText $ "doNothing2" | ||
|
||
slackActionDoNothing3 :: SlackActionId | ||
slackActionDoNothing3 = SlackActionId . fromJust . mkNonEmptyText $ "doNothing3" | ||
|
||
slackShowBlockFormat :: BlockKitBuilderMessage | ||
slackShowBlockFormat = | ||
BlockKitBuilderMessage $ | ||
execWriter $ do | ||
headerBlock ("Blah: " <> list ["a", "b", "c"]) | ||
dividerBlock | ||
sectionBlock (bold $ list ["blah", "blah2", "blah3"]) | ||
sectionBlockWithAccessory | ||
(monospaced @Text "blah") | ||
(button slackActionDoNothing ":mag: Look at it" buttonSettings {buttonUrl = google}) | ||
sectionBlockWithAccessory | ||
(bold $ list ["blah", "blah2", "blah3"]) | ||
(button slackActionDoNothing ":office: Look at it but different" buttonSettings {buttonUrl = google}) | ||
sectionBlock (bold (textMessage "Letters:") <> newline (list ["a", "b", "c"])) | ||
sectionBlock (list ["blah1", "blah2", "blah3"]) | ||
dividerBlock | ||
sectionBlock (bold . textMessage $ "blah") | ||
sectionBlockWithAccessory | ||
(bold . textMessage $ "blah") | ||
(button slackActionDoNothing ":bank: Look at it!" buttonSettings {buttonUrl = google}) | ||
contextBlock [SlackContentText ":key: Context!"] | ||
dividerBlock | ||
actionsBlock Nothing $ | ||
toSlackActionList | ||
( button slackActionDoNothing ":mag: View" buttonSettings {buttonUrl = google} | ||
, button slackActionDoNothing2 ":office: View" buttonSettings {buttonUrl = google} | ||
, button slackActionDoNothing3 ":bank: View" buttonSettings {buttonUrl = google} | ||
) | ||
where | ||
textMessage = message @Text | ||
list x = textMessage (intercalate ", " x) | ||
google = OptionalSetting . mkNonEmptyText @3000 $ "https://google.com" | ||
|
||
-- | This is used so that we can paste the output of these tests directly into | ||
-- the block kit builder: | ||
-- <https://app.slack.com/block-kit-builder> | ||
newtype BlockKitBuilderMessage = BlockKitBuilderMessage {blocks :: [SlackBlock]} | ||
|
||
instance ToJSON BlockKitBuilderMessage where | ||
toJSON BlockKitBuilderMessage {blocks} = object [("blocks", toJSON blocks)] | ||
|
||
spec :: Spec | ||
spec = describe "Slack block builder" do | ||
oneGoldenTestEncode "simple_blocks" slackShowBlockFormat |
137 changes: 137 additions & 0 deletions
137
tests/golden/BlockKitBuilderMessage/simple_blocks.golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
{ | ||
"blocks": [ | ||
{ | ||
"text": { | ||
"text": "Blah: \na, b, c", | ||
"type": "plain_text" | ||
}, | ||
"type": "header" | ||
}, | ||
{ | ||
"type": "divider" | ||
}, | ||
{ | ||
"text": { | ||
"text": "*blah, blah2, blah3*", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"accessory": { | ||
"action_id": "doNothing", | ||
"text": { | ||
"text": ":mag: Look at it", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
}, | ||
"text": { | ||
"text": "`blah`", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"accessory": { | ||
"action_id": "doNothing", | ||
"text": { | ||
"text": ":office: Look at it but different", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
}, | ||
"text": { | ||
"text": "*blah, blah2, blah3*", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"text": { | ||
"text": "*Letters:*\na, b, c", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"text": { | ||
"text": "blah1, blah2, blah3", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"type": "divider" | ||
}, | ||
{ | ||
"text": { | ||
"text": "*blah*", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"accessory": { | ||
"action_id": "doNothing", | ||
"text": { | ||
"text": ":bank: Look at it!", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
}, | ||
"text": { | ||
"text": "*blah*", | ||
"type": "mrkdwn" | ||
}, | ||
"type": "section" | ||
}, | ||
{ | ||
"elements": [ | ||
{ | ||
"text": ":key: Context!", | ||
"type": "mrkdwn" | ||
} | ||
], | ||
"type": "context" | ||
}, | ||
{ | ||
"type": "divider" | ||
}, | ||
{ | ||
"elements": [ | ||
{ | ||
"action_id": "doNothing", | ||
"text": { | ||
"text": ":mag: View", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
}, | ||
{ | ||
"action_id": "doNothing2", | ||
"text": { | ||
"text": ":office: View", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
}, | ||
{ | ||
"action_id": "doNothing3", | ||
"text": { | ||
"text": ":bank: View", | ||
"type": "plain_text" | ||
}, | ||
"type": "button", | ||
"url": "https://google.com" | ||
} | ||
], | ||
"type": "actions" | ||
} | ||
] | ||
} |