github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/js_test.go (about)

     1  package render
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  const jsLayout = "layout.js"
    12  const jsAltLayout = "alt_layout.plush.js"
    13  const jsTemplate = "my-template.js"
    14  
    15  func Test_JavaScript_WithoutLayout(t *testing.T) {
    16  	r := require.New(t)
    17  
    18  	e := NewEngine()
    19  
    20  	box := e.TemplatesBox
    21  	r.NoError(box.AddString(jsTemplate, "alert(<%= name %>)"))
    22  
    23  	h := e.JavaScript(jsTemplate)
    24  	r.Equal("application/javascript", h.ContentType())
    25  	bb := &bytes.Buffer{}
    26  
    27  	r.NoError(h.Render(bb, Data{"name": "Mark"}))
    28  	r.Equal("alert(Mark)", strings.TrimSpace(bb.String()))
    29  }
    30  
    31  func Test_JavaScript_WithLayout(t *testing.T) {
    32  	r := require.New(t)
    33  
    34  	e := NewEngine()
    35  	e.JavaScriptLayout = jsLayout
    36  
    37  	box := e.TemplatesBox
    38  	r.NoError(box.AddString(jsTemplate, "alert(<%= name %>)"))
    39  	r.NoError(box.AddString(jsLayout, "$(<%= yield %>)"))
    40  
    41  	h := e.JavaScript(jsTemplate)
    42  	r.Equal("application/javascript", h.ContentType())
    43  	bb := &bytes.Buffer{}
    44  
    45  	r.NoError(h.Render(bb, Data{"name": "Mark"}))
    46  	r.Equal("$(alert(Mark))", strings.TrimSpace(bb.String()))
    47  }
    48  
    49  func Test_JavaScript_WithLayout_Override(t *testing.T) {
    50  	r := require.New(t)
    51  
    52  	e := NewEngine()
    53  	e.JavaScriptLayout = jsLayout
    54  
    55  	box := e.TemplatesBox
    56  	r.NoError(box.AddString(jsTemplate, "alert(<%= name %>)"))
    57  	r.NoError(box.AddString(jsLayout, "$(<%= yield %>)"))
    58  	r.NoError(box.AddString(jsAltLayout, "_(<%= yield %>)"))
    59  
    60  	h := e.JavaScript(jsTemplate, jsAltLayout)
    61  	r.Equal("application/javascript", h.ContentType())
    62  	bb := &bytes.Buffer{}
    63  
    64  	r.NoError(h.Render(bb, Data{"name": "Mark"}))
    65  	r.Equal("_(alert(Mark))", strings.TrimSpace(bb.String()))
    66  }
    67  
    68  func Test_JavaScript_Partial_Without_Extension(t *testing.T) {
    69  	const tmpl = "let a = 1;\n<%= partial(\"part\") %>"
    70  	const part = "alert('Hi <%= name %>!');"
    71  
    72  	r := require.New(t)
    73  
    74  	e := NewEngine()
    75  
    76  	box := e.TemplatesBox
    77  	r.NoError(box.AddString(jsTemplate, tmpl))
    78  	r.NoError(box.AddString("_part.js", part))
    79  
    80  	h := e.JavaScript(jsTemplate)
    81  	r.Equal("application/javascript", h.ContentType())
    82  	bb := &bytes.Buffer{}
    83  
    84  	r.NoError(h.Render(bb, Data{"name": "Yonghwan"}))
    85  	r.Equal("let a = 1;\nalert('Hi Yonghwan!');", bb.String())
    86  }
    87  
    88  func Test_JavaScript_Partial(t *testing.T) {
    89  	const tmpl = "let a = 1;\n<%= partial(\"part.js\") %>"
    90  	const part = "alert('Hi <%= name %>!');"
    91  
    92  	r := require.New(t)
    93  
    94  	e := NewEngine()
    95  
    96  	box := e.TemplatesBox
    97  	r.NoError(box.AddString(jsTemplate, tmpl))
    98  	r.NoError(box.AddString("_part.js", part))
    99  
   100  	h := e.JavaScript(jsTemplate)
   101  	r.Equal("application/javascript", h.ContentType())
   102  	bb := &bytes.Buffer{}
   103  
   104  	r.NoError(h.Render(bb, Data{"name": "Yonghwan"}))
   105  	r.Equal("let a = 1;\nalert('Hi Yonghwan!');", bb.String())
   106  }
   107  
   108  func Test_JavaScript_HTML_Partial(t *testing.T) {
   109  	const tmpl = "let a = \"<%= partial(\"part.html\") %>\""
   110  	const part = `<div id="foo">
   111  	<p>hi</p>
   112  </div>`
   113  
   114  	r := require.New(t)
   115  
   116  	e := NewEngine()
   117  
   118  	box := e.TemplatesBox
   119  	r.NoError(box.AddString(jsTemplate, tmpl))
   120  	r.NoError(box.AddString("_part.html", part))
   121  
   122  	h := e.JavaScript(jsTemplate)
   123  	r.Equal("application/javascript", h.ContentType())
   124  	bb := &bytes.Buffer{}
   125  
   126  	r.NoError(h.Render(bb, Data{}))
   127  	r.Contains(bb.String(), `id`)
   128  	r.Contains(bb.String(), `foo`)
   129  
   130  	// To check it has escaped the partial
   131  	r.NotContains(bb.String(), `<div id="foo">`)
   132  }