loading_skeleton_test.go RawHistoryFolder
1package component_test
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/mock"
7 "github.com/stretchr/testify/require"
8
9 "github.com/bnema/dumber/internal/ui/component"
10 "github.com/bnema/dumber/internal/ui/layout"
11 "github.com/bnema/dumber/internal/ui/layout/mocks"
12)
13
14func TestNewLoadingSkeleton_ShowsAndStartsSpinner(t *testing.T) {
15 mockFactory := mocks.NewMockWidgetFactory(t)
16 mockContainer := mocks.NewMockBoxWidget(t)
17 mockContent := mocks.NewMockBoxWidget(t)
18 mockSpinner := mocks.NewMockSpinnerWidget(t)
19 mockLogo := mocks.NewMockImageWidget(t)
20 mockVersion := mocks.NewMockLabelWidget(t)
21
22 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 0).Return(mockContainer).Once()
23 mockContainer.EXPECT().SetHexpand(true).Once()
24 mockContainer.EXPECT().SetVexpand(true).Once()
25 mockContainer.EXPECT().SetHalign(mock.Anything).Once()
26 mockContainer.EXPECT().SetValign(mock.Anything).Once()
27 mockContainer.EXPECT().SetCanFocus(false).Once()
28 mockContainer.EXPECT().SetCanTarget(false).Once()
29 mockContainer.EXPECT().AddCssClass("loading-skeleton").Once()
30
31 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 6).Return(mockContent).Once()
32 mockContent.EXPECT().SetHalign(mock.Anything).Once()
33 mockContent.EXPECT().SetValign(mock.Anything).Once()
34 mockContent.EXPECT().SetCanFocus(false).Once()
35 mockContent.EXPECT().SetCanTarget(false).Once()
36 mockContent.EXPECT().AddCssClass("loading-skeleton-content").Once()
37
38 mockFactory.EXPECT().NewImage().Return(mockLogo).Once()
39 mockLogo.EXPECT().SetHalign(mock.Anything).Once()
40 mockLogo.EXPECT().SetValign(mock.Anything).Once()
41 mockLogo.EXPECT().SetCanFocus(false).Once()
42 mockLogo.EXPECT().SetCanTarget(false).Once()
43 mockLogo.EXPECT().SetSizeRequest(mock.Anything, mock.Anything).Once()
44 mockLogo.EXPECT().SetPixelSize(mock.Anything).Once()
45 mockLogo.EXPECT().AddCssClass("loading-skeleton-logo").Once()
46 mockLogo.EXPECT().SetFromPaintable(mock.Anything).Maybe()
47
48 mockFactory.EXPECT().NewSpinner().Return(mockSpinner).Once()
49 mockSpinner.EXPECT().SetHalign(mock.Anything).Once()
50 mockSpinner.EXPECT().SetValign(mock.Anything).Once()
51 mockSpinner.EXPECT().SetCanFocus(false).Once()
52 mockSpinner.EXPECT().SetCanTarget(false).Once()
53 mockSpinner.EXPECT().SetSizeRequest(mock.Anything, mock.Anything).Once()
54 mockSpinner.EXPECT().AddCssClass("loading-skeleton-spinner").Once()
55
56 mockFactory.EXPECT().NewLabel(mock.Anything).Return(mockVersion).Once()
57 mockVersion.EXPECT().SetHalign(mock.Anything).Once()
58 mockVersion.EXPECT().SetValign(mock.Anything).Once()
59 mockVersion.EXPECT().SetCanFocus(false).Once()
60 mockVersion.EXPECT().SetCanTarget(false).Once()
61 mockVersion.EXPECT().SetMaxWidthChars(mock.Anything).Once()
62 mockVersion.EXPECT().SetEllipsize(mock.Anything).Once()
63 mockVersion.EXPECT().AddCssClass("loading-skeleton-version").Once()
64
65 mockContent.EXPECT().Append(mockLogo).Once()
66 mockContent.EXPECT().Append(mockSpinner).Once()
67 mockContent.EXPECT().Append(mockVersion).Once()
68 mockContainer.EXPECT().Append(mockContent).Once()
69
70 // NewLoadingSkeleton ends by calling SetVisible(true).
71 mockContainer.EXPECT().SetVisible(true).Once()
72 mockSpinner.EXPECT().Start().Once()
73
74 ls := component.NewLoadingSkeleton(mockFactory)
75 require.NotNil(t, ls)
76}
77
78func TestLoadingSkeleton_SetVisible_StopsSpinnerWhenHidden(t *testing.T) {
79 mockFactory := mocks.NewMockWidgetFactory(t)
80 mockContainer := mocks.NewMockBoxWidget(t)
81 mockContent := mocks.NewMockBoxWidget(t)
82 mockSpinner := mocks.NewMockSpinnerWidget(t)
83 mockLogo := mocks.NewMockImageWidget(t)
84 mockVersion := mocks.NewMockLabelWidget(t)
85
86 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 0).Return(mockContainer).Once()
87 mockContainer.EXPECT().SetHexpand(true).Once()
88 mockContainer.EXPECT().SetVexpand(true).Once()
89 mockContainer.EXPECT().SetHalign(mock.Anything).Once()
90 mockContainer.EXPECT().SetValign(mock.Anything).Once()
91 mockContainer.EXPECT().SetCanFocus(false).Once()
92 mockContainer.EXPECT().SetCanTarget(false).Once()
93 mockContainer.EXPECT().AddCssClass("loading-skeleton").Once()
94
95 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 6).Return(mockContent).Once()
96 mockContent.EXPECT().SetHalign(mock.Anything).Once()
97 mockContent.EXPECT().SetValign(mock.Anything).Once()
98 mockContent.EXPECT().SetCanFocus(false).Once()
99 mockContent.EXPECT().SetCanTarget(false).Once()
100 mockContent.EXPECT().AddCssClass("loading-skeleton-content").Once()
101
102 mockFactory.EXPECT().NewImage().Return(mockLogo).Once()
103 mockLogo.EXPECT().SetHalign(mock.Anything).Once()
104 mockLogo.EXPECT().SetValign(mock.Anything).Once()
105 mockLogo.EXPECT().SetCanFocus(false).Once()
106 mockLogo.EXPECT().SetCanTarget(false).Once()
107 mockLogo.EXPECT().SetSizeRequest(mock.Anything, mock.Anything).Once()
108 mockLogo.EXPECT().SetPixelSize(mock.Anything).Once()
109 mockLogo.EXPECT().AddCssClass("loading-skeleton-logo").Once()
110 mockLogo.EXPECT().SetFromPaintable(mock.Anything).Maybe()
111
112 mockFactory.EXPECT().NewSpinner().Return(mockSpinner).Once()
113 mockSpinner.EXPECT().SetHalign(mock.Anything).Once()
114 mockSpinner.EXPECT().SetValign(mock.Anything).Once()
115 mockSpinner.EXPECT().SetCanFocus(false).Once()
116 mockSpinner.EXPECT().SetCanTarget(false).Once()
117 mockSpinner.EXPECT().SetSizeRequest(mock.Anything, mock.Anything).Once()
118 mockSpinner.EXPECT().AddCssClass("loading-skeleton-spinner").Once()
119
120 mockFactory.EXPECT().NewLabel(mock.Anything).Return(mockVersion).Once()
121 mockVersion.EXPECT().SetHalign(mock.Anything).Once()
122 mockVersion.EXPECT().SetValign(mock.Anything).Once()
123 mockVersion.EXPECT().SetCanFocus(false).Once()
124 mockVersion.EXPECT().SetCanTarget(false).Once()
125 mockVersion.EXPECT().SetMaxWidthChars(mock.Anything).Once()
126 mockVersion.EXPECT().SetEllipsize(mock.Anything).Once()
127 mockVersion.EXPECT().AddCssClass("loading-skeleton-version").Once()
128
129 mockContent.EXPECT().Append(mockLogo).Once()
130 mockContent.EXPECT().Append(mockSpinner).Once()
131 mockContent.EXPECT().Append(mockVersion).Once()
132 mockContainer.EXPECT().Append(mockContent).Once()
133
134 mockContainer.EXPECT().SetVisible(true).Once()
135 mockSpinner.EXPECT().Start().Once()
136
137 ls := component.NewLoadingSkeleton(mockFactory)
138 require.NotNil(t, ls)
139
140 mockContainer.EXPECT().SetVisible(false).Once()
141 mockSpinner.EXPECT().Stop().Once()
142 ls.SetVisible(false)
143}