Skip to content
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
6 changes: 6 additions & 0 deletions jaws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jaws
import (
"html/template"
"io"
"net/http"
"sync"
"time"

Expand Down Expand Up @@ -49,6 +50,7 @@ type (
With = pkg.With
Session = pkg.Session
Tag = pkg.Tag
TestRequest = pkg.TestRequest
)

var (
Expand Down Expand Up @@ -236,3 +238,7 @@ func NewUiText(vp Setter[string]) *UiText {
func NewUiTr(innerHTML HTMLGetter) *UiTr {
return pkg.NewUiTr(innerHTML)
}

func NewTestRequest(jw *Jaws, hr *http.Request) (tr *TestRequest) {
return pkg.NewTestRequest(jw, hr)
}
8 changes: 4 additions & 4 deletions jaws/clickhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ ClickHandler = (*testJawsClick)(nil)
func Test_clickHandlerWapper_JawsEvent(t *testing.T) {
th := newTestHelper(t)
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tjc := &testJawsClick{
Expand All @@ -37,16 +37,16 @@ func Test_clickHandlerWapper_JawsEvent(t *testing.T) {
t.Errorf("Request.Div() = %q, want %q", got, want)
}

rq.inCh <- wsMsg{Data: "text", Jid: 1, What: what.Input}
rq.InCh <- wsMsg{Data: "text", Jid: 1, What: what.Input}
select {
case <-th.C:
th.Timeout()
case s := <-rq.outCh:
case s := <-rq.OutCh:
t.Errorf("%q", s)
default:
}

rq.inCh <- wsMsg{Data: "adam", Jid: 1, What: what.Click}
rq.InCh <- wsMsg{Data: "adam", Jid: 1, What: what.Click}
select {
case <-th.C:
th.Timeout()
Expand Down
17 changes: 8 additions & 9 deletions jaws/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (tss *testUi) JawsUpdate(e *Element) {

func TestElement_helpers(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{}
e := rq.NewElement(tss)
is.Equal(e.Jaws, rq.jw.Jaws)
is.Equal(e.Jaws, rq.Jaws)
is.Equal(e.Request, rq.Request)
is.Equal(e.Session(), nil)
e.Set("foo", "bar") // no session, so no effect
Expand All @@ -80,7 +80,7 @@ func TestElement_helpers(t *testing.T) {

func TestElement_Tag(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{}
Expand All @@ -96,7 +96,7 @@ func TestElement_Tag(t *testing.T) {

func TestElement_Queued(t *testing.T) {
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{
Expand Down Expand Up @@ -181,13 +181,12 @@ func TestElement_Queued(t *testing.T) {
time.Sleep(time.Millisecond)
}
}
th.Equal(tss.updateCalled, int32(1))
th.Equal(tss.renderCalled, int32(2))
}

func TestElement_ReplacePanicsOnMissingId(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()
defer func() {
if x := recover(); x == nil {
Expand All @@ -202,7 +201,7 @@ func TestElement_ReplacePanicsOnMissingId(t *testing.T) {

func TestElement_maybeDirty(t *testing.T) {
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()
tss := &testUi{s: "foo"}
e := rq.NewElement(tss)
Expand Down Expand Up @@ -231,7 +230,7 @@ var _ ClickHandler = testClickHandler{}

func TestElement_ApplyGetter(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{s: "foo"}
Expand All @@ -250,7 +249,7 @@ func TestElement_ApplyGetter(t *testing.T) {

func TestElement_JawsInit(t *testing.T) {
is := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{s: "foo"}
Expand Down
2 changes: 1 addition & 1 deletion jaws/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestHandler_ServeHTTP(t *testing.T) {
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

dot := Tag("123")
Expand Down
12 changes: 6 additions & 6 deletions jaws/jawsevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ UI = (*testJawsEvent)(nil)
func Test_JawsEvent_ClickUnhandled(t *testing.T) {
th := newTestHelper(t)
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

msgCh := make(chan string, 1)
Expand All @@ -68,7 +68,7 @@ func Test_JawsEvent_ClickUnhandled(t *testing.T) {
id := rq.Register(zomgItem, je, "attr1", []string{"attr2"}, template.HTMLAttr("attr3"), []template.HTMLAttr{"attr4"})

je.clickerr = ErrEventUnhandled
rq.inCh <- wsMsg{Data: "name", Jid: id, What: what.Click}
rq.InCh <- wsMsg{Data: "name", Jid: id, What: what.Click}
select {
case <-th.C:
th.Timeout()
Expand All @@ -82,7 +82,7 @@ func Test_JawsEvent_ClickUnhandled(t *testing.T) {
func Test_JawsEvent_AllUnhandled(t *testing.T) {
th := newTestHelper(t)
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

msgCh := make(chan string, 1)
Expand All @@ -93,7 +93,7 @@ func Test_JawsEvent_AllUnhandled(t *testing.T) {

je.clickerr = ErrEventUnhandled
je.eventerr = ErrEventUnhandled
rq.inCh <- wsMsg{Data: "name", Jid: id, What: what.Click}
rq.InCh <- wsMsg{Data: "name", Jid: id, What: what.Click}
select {
case <-th.C:
th.Timeout()
Expand Down Expand Up @@ -126,7 +126,7 @@ func (t *testJawsEventHandler) JawsEvent(e *Element, wht what.What, val string)
func Test_JawsEvent_ExtraHandler(t *testing.T) {
th := newTestHelper(t)
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

msgCh := make(chan string, 1)
Expand All @@ -139,7 +139,7 @@ func Test_JawsEvent_ExtraHandler(t *testing.T) {
th.NoErr(je.JawsRender(elem, &sb, nil))
th.Equal(sb.String(), "<div id=\"Jid.1\">tjEH</div>")

rq.inCh <- wsMsg{Data: "name", Jid: 1, What: what.Click}
rq.InCh <- wsMsg{Data: "name", Jid: 1, What: what.Click}
select {
case <-th.C:
th.Timeout()
Expand Down
14 changes: 9 additions & 5 deletions jaws/jawsjaws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func TestJaws_BroadcastFullClosesChannel(t *testing.T) {

go func() {
select {
case <-t.Context().Done():
close(failCh)
case <-th.C:
close(failCh)
case <-subCh2:
Expand Down Expand Up @@ -521,18 +523,20 @@ func TestJaws_GenerateHeadHTML(t *testing.T) {

func TestJaws_TemplateLookuper(t *testing.T) {
th := newTestHelper(t)
rq := newTestRequest()
tj := newTestJaws()
defer tj.Close()
rq := NewTestRequest(tj.Jaws, nil)
defer rq.Close()
th.Equal(rq.Jaws.LookupTemplate("nosuchtemplate"), nil)
th.Equal(rq.Jaws.LookupTemplate("testtemplate"), rq.jw.testtmpl)
rq.Jaws.RemoveTemplateLookuper(rq.jw.testtmpl)
th.Equal(rq.Jaws.LookupTemplate("testtemplate"), tj.testtmpl)
rq.Jaws.RemoveTemplateLookuper(tj.testtmpl)
th.Equal(rq.Jaws.LookupTemplate("testtemplate"), nil)
}

func TestJaws_JsCall(t *testing.T) {
th := newTestHelper(t)
nextJid = 0
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

tss := &testUi{}
Expand All @@ -548,7 +552,7 @@ func TestJaws_JsCall(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case msg := <-rq.outCh:
case msg := <-rq.OutCh:
got := msg.Format()
th.Equal(got, "Call\tJid.1\tsomefn=1.3\n")
}
Expand Down
26 changes: 13 additions & 13 deletions jaws/jsvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (tl *testLocker) Unlock() {

func Test_JsVar_JawsRender(t *testing.T) {
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

nextJid = 0
rq.jw.AddTemplateLookuper(template.Must(template.New("jsvartemplate").Parse(`{{$.JsVar "` + varname + `" .Dot}}`)))
rq.Jaws.AddTemplateLookuper(template.Must(template.New("jsvartemplate").Parse(`{{$.JsVar "` + varname + `" .Dot}}`)))

var mu deadlock.RWMutex
var val valtype
Expand Down Expand Up @@ -88,7 +88,7 @@ func Test_JsVar_Update(t *testing.T) {
var val valtype
dot := NewJsVar(&mu, &val)

rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

elem := rq.NewElement(dot)
Expand All @@ -107,7 +107,7 @@ func Test_JsVar_Update(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case gotMsg := <-rq.outCh:
case gotMsg := <-rq.OutCh:
wantMsg := wsMsg{
Data: "={\"String\":\"x\",\"Number\":2}",
Jid: 1,
Expand Down Expand Up @@ -166,7 +166,7 @@ func Test_JsVar_Event(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case rq1.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "={\"String\":\"y\",\"Number\":3}"}:
case rq1.InCh <- wsMsg{Jid: 1, What: what.Set, Data: "={\"String\":\"y\",\"Number\":3}"}:
}

select {
Expand All @@ -180,7 +180,7 @@ func Test_JsVar_Event(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case msg := <-rq1.outCh:
case msg := <-rq1.OutCh:
s := msg.Format()
after, found := strings.CutPrefix(s, "Set\tJid.1\t=")
th.Equal(found, true)
Expand All @@ -197,7 +197,7 @@ func Test_JsVar_Event(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case msg := <-rq2.outCh:
case msg := <-rq2.OutCh:
s := msg.Format()
after, found := strings.CutPrefix(s, "Set\tJid.2\t=")
th.Equal(found, true)
Expand All @@ -214,13 +214,13 @@ func Test_JsVar_Event(t *testing.T) {
select {
case <-th.C:
th.Timeout()
case rq1.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "=1"}:
case rq1.InCh <- wsMsg{Jid: 1, What: what.Set, Data: "=1"}:
}

select {
case <-th.C:
th.Timeout()
case msg := <-rq1.outCh:
case msg := <-rq1.OutCh:
s := msg.Format()
if !strings.Contains(s, "jq: expected") {
th.Error(s)
Expand All @@ -230,7 +230,7 @@ func Test_JsVar_Event(t *testing.T) {

func Test_JsVar_PanicsOnWrongType(t *testing.T) {
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()
defer func() {
if x := recover(); x == nil {
Expand All @@ -255,7 +255,7 @@ var _ JsVarMaker = &testJsVarMaker{}
func Test_JsVar_JsVarMaker(t *testing.T) {
nextJid = 0
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()
err := rq.JsVar("foo", &testJsVarMaker{})
th.NoErr(err)
Expand Down Expand Up @@ -286,7 +286,7 @@ var _ SetPather = &testJsVarPathSetter{}
func Test_JsVar_PathSetter_SetPather(t *testing.T) {
nextJid = 0
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

var mu deadlock.Mutex
Expand All @@ -302,7 +302,7 @@ func Test_JsVar_PathSetter_SetPather(t *testing.T) {
func Test_JsVar_Unchanged(t *testing.T) {
nextJid = 0
th := newTestHelper(t)
rq := newTestRequest()
rq := newTestRequest(t)
defer rq.Close()

var mu deadlock.Mutex
Expand Down
2 changes: 1 addition & 1 deletion jaws/namedbool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNamedBool(t *testing.T) {
nba.Add("1", "one")
nb := nba.data[0]

rq := newTestRequest()
rq := newTestRequest(t)
e := rq.NewElement(NewUiCheckbox(nb))
defer rq.Close()

Expand Down
2 changes: 1 addition & 1 deletion jaws/namedboolarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Test_NamedBoolArray(t *testing.T) {
(nba.data)[1].Set(true)
is.Equal(nba.IsChecked("2"), true)

rq := newTestRequest()
rq := newTestRequest(t)
e := rq.NewElement(NewUiSelect(nba))
defer rq.Close()

Expand Down
7 changes: 7 additions & 0 deletions jaws/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ func (rq *Request) NewElement(ui UI) *Element {
return rq.newElementLocked(ui)
}

func (rq *Request) GetElementByJid(jid Jid) (e *Element) {
rq.mu.RLock()
defer rq.mu.RUnlock()
e = rq.getElementByJidLocked(jid)
return
}

func (rq *Request) getElementByJidLocked(jid Jid) (elem *Element) {
for _, e := range rq.elems {
if e.Jid() == jid {
Expand Down
Loading