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
21 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 0).Return(mockContainer).Once()
22 mockContainer.EXPECT().SetHexpand(true).Once()
23 mockContainer.EXPECT().SetVexpand(true).Once()
24 mockContainer.EXPECT().SetHalign(mock.Anything).Once()
25 mockContainer.EXPECT().SetValign(mock.Anything).Once()
26 mockContainer.EXPECT().SetCanFocus(false).Once()
27 mockContainer.EXPECT().SetCanTarget(false).Once()
28 mockContainer.EXPECT().AddCssClass("loading-skeleton").Once()
29
30 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 6).Return(mockContent).Once()
31 mockContent.EXPECT().SetHalign(mock.Anything).Once()
32 mockContent.EXPECT().SetValign(mock.Anything).Once()
33 mockContent.EXPECT().SetCanFocus(false).Once()
34 mockContent.EXPECT().SetCanTarget(false).Once()
35 mockContent.EXPECT().AddCssClass("loading-skeleton-content").Once()
36
37 mockFactory.EXPECT().NewSpinner().Return(mockSpinner).Once()
38 mockSpinner.EXPECT().SetHalign(mock.Anything).Once()
39 mockSpinner.EXPECT().SetValign(mock.Anything).Once()
40 mockSpinner.EXPECT().SetCanFocus(false).Once()
41 mockSpinner.EXPECT().SetCanTarget(false).Once()
42 mockSpinner.EXPECT().SetSizeRequest(32, 32).Once()
43 mockSpinner.EXPECT().AddCssClass("loading-skeleton-spinner").Once()
44
45 mockFactory.EXPECT().NewImage().Return(mockLogo).Once()
46 mockLogo.EXPECT().SetHalign(mock.Anything).Once()
47 mockLogo.EXPECT().SetValign(mock.Anything).Once()
48 mockLogo.EXPECT().SetCanFocus(false).Once()
49 mockLogo.EXPECT().SetCanTarget(false).Once()
50 mockLogo.EXPECT().SetSizeRequest(512, 512).Once()
51 mockLogo.EXPECT().SetPixelSize(512).Once()
52 mockLogo.EXPECT().AddCssClass("loading-skeleton-logo").Once()
53 mockLogo.EXPECT().SetFromPaintable(mock.Anything).Maybe()
54
55 mockContent.EXPECT().Append(mockLogo).Once()
56 mockContent.EXPECT().Append(mockSpinner).Once()
57 mockContainer.EXPECT().Append(mockContent).Once()
58
59 // NewLoadingSkeleton ends by calling SetVisible(true).
60 mockContainer.EXPECT().SetVisible(true).Once()
61 mockSpinner.EXPECT().Start().Once()
62
63 ls := component.NewLoadingSkeleton(mockFactory)
64 require.NotNil(t, ls)
65}
66
67func TestLoadingSkeleton_SetVisible_StopsSpinnerWhenHidden(t *testing.T) {
68 mockFactory := mocks.NewMockWidgetFactory(t)
69 mockContainer := mocks.NewMockBoxWidget(t)
70 mockContent := mocks.NewMockBoxWidget(t)
71 mockSpinner := mocks.NewMockSpinnerWidget(t)
72 mockLogo := mocks.NewMockImageWidget(t)
73
74 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 0).Return(mockContainer).Once()
75 mockContainer.EXPECT().SetHexpand(true).Once()
76 mockContainer.EXPECT().SetVexpand(true).Once()
77 mockContainer.EXPECT().SetHalign(mock.Anything).Once()
78 mockContainer.EXPECT().SetValign(mock.Anything).Once()
79 mockContainer.EXPECT().SetCanFocus(false).Once()
80 mockContainer.EXPECT().SetCanTarget(false).Once()
81 mockContainer.EXPECT().AddCssClass("loading-skeleton").Once()
82
83 mockFactory.EXPECT().NewBox(layout.OrientationVertical, 6).Return(mockContent).Once()
84 mockContent.EXPECT().SetHalign(mock.Anything).Once()
85 mockContent.EXPECT().SetValign(mock.Anything).Once()
86 mockContent.EXPECT().SetCanFocus(false).Once()
87 mockContent.EXPECT().SetCanTarget(false).Once()
88 mockContent.EXPECT().AddCssClass("loading-skeleton-content").Once()
89
90 mockFactory.EXPECT().NewSpinner().Return(mockSpinner).Once()
91 mockSpinner.EXPECT().SetHalign(mock.Anything).Once()
92 mockSpinner.EXPECT().SetValign(mock.Anything).Once()
93 mockSpinner.EXPECT().SetCanFocus(false).Once()
94 mockSpinner.EXPECT().SetCanTarget(false).Once()
95 mockSpinner.EXPECT().SetSizeRequest(32, 32).Once()
96 mockSpinner.EXPECT().AddCssClass("loading-skeleton-spinner").Once()
97
98 mockFactory.EXPECT().NewImage().Return(mockLogo).Once()
99 mockLogo.EXPECT().SetHalign(mock.Anything).Once()
100 mockLogo.EXPECT().SetValign(mock.Anything).Once()
101 mockLogo.EXPECT().SetCanFocus(false).Once()
102 mockLogo.EXPECT().SetCanTarget(false).Once()
103 mockLogo.EXPECT().SetSizeRequest(512, 512).Once()
104 mockLogo.EXPECT().SetPixelSize(512).Once()
105 mockLogo.EXPECT().AddCssClass("loading-skeleton-logo").Once()
106 mockLogo.EXPECT().SetFromPaintable(mock.Anything).Maybe()
107
108 mockContent.EXPECT().Append(mockLogo).Once()
109 mockContent.EXPECT().Append(mockSpinner).Once()
110 mockContainer.EXPECT().Append(mockContent).Once()
111
112 mockContainer.EXPECT().SetVisible(true).Once()
113 mockSpinner.EXPECT().Start().Once()
114
115 ls := component.NewLoadingSkeleton(mockFactory)
116 require.NotNil(t, ls)
117
118 mockContainer.EXPECT().SetVisible(false).Once()
119 mockSpinner.EXPECT().Stop().Once()
120 ls.SetVisible(false)
121}