Skip to content

Commit

Permalink
feat: add more update func (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Aug 15, 2024
1 parent 4d8680d commit 5e5c9c9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/ysk/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ func (yskCard YSKCard) WithList(params []YSKCardListItem) YSKCard {
return yskCard
}

func (yskCard YSKCard) WithIconText(icon YSKCardIcon, description string) YSKCard {
yskCard.Content.BodyIconWithText = &YSKCardIconWithText{
Icon: icon,
Description: description,
}
return yskCard
}

// replace the all old action
func (yskCard YSKCard) WithFooterActions(actions []YSKCardFooterAction) YSKCard {
yskCard.Content.FooterActions = actions
return yskCard
}

// it will replace the old action by same side and style
func (YSKCard YSKCard) UpsertFooterAction(action YSKCardFooterAction) YSKCard {
for i, a := range YSKCard.Content.FooterActions {
Expand Down
72 changes: 72 additions & 0 deletions pkg/ysk/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,75 @@ func TestJsonIsSame(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, equal, true)
}

func TestWithFunc(t *testing.T) {
t.Run("WithId", func(t *testing.T) {
card := ysk.YSKCard{}
updatedCard := card.WithId("test-id")
assert.Equal(t, "test-id", updatedCard.Id)
})

t.Run("WithTaskContent", func(t *testing.T) {
card := ysk.YSKCard{}
updatedCard := card.WithTaskContent(ysk.FileIcon, "Test Title")
assert.Equal(t, ysk.FileIcon, updatedCard.Content.TitleIcon)
assert.Equal(t, "Test Title", updatedCard.Content.TitleText)
})

t.Run("WithProgress", func(t *testing.T) {
card := ysk.YSKCard{
Content: ysk.YSKCardContent{
BodyProgress: &ysk.YSKCardProgress{
Label: "hello",
Progress: 20,
},
},
}
updatedCard := card.WithProgress("Progress", 50)
assert.Assert(t, updatedCard.Content.BodyProgress != nil)
assert.Equal(t, "Progress", updatedCard.Content.BodyProgress.Label)
assert.Equal(t, 50, updatedCard.Content.BodyProgress.Progress)
})

t.Run("WithList", func(t *testing.T) {
card := ysk.YSKCard{}
listItems := []ysk.YSKCardListItem{
{Icon: ysk.FileIcon, Description: "Item 1", RightText: "Right Text 1"},
{Icon: ysk.DiskIcon, Description: "Item 2", RightText: "Right Text 2"},
}
updatedCard := card.WithList(listItems)
assert.DeepEqual(t, listItems, updatedCard.Content.BodyList)
})

t.Run("WithIconText", func(t *testing.T) {
card := ysk.YSKCard{}
updatedCard := card.WithIconText(ysk.StorageIcon, "Storage Description")
assert.Assert(t, updatedCard.Content.BodyIconWithText != nil)
assert.Equal(t, ysk.StorageIcon, updatedCard.Content.BodyIconWithText.Icon)
assert.Equal(t, "Storage Description", updatedCard.Content.BodyIconWithText.Description)
})

t.Run("WithFooterActions", func(t *testing.T) {
card := ysk.YSKCard{}
actions := []ysk.YSKCardFooterAction{
{Side: ysk.ActionPositionLeft, Style: "primary", Text: "Confirm", MessageBus: ysk.YSKCardMessageBusAction{Key: "action1", Payload: "payload1"}},
{Side: ysk.ActionPositionRight, Style: "secondary", Text: "Cancel", MessageBus: ysk.YSKCardMessageBusAction{Key: "action2", Payload: "payload2"}},
}
updatedCard := card.WithFooterActions(actions)
assert.DeepEqual(t, actions, updatedCard.Content.FooterActions)
})

t.Run("UpsertFooterAction", func(t *testing.T) {
card := ysk.YSKCard{
Content: ysk.YSKCardContent{
FooterActions: []ysk.YSKCardFooterAction{
{Side: ysk.ActionPositionLeft, Style: "primary", Text: "Old Button", MessageBus: ysk.YSKCardMessageBusAction{Key: "old", Payload: "old"}},
},
},
}
newAction := ysk.YSKCardFooterAction{Side: ysk.ActionPositionLeft, Style: "primary", Text: "New Button", MessageBus: ysk.YSKCardMessageBusAction{Key: "new", Payload: "new"}}
updatedCard := card.UpsertFooterAction(newAction)
assert.Equal(t, 1, len(updatedCard.Content.FooterActions))
assert.DeepEqual(t, newAction, updatedCard.Content.FooterActions[0])
})
}

0 comments on commit 5e5c9c9

Please sign in to comment.