Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more update func #50

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
})
}
Loading