Skip to content

Commit

Permalink
more container tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 18, 2023
1 parent d3bc69e commit acfc32c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 20 deletions.
120 changes: 120 additions & 0 deletions uicontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package jaws

import (
"html/template"
"net/http"
"net/http/httptest"
"reflect"
"slices"
"strings"
"testing"

"github.com/linkdata/jaws/what"
)

type testContainer struct{ contents []UI }
Expand Down Expand Up @@ -60,3 +66,117 @@ func TestRequest_Container(t *testing.T) {
})
}
}

func TestRequest_Container_Alteration(t *testing.T) {
span1 := NewUiSpan(makeHtmlGetter("span1"))
span2 := NewUiSpan(makeHtmlGetter("span2"))
span3 := NewUiSpan(makeHtmlGetter("span3"))
tests := []struct {
name string
c *testContainer
l []UI
want []wsMsg
}{
{
name: "no change",
c: &testContainer{contents: []UI{span1, span2, span3}},
l: []UI{span1, span2, span3},
want: []wsMsg{},
},
{
name: "add one to empty",
c: &testContainer{},
l: []UI{span1},
want: []wsMsg{
{
Data: `<span id="Jid.2">span1</span>`,
Jid: 1,
What: what.Append,
},
{
Data: `Jid.2`,
Jid: 1,
What: what.Order,
},
},
},
{
name: "append two",
c: &testContainer{contents: []UI{span1}},
l: []UI{span1, span2, span3},
want: []wsMsg{
{
Data: `<span id="Jid.3">span2</span>`,
Jid: 1,
What: what.Append,
},
{
Data: `<span id="Jid.4">span3</span>`,
Jid: 1,
What: what.Append,
},
{
Data: `Jid.2 Jid.3 Jid.4`,
Jid: 1,
What: what.Order,
},
},
},
{
name: "remove first",
c: &testContainer{contents: []UI{span1, span2, span3}},
l: []UI{span2, span3},
want: []wsMsg{
{
Data: `Jid.2`,
Jid: 1,
What: what.Remove,
},
{
Data: `Jid.3 Jid.4`,
Jid: 1,
What: what.Order,
},
},
},
{
name: "reorder and replace",
c: &testContainer{contents: []UI{span1, span2}},
l: []UI{span3, span1},
want: []wsMsg{
{
Data: `Jid.3`,
Jid: 1,
What: what.Remove,
},
{
Data: `<span id="Jid.4">span3</span>`,
Jid: 1,
What: what.Append,
},
{
Data: `Jid.4 Jid.2`,
Jid: 1,
What: what.Order,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jw := New()
defer jw.Close()
nextJid = 0
rq := jw.NewRequest(httptest.NewRequest(http.MethodGet, "/", nil))
ui := NewUiContainer("div", tt.c)
elem := rq.NewElement(ui)
var sb strings.Builder
ui.JawsRender(elem, &sb, nil)
tt.c.contents = tt.l
ui.JawsUpdate(elem)
if !slices.Equal(elem.wsQueue, tt.want) {
t.Errorf("got %v, want %v", elem.wsQueue, tt.want)
}
})
}
}
26 changes: 6 additions & 20 deletions uiwrapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jaws
import (
"html/template"
"io"
"slices"
"strings"

"github.com/linkdata/deadlock"
Expand All @@ -27,10 +28,9 @@ func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag
_, err := w.Write(b)
if err == nil {
for _, cui := range ui.Container.JawsContains(e.Request) {
if elem := e.Request.NewElement(cui); elem != nil {
ui.contents = append(ui.contents, elem)
elem.Render(w, nil)
}
elem := e.Request.NewElement(cui)
ui.contents = append(ui.contents, elem)
elem.Render(w, nil)
}
b = b[:0]
b = append(b, "</"...)
Expand All @@ -41,18 +41,6 @@ func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag
maybePanic(err)
}

func sameOrder(a, b []Jid) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func (ui *uiWrapContainer) JawsUpdate(e *Element) {
var toRemove, toAppend []*Element
var orderData []Jid
Expand All @@ -77,9 +65,7 @@ func (ui *uiWrapContainer) JawsUpdate(e *Element) {
for _, cui := range newContents {
var elem *Element
if elem = oldMap[cui]; elem == nil {
if elem = e.Request.NewElement(cui); elem == nil {
continue
}
elem = e.Request.NewElement(cui)
toAppend = append(toAppend, elem)
}
ui.contents = append(ui.contents, elem)
Expand All @@ -98,7 +84,7 @@ func (ui *uiWrapContainer) JawsUpdate(e *Element) {
e.Append(template.HTML(sb.String())) // #nosec G203
}

if !sameOrder(oldOrder, orderData) {
if !slices.Equal(oldOrder, orderData) {
e.Order(orderData)
}
}

0 comments on commit acfc32c

Please sign in to comment.