github.com/saucelabs/saucectl@v0.175.1/internal/report/table/table_test.go (about)

     1  package table
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/saucelabs/saucectl/internal/job"
    10  	"github.com/saucelabs/saucectl/internal/report"
    11  )
    12  
    13  func TestReporter_Render(t *testing.T) {
    14  	type fields struct {
    15  		TestResults []report.TestResult
    16  	}
    17  	startTime := time.Now()
    18  	tests := []struct {
    19  		name   string
    20  		fields fields
    21  		want   string
    22  	}{
    23  		{
    24  			name: "all pass",
    25  			fields: fields{
    26  				TestResults: []report.TestResult{
    27  					{
    28  						Name:          "Firefox",
    29  						Duration:      34479 * time.Millisecond,
    30  						StartTime:     startTime,
    31  						EndTime:       startTime.Add(34479 * time.Millisecond),
    32  						Status:        job.StatePassed,
    33  						Browser:       "Firefox",
    34  						Platform:      "Windows 10",
    35  						PassThreshold: true,
    36  						Attempts: []report.Attempt{
    37  							{Status: job.StateFailed},
    38  							{Status: job.StateFailed},
    39  							{Status: job.StatePassed},
    40  						},
    41  					},
    42  					{
    43  						Name:          "Chrome",
    44  						Duration:      5123 * time.Millisecond,
    45  						StartTime:     startTime,
    46  						EndTime:       startTime.Add(5123 * time.Millisecond),
    47  						Status:        job.StatePassed,
    48  						Browser:       "Chrome",
    49  						Platform:      "Windows 10",
    50  						PassThreshold: true,
    51  						Attempts: []report.Attempt{
    52  							{Status: job.StatePassed},
    53  						},
    54  					},
    55  				},
    56  			},
    57  			want: `
    58         Name                              Duration    Status    Browser    Platform      Attempts  
    59  ──────────────────────────────────────────────────────────────────────────────────────────────────
    60    ✔    Firefox                                34s    passed    Firefox    Windows 10           3  
    61    ✔    Chrome                                  5s    passed    Chrome     Windows 10           1  
    62  ──────────────────────────────────────────────────────────────────────────────────────────────────
    63    ✔    All suites have passed                 34s                                                 
    64  `,
    65  		},
    66  		{
    67  			name: "with failure",
    68  			fields: fields{
    69  				TestResults: []report.TestResult{
    70  					{
    71  						Name:      "Firefox",
    72  						Duration:  34479 * time.Millisecond,
    73  						StartTime: startTime,
    74  						EndTime:   startTime.Add(34479 * time.Millisecond),
    75  						Status:    job.StatePassed,
    76  						Browser:   "Firefox",
    77  						Platform:  "Windows 10",
    78  						Attempts: []report.Attempt{
    79  							{Status: job.StatePassed},
    80  						},
    81  					},
    82  					{
    83  						Name:      "Chrome",
    84  						Duration:  171452 * time.Millisecond,
    85  						StartTime: startTime,
    86  						EndTime:   startTime.Add(171452 * time.Millisecond),
    87  						Status:    job.StateFailed,
    88  						Browser:   "Chrome",
    89  						Platform:  "Windows 10",
    90  						Attempts: []report.Attempt{
    91  							{Status: job.StateFailed},
    92  							{Status: job.StateFailed},
    93  							{Status: job.StateFailed},
    94  						},
    95  					},
    96  				},
    97  			},
    98  			want: `
    99         Name                               Duration    Status    Browser    Platform      Attempts  
   100  ───────────────────────────────────────────────────────────────────────────────────────────────────
   101    ✔    Firefox                                 34s    passed    Firefox    Windows 10           1  
   102    ✖    Chrome                                2m51s    failed    Chrome     Windows 10           3  
   103  ───────────────────────────────────────────────────────────────────────────────────────────────────
   104    ✖    1 of 2 suites have failed (50%)       2m51s                                                 
   105  `,
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			var buffy bytes.Buffer
   111  
   112  			r := &Reporter{
   113  				TestResults: tt.fields.TestResults,
   114  				Dst:         &buffy,
   115  			}
   116  			r.Render()
   117  
   118  			out := buffy.String()
   119  			if !reflect.DeepEqual(out, tt.want) {
   120  				t.Errorf("Render() got = \n%s, want = \n%s", out, tt.want)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func TestReporter_Reset(t *testing.T) {
   127  	type fields struct {
   128  		TestResults []report.TestResult
   129  	}
   130  	tests := []struct {
   131  		name   string
   132  		fields fields
   133  	}{
   134  		{
   135  			name: "expect empty render",
   136  			fields: fields{
   137  				TestResults: []report.TestResult{
   138  					{
   139  						Name:     "Firefox",
   140  						Duration: 34479 * time.Millisecond,
   141  						Status:   job.StatePassed,
   142  						Browser:  "Firefox",
   143  						Platform: "Windows 10",
   144  					}},
   145  			},
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			r := &Reporter{
   151  				TestResults: tt.fields.TestResults,
   152  			}
   153  			r.Reset()
   154  
   155  			if len(r.TestResults) != 0 {
   156  				t.Errorf("len(TestResults) got = %d, want = %d", len(r.TestResults), 0)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func TestReporter_Add(t *testing.T) {
   163  	type fields struct {
   164  		TestResults []report.TestResult
   165  	}
   166  	type args struct {
   167  		t report.TestResult
   168  	}
   169  	tests := []struct {
   170  		name   string
   171  		fields fields
   172  		args   args
   173  	}{
   174  		{
   175  			name:   "just one",
   176  			fields: fields{},
   177  			args: args{
   178  				t: report.TestResult{
   179  					Name:     "Firefox",
   180  					Duration: 34479 * time.Millisecond,
   181  					Status:   job.StatePassed,
   182  					Browser:  "Firefox",
   183  					Platform: "Windows 10",
   184  				},
   185  			},
   186  		},
   187  	}
   188  	for _, tt := range tests {
   189  		t.Run(tt.name, func(t *testing.T) {
   190  			r := &Reporter{
   191  				TestResults: tt.fields.TestResults,
   192  			}
   193  			r.Add(tt.args.t)
   194  
   195  			if len(r.TestResults) != 1 {
   196  				t.Errorf("len(TestResults) got = %d, want = %d", len(r.TestResults), 1)
   197  			}
   198  			if !reflect.DeepEqual(r.TestResults[0], tt.args.t) {
   199  				t.Errorf(" got = %v, want = %v", r.TestResults[0], tt.args.t)
   200  			}
   201  		})
   202  	}
   203  }