Skip to content

Commit 9fd57f5

Browse files
committed
chore: test init singleton
1 parent acf17ac commit 9fd57f5

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

states/homepage.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ type HomePage struct {
1515
once sync.Once
1616
}
1717

18-
// homePageInstance holds the singleton instance of HomePage
19-
var homePageInstance *HomePage
18+
// HomePageInstance holds the singleton instance of HomePage
19+
var HomePageInstance *HomePage
2020

2121
// GetHomePage returns the singleton instance of the HomePage state
2222
func GetHomePage() *HomePage {
2323
// Use sync.Once to ensure the HomePage is initialized only once
24-
if homePageInstance == nil {
25-
homePageInstance = &HomePage{}
26-
homePageInstance.once.Do(func() {
27-
homePageInstance.BaseState = BaseState{
24+
if HomePageInstance == nil {
25+
HomePageInstance = &HomePage{}
26+
HomePageInstance.once.Do(func() {
27+
HomePageInstance.BaseState = BaseState{
2828
Transitions: []State{GetInitiaInit()}, // Ensure all transitions are properly initialized
2929
}
3030
})
3131
}
32-
return homePageInstance
32+
return HomePageInstance
3333
}
3434

3535
func (hp *HomePage) Init() tea.Cmd {

states/launch_minitia.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ type LaunchNewMinitia struct {
1414
once sync.Once
1515
}
1616

17-
// launchNewMinitiaInstance holds the singleton instance of LaunchNewMinitia
18-
var launchNewMinitiaInstance *LaunchNewMinitia
17+
// LaunchNewMinitiaInstance holds the singleton instance of LaunchNewMinitia
18+
var LaunchNewMinitiaInstance *LaunchNewMinitia
1919

2020
// GetLaunchNewMinitia returns the singleton instance of the LaunchNewMinitia state
2121
func GetLaunchNewMinitia() *LaunchNewMinitia {
22-
if launchNewMinitiaInstance == nil {
23-
launchNewMinitiaInstance = &LaunchNewMinitia{}
24-
launchNewMinitiaInstance.once.Do(func() {
25-
launchNewMinitiaInstance.BaseState = BaseState{
22+
if LaunchNewMinitiaInstance == nil {
23+
LaunchNewMinitiaInstance = &LaunchNewMinitia{}
24+
LaunchNewMinitiaInstance.once.Do(func() {
25+
LaunchNewMinitiaInstance.BaseState = BaseState{
2626
Transitions: []State{},
2727
}
2828
})
2929
}
30-
return launchNewMinitiaInstance
30+
return LaunchNewMinitiaInstance
3131
}
3232

3333
func (lnm *LaunchNewMinitia) Init() tea.Cmd {

states/run_l1_node.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ type RunL1Node struct {
1414
once sync.Once
1515
}
1616

17-
// runL1NodeInstance holds the singleton instance of RunL1Node
18-
var runL1NodeInstance *RunL1Node
17+
// RunL1NodeInstance holds the singleton instance of RunL1Node
18+
var RunL1NodeInstance *RunL1Node
1919

2020
// GetRunL1Node returns the singleton instance of the RunL1Node state
2121
func GetRunL1Node() *RunL1Node {
22-
if runL1NodeInstance == nil {
23-
runL1NodeInstance = &RunL1Node{}
24-
runL1NodeInstance.once.Do(func() {
25-
runL1NodeInstance.BaseState = BaseState{
22+
if RunL1NodeInstance == nil {
23+
RunL1NodeInstance = &RunL1Node{}
24+
RunL1NodeInstance.once.Do(func() {
25+
RunL1NodeInstance.BaseState = BaseState{
2626
Transitions: []State{},
2727
}
2828
})
2929
}
30-
return runL1NodeInstance
30+
return RunL1NodeInstance
3131
}
3232

3333
func (rl1 *RunL1Node) Init() tea.Cmd {

states/state.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66

77
// Example Reset function for all states:
88
func ResetStates() {
9-
homePageInstance = nil
9+
HomePageInstance = nil
1010
InitiaInitInstance = nil
11-
runL1NodeInstance = nil
12-
launchNewMinitiaInstance = nil
11+
RunL1NodeInstance = nil
12+
LaunchNewMinitiaInstance = nil
1313
}
1414

1515
type State interface {

states/states_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"github.com/initia-labs/weave/states"
1010
)
1111

12+
func TestSingletonInitializations(t *testing.T) {
13+
// Reset all singleton instances before testing to ensure a clean state
14+
states.ResetStates()
15+
states.GetHomePage()
16+
17+
// Assert that none of the singleton instances are nil
18+
assert.NotNil(t, states.HomePageInstance, "HomePage instance should not be nil")
19+
assert.NotNil(t, states.InitiaInitInstance, "InitiaInit instance should not be nil")
20+
assert.NotNil(t, states.RunL1NodeInstance, "RunL1Node instance should not be nil")
21+
assert.NotNil(t, states.LaunchNewMinitiaInstance, "LaunchNewMinitia instance should not be nil")
22+
}
23+
1224
func TestInitiaInitNavigation(t *testing.T) {
1325
states.ResetStates() // Optional: Reset at package initialization if needed for all tests
1426
home := states.GetHomePage()

0 commit comments

Comments
 (0)