Skip to content

Commit

Permalink
mount text
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Oct 1, 2023
1 parent 860a383 commit cd16644
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/app/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ func (m nodeManager) Mount(depth uint, v UI) (UI, error) {
}

func (m nodeManager) mountText(depth uint, v *text) (UI, error) {
panic("not implemented")
if v.Mounted() {
return nil, errors.New("text is already mounted").
WithTag("parent-type", reflect.TypeOf(v.getParent())).
WithTag("preview-value", previewText(v.value))
}

v.jsvalue = Window().createTextNode(v.value)
return v, nil
}

func (m nodeManager) mountHTMLElement(depth uint, v HTML) (UI, error) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/app/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,26 @@ func TestHTMLString(t *testing.T) {
})
}
}

func TestNodeManagerMount(t *testing.T) {
t.Run("mounting a text succeeds", func(t *testing.T) {
var m nodeManager

text, err := m.Mount(1, Text("hello"))
require.NoError(t, err)
require.NotZero(t, text)
require.NotNil(t, text.JSValue())

})

t.Run("mounting an already mounted text returns an error", func(t *testing.T) {
var m nodeManager

text, err := m.Mount(1, Text("hello"))
require.NoError(t, err)

text, err = m.Mount(1, text)
require.Error(t, err)
require.Zero(t, text)
})
}
7 changes: 7 additions & 0 deletions pkg/app/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ func FormatString(format string, v ...any) string {
}
return fmt.Sprintf(format, v...)
}

func previewText(v string) string {
if len(v) <= 80 {
return v
}
return v[:77] + "..."
}
20 changes: 20 additions & 0 deletions pkg/app/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ func BenchmarkAppendClass(b *testing.B) {
AppendClass("hello", "foo-bar-k", "bar-lkj-adsf-adsf")
}
}

func TestPreviewText(t *testing.T) {
t.Run("full text is previewed", func(t *testing.T) {
require.Equal(t, "hello", previewText("hello"))
})

t.Run("text of 80 chara is previewed", func(t *testing.T) {
require.Equal(t,
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
previewText("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
)
})

t.Run("text over 80 chara is previewed", func(t *testing.T) {
require.Equal(t,
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...",
previewText("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxhello"),
)
})
}
2 changes: 1 addition & 1 deletion pkg/app/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *text) JSValue() Value {
}

func (t *text) Mounted() bool {
return t.jsvalue != nil && t.getDispatcher() != nil
return t.jsvalue != nil
}

func (t *text) name() string {
Expand Down

0 comments on commit cd16644

Please sign in to comment.