Passed
Pull Request — main (#166)
by Yume
02:23
created

partials_test.TestNavbar   C

Complexity

Conditions 10

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nop 1
dl 0
loc 46
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like partials_test.TestNavbar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package partials_test
2
3
import (
4
	"context"
5
	"io"
6
	"testing"
7
8
	"github.com/PuerkitoBio/goquery"
9
	"github.com/memnix/memnix-rest/app/v2/views/partials"
10
)
11
12
func TestNavbar(t *testing.T) {
13
	r, w := io.Pipe()
14
	username, fromProtected := "", false
15
	go func() {
16
		_ = partials.Navbar(username, fromProtected).Render(context.Background(), w)
17
		_ = w.Close()
18
	}()
19
20
	doc, err := goquery.NewDocumentFromReader(r)
21
	if err != nil {
22
		t.Fatalf("Error reading document: %s", err)
23
	}
24
25
	// Assert that the navbar exists
26
	if doc.Find(`[data-testid="navbar"]`).Length() != 1 {
27
		t.Errorf("Expected to find a navbar")
28
	}
29
30
	// Assert that the navbar has a login button
31
	if doc.Find(`[data-testid="loginButton"]`).Length() != 1 {
32
		t.Errorf("Expected to find a login button")
33
	}
34
35
	// Assert that the navbar has a navbar start
36
	if doc.Find(`[data-testid="navbarStart"]`).Length() != 1 {
37
		t.Errorf("Expected to find a navbar start")
38
	}
39
40
	// Assert that the navbar has a navbar end
41
	if doc.Find(`[data-testid="navbarEnd"]`).Length() != 1 {
42
		t.Errorf("Expected to find a navbar end")
43
	}
44
45
	// Assert that the navbar has a navbar center
46
	if doc.Find(`[data-testid="navbarCenter"]`).Length() != 1 {
47
		t.Errorf("Expected to find a navbar center")
48
	}
49
50
	// Assert that the navbar does not have a username
51
	if doc.Find(`[data-testid="username"]`).Length() != 0 {
52
		t.Errorf("Expected to not find a username")
53
	}
54
55
	// Assert that the navbar does not have a logout button
56
	if doc.Find(`[data-testid="logoutButton"]`).Length() != 0 {
57
		t.Errorf("Expected to not find a logout button")
58
	}
59
}
60
61
func TestNavbarProtected(t *testing.T) {
62
	r, w := io.Pipe()
63
	username, fromProtected := "Test", true
64
	go func() {
65
		_ = partials.Navbar(username, fromProtected).Render(context.Background(), w)
66
		_ = w.Close()
67
	}()
68
69
	doc, err := goquery.NewDocumentFromReader(r)
70
	if err != nil {
71
		t.Fatalf("Error reading document: %s", err)
72
	}
73
74
	// Assert that the navbar exists
75
	if doc.Find(`[data-testid="navbar"]`).Length() != 1 {
76
		t.Errorf("Expected to find a navbar")
77
	}
78
79
	// Assert that the navbar has a logout button
80
	if doc.Find(`[data-testid="logoutButton"]`).Length() != 1 {
81
		t.Errorf("Expected to find a logout button")
82
	}
83
84
	// Assert that the navbar has a username
85
	if doc.Find(`[data-testid="username"]`).Length() != 1 {
86
		t.Errorf("Expected to find a username")
87
	}
88
}
89