func New() *StdWeb { rng := rand.New(rand.NewSource(time.Now().UnixNano())) s := &StdWeb{ listeners: make(map[chan *Update]bool), recentNodeUpdates: make(map[string]*timedNodeUpdate), rng: rng, runID: fmt.Sprintf("%v", rng.Int31()), } s.tree = core.NewTree("sw", s) return s }
func TestBasicNode(t *testing.T) { pub := testutils.NewPublisher() s := core.NewTree("test", pub).Slot() s.Set("foobar") n := s.Node() if exp, got := "foobar", testutils.Render(n); exp != got { t.Errorf("Expected %q, got %q", exp, got) } if pub.Last("test") != "foobar" { t.Errorf("Expected update with content 'foobar'; published:\n%s", pub.Dump()) } }
func TestCustomRenderer(t *testing.T) { pub := testutils.NewPublisher() s := core.NewTree("test", pub).Slot() core.NewConst(s, "other") n := s.Node() if exp, got := "other", testutils.Render(n); exp != got { t.Errorf("Expected %q, got %q", exp, got) } if pub.Last("test") != "other" { t.Errorf("Expected update with content 'other'; published:\n%s", pub.Dump()) } }
func TestPageRender(t *testing.T) { s := core.NewTree("test", testutils.NewPublisher()).Slot() page := NewPage(s) page.SetTitle("footitle") page.Body().Set("foobody") page.CSS("bar").Set("foocss") content := testutils.Render(s.Node()) for _, s := range []string{"footitle", "foobody", "foocss"} { if !strings.Contains(content, s) { t.Errorf("Rendered page should have contained %q; generated content:\n%s", s, content) } } }
func TestSimpleTemplate(t *testing.T) { pub := testutils.NewPublisher() s := core.NewTree("test", pub).Slot() tpl := MustParseTemplate("foo{{.Data}}bar").New(s) if exp, got := "foobar", testutils.Render(s.Node()); exp != got { t.Errorf("Expected %q, got %q", exp, got) } tpl.SetData(314) if pub.Last("test") != "foo314bar" { t.Errorf("Expected update with content 'foo314bar'; published:\n%s", pub.Dump()) } if exp, got := "foo314bar", testutils.Render(s.Node()); exp != got { t.Errorf("Expected %q, got %q", exp, got) } }
func TestNestedTemplate(t *testing.T) { pub := testutils.NewPublisher() s := core.NewTree("test", pub).Slot() tpl := MustParseTemplate(`{{slot "foo" "span"}}`).New(s) if exp, got := `<span id='test-foo'></span>`, testutils.Render(s.Node()); exp != got { t.Errorf("Expected %q, got %q", exp, got) } tpl.Slot("foo").Set("bar") if pub.Last("test-foo") != "bar" { t.Errorf("Expected update with content 'bar'; published:\n%s", pub.Dump()) } if exp, got := `<span id='test-foo'>bar</span>`, testutils.Render(s.Node()); exp != got { t.Errorf("Expected %q, got %q", exp, got) } }
func TestPageUpdates(t *testing.T) { pub := testutils.NewPublisher() s := core.NewTree("test", pub).Slot() page := NewPage(s) page.SetTitle("foo") if pub.Last("test-title") != "foo" { t.Errorf("Title should have been set to 'foo'; published:\n%s", pub.Dump()) } page.Body().Set("bar") if pub.Last("test-body") != "bar" { t.Errorf("Body should have been set to 'bar'; published:\n%s", pub.Dump()) } page.CSS("test0").Set("plop") if pub.Last("test-css_test0") != "plop" { t.Errorf("CSS should have been set to 'plop'; published:\n%s", pub.Dump()) } }