github.com/saucelabs/saucectl@v0.175.1/internal/notification/slack/slack_test.go (about)

     1  package slack
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/saucelabs/saucectl/internal/report"
     7  
     8  	"github.com/saucelabs/saucectl/internal/config"
     9  )
    10  
    11  func Test_shouldSendNotification(t *testing.T) {
    12  	type params struct {
    13  		testResults []report.TestResult
    14  		config      config.Notifications
    15  		passed      bool
    16  	}
    17  
    18  	testCases := []struct {
    19  		name     string
    20  		params   params
    21  		expected bool
    22  	}{
    23  		{
    24  			name: "empty slack channel",
    25  			params: params{
    26  				testResults: []report.TestResult{report.TestResult{URL: "http://example.com"}},
    27  				config:      config.Notifications{Slack: config.Slack{Channels: []string{}}},
    28  			},
    29  			expected: false,
    30  		},
    31  		{
    32  			name: "send always",
    33  			params: params{
    34  				testResults: []report.TestResult{report.TestResult{URL: "http://example.com"}},
    35  				config:      config.Notifications{Slack: config.Slack{Channels: []string{"test-channel"}, Send: config.WhenAlways}},
    36  				passed:      true,
    37  			},
    38  			expected: true,
    39  		},
    40  		{
    41  			name: "send pass",
    42  			params: params{
    43  				testResults: []report.TestResult{report.TestResult{URL: "http://example.com"}},
    44  				config:      config.Notifications{Slack: config.Slack{Channels: []string{"test-channel"}, Send: config.WhenPass}},
    45  				passed:      true,
    46  			},
    47  			expected: true,
    48  		},
    49  		{
    50  			name: "send on fail",
    51  			params: params{
    52  				testResults: []report.TestResult{report.TestResult{URL: "http://example.com"}},
    53  				config:      config.Notifications{Slack: config.Slack{Channels: []string{"test-channel"}, Send: config.WhenFail}},
    54  				passed:      false,
    55  			},
    56  			expected: true,
    57  		},
    58  		{
    59  			name: "default",
    60  			params: params{
    61  				testResults: []report.TestResult{report.TestResult{URL: "http://example.com"}},
    62  				config:      config.Notifications{Slack: config.Slack{Channels: []string{"test-channel"}}},
    63  				passed:      true,
    64  			},
    65  			expected: false,
    66  		},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		notifier := Reporter{
    71  			Config:      tc.params.config,
    72  			TestResults: tc.params.testResults,
    73  		}
    74  		got := notifier.shouldSendNotification(tc.params.passed)
    75  		if tc.expected != got {
    76  			t.Errorf("test case name: %s  got: %v expected: %v", tc.name, got, tc.expected)
    77  		}
    78  	}
    79  }
    80  
    81  func Test_addRightSpaces(t *testing.T) {
    82  	type params struct {
    83  		name      string
    84  		wholeName string
    85  		length    int
    86  	}
    87  	testCases := []struct {
    88  		name     string
    89  		params   params
    90  		expected string
    91  	}{
    92  		{
    93  			name: "add right spaces",
    94  			params: params{
    95  				name:      "very long name",
    96  				wholeName: "short name",
    97  				length:    20,
    98  			},
    99  			expected: "short name      ",
   100  		},
   101  		{
   102  			name: "wholeName stays the same",
   103  			params: params{
   104  				name:      "some long string",
   105  				wholeName: "short name",
   106  				length:    16,
   107  			},
   108  			expected: "short name",
   109  		},
   110  	}
   111  
   112  	for _, tc := range testCases {
   113  		got := addRightSpaces(tc.params.name, tc.params.wholeName, tc.params.length)
   114  		if got != tc.expected {
   115  			t.Errorf("test case name: %s  got: '%v' expected: %v", tc.name, got, tc.expected)
   116  		}
   117  	}
   118  }