github.com/blend/go-sdk@v1.20220411.3/web/view_model_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestViewModelWrap(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	indexTemplate := `{{ define "index" }}{{ range $index, $obj := .ViewModel }}<div>{{ template "control" ( $.Wrap $obj ) }}</div>{{ end }}{{ end }}`
    21  	controlTemplate := `{{ define "control" }}{{ if .Ctx }}{{ .ViewModel }}{{ end }}{{ end }}`
    22  
    23  	app := MustNew()
    24  	app.Views.AddLiterals(indexTemplate, controlTemplate)
    25  
    26  	app.GET("/", func(r *Ctx) Result {
    27  		return r.Views.View("index", []string{"foo", "bar", "baz"})
    28  	})
    29  
    30  	contents, meta, err := MockGet(app, "/").Bytes()
    31  	assert.Nil(err)
    32  	assert.Equal(http.StatusOK, meta.StatusCode, string(contents))
    33  	assert.Equal("<div>foo</div><div>bar</div><div>baz</div>", string(contents))
    34  }