From 5e5c9c94ed84795704d058f279c45330e7f8534f Mon Sep 17 00:00:00 2001 From: CorrectRoad Date: Thu, 15 Aug 2024 11:59:04 +0900 Subject: [PATCH] feat: add more update func (#50) --- pkg/ysk/adapter.go | 14 ++++++++ pkg/ysk/adapter_test.go | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/pkg/ysk/adapter.go b/pkg/ysk/adapter.go index 5fe82a1..0be496e 100644 --- a/pkg/ysk/adapter.go +++ b/pkg/ysk/adapter.go @@ -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 { diff --git a/pkg/ysk/adapter_test.go b/pkg/ysk/adapter_test.go index 8bb0a64..069bbea 100644 --- a/pkg/ysk/adapter_test.go +++ b/pkg/ysk/adapter_test.go @@ -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]) + }) +}