| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | func TestNewImageBlockObject(t *testing.T) { |
| 11 | |
| 12 | imageObject := NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/beagle.png", "Beagle") |
| 13 | |
| 14 | assert.Equal(t, string(imageObject.Type), "image") |
| 15 | assert.Equal(t, imageObject.AltText, "Beagle") |
| 16 | assert.Contains(t, imageObject.ImageURL, "beagle.png") |
| 17 | |
| 18 | } |
| 19 | |
| 20 | func TestNewTextBlockObject(t *testing.T) { |
| 21 | |
| 22 | textObject := NewTextBlockObject("plain_text", "test", true, false) |
| 23 | |
| 24 | assert.Equal(t, textObject.Type, "plain_text") |
| 25 | assert.Equal(t, textObject.Text, "test") |
| 26 | assert.True(t, textObject.Emoji, "Emoji property should be true") |
| 27 | assert.False(t, textObject.Verbatim, "Verbatim should be false") |
| 28 | |
| 29 | } |
| 30 | |
| 31 | func TestNewConfirmationBlockObject(t *testing.T) { |
| 32 | |
| 33 | titleObj := NewTextBlockObject("plain_text", "testTitle", false, false) |
| 34 | textObj := NewTextBlockObject("plain_text", "testText", false, false) |
| 35 | confirmObj := NewTextBlockObject("plain_text", "testConfirm", false, false) |
| 36 | |
| 37 | confirmation := NewConfirmationBlockObject(titleObj, textObj, confirmObj, nil) |
| 38 | |
| 39 | assert.Equal(t, confirmation.Title.Text, "testTitle") |
| 40 | assert.Equal(t, confirmation.Text.Text, "testText") |
| 41 | assert.Equal(t, confirmation.Confirm.Text, "testConfirm") |
| 42 | assert.Nil(t, confirmation.Deny, "Deny should be nil") |
| 43 | |
| 44 | } |
| 45 | |
| 46 | func TestWithStyleForConfirmation(t *testing.T) { |
| 47 | |
| 48 | // these values are irrelevant in this test |
| 49 | titleObj := NewTextBlockObject("plain_text", "testTitle", false, false) |
| 50 | textObj := NewTextBlockObject("plain_text", "testText", false, false) |
| 51 | confirmObj := NewTextBlockObject("plain_text", "testConfirm", false, false) |
| 52 | confirmation := NewConfirmationBlockObject(titleObj, textObj, confirmObj, nil) |
| 53 | |
| 54 | confirmation.WithStyle(StyleDefault) |
| 55 | assert.Equal(t, confirmation.Style, Style("")) |
| 56 | confirmation.WithStyle(StylePrimary) |
| 57 | assert.Equal(t, confirmation.Style, Style("primary")) |
| 58 | confirmation.WithStyle(StyleDanger) |
| 59 | assert.Equal(t, confirmation.Style, Style("danger")) |
| 60 | |
| 61 | } |
| 62 | |
| 63 | func TestNewOptionBlockObject(t *testing.T) { |
| 64 | |
| 65 | valTextObj := NewTextBlockObject("plain_text", "testText", false, false) |
| 66 | valDescriptionObj := NewTextBlockObject("plain_text", "testDescription", false, false) |
| 67 | optObj := NewOptionBlockObject("testOpt", valTextObj, valDescriptionObj) |
| 68 | |
| 69 | assert.Equal(t, optObj.Text.Text, "testText") |
| 70 | assert.Equal(t, optObj.Description.Text, "testDescription") |
| 71 | assert.Equal(t, optObj.Value, "testOpt") |
| 72 | |
| 73 | } |
| 74 | |
| 75 | func TestNewOptionGroupBlockElement(t *testing.T) { |
| 76 | |
| 77 | labelObj := NewTextBlockObject("plain_text", "testLabel", false, false) |
| 78 | valTextObj := NewTextBlockObject("plain_text", "testText", false, false) |
| 79 | optObj := NewOptionBlockObject("testOpt", valTextObj, nil) |
| 80 | |
| 81 | optGroup := NewOptionGroupBlockElement(labelObj, optObj) |
| 82 | |
| 83 | assert.Equal(t, optGroup.Label.Text, "testLabel") |
| 84 | assert.Len(t, optGroup.Options, 1, "Options should contain one element") |
| 85 | |
| 86 | } |
| 87 | |
| 88 | func TestValidateTextBlockObject(t *testing.T) { |
| 89 | tests := []struct { |
| 90 | input TextBlockObject |
| 91 | expected error |
| 92 | }{ |
| 93 | { |
| 94 | input: TextBlockObject{ |
| 95 | Type: "plain_text", |
| 96 | Text: "testText", |
| 97 | Emoji: false, |
| 98 | Verbatim: false, |
| 99 | }, |
| 100 | expected: nil, |
| 101 | }, |
| 102 | { |
| 103 | input: TextBlockObject{ |
| 104 | Type: "mrkdwn", |
| 105 | Text: "testText", |
| 106 | Emoji: false, |
| 107 | Verbatim: false, |
| 108 | }, |
| 109 | expected: nil, |
| 110 | }, |
| 111 | { |
| 112 | input: TextBlockObject{ |
| 113 | Type: "invalid", |
| 114 | Text: "testText", |
| 115 | Emoji: false, |
| 116 | Verbatim: false, |
| 117 | }, |
| 118 | expected: errors.New("type must be either of plain_text or mrkdwn"), |
| 119 | }, |
| 120 | { |
| 121 | input: TextBlockObject{ |
| 122 | Type: "mrkdwn", |
| 123 | Text: "testText", |
| 124 | Emoji: true, |
| 125 | Verbatim: false, |
| 126 | }, |
| 127 | expected: errors.New("emoji cannot be true in mrkdown"), |
| 128 | }, |
| 129 | } |
| 130 | |
| 131 | for _, test := range tests { |
| 132 | err := test.input.Validate() |
| 133 | assert.Equal(t, err, test.expected) |
| 134 | } |
| 135 | } |