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

     1  package buffalo
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/gobuffalo/buffalo/render"
     8  	"github.com/gobuffalo/httptest"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_FlashAdd(t *testing.T) {
    13  	r := require.New(t)
    14  	f := newFlash(&Session{})
    15  
    16  	r.Equal(f.data, map[string][]string{})
    17  
    18  	f.Add("error", "something")
    19  	r.Equal(f.data, map[string][]string{
    20  		"error": {"something"},
    21  	})
    22  
    23  	f.Add("error", "other")
    24  	r.Equal(f.data, map[string][]string{
    25  		"error": {"something", "other"},
    26  	})
    27  }
    28  
    29  func Test_FlashRender(t *testing.T) {
    30  	r := require.New(t)
    31  	a := New(Options{})
    32  	rr := render.New(render.Options{})
    33  
    34  	a.GET("/", func(c Context) error {
    35  		c.Flash().Add("errors", "Error AJ set")
    36  		c.Flash().Add("errors", "Error DAL set")
    37  
    38  		return c.Render(http.StatusCreated, rr.String(errorsTPL))
    39  	})
    40  
    41  	w := httptest.New(a)
    42  	res := w.HTML("/").Get()
    43  
    44  	r.Contains(res.Body.String(), "Error AJ set")
    45  	r.Contains(res.Body.String(), "Error DAL set")
    46  }
    47  
    48  func Test_FlashRenderEmpty(t *testing.T) {
    49  	r := require.New(t)
    50  	a := New(Options{})
    51  	rr := render.New(render.Options{})
    52  
    53  	a.GET("/", func(c Context) error {
    54  		return c.Render(http.StatusCreated, rr.String(errorsTPL))
    55  	})
    56  
    57  	w := httptest.New(a)
    58  
    59  	res := w.HTML("/").Get()
    60  	r.NotContains(res.Body.String(), "Flash:")
    61  }
    62  
    63  const errorsTPL = `
    64  <%= for (k, v) in flash["errors"] { %>
    65  	Flash:
    66  		<%= k %>:<%= v %>
    67  <% } %>
    68  `
    69  
    70  func Test_FlashRenderEntireFlash(t *testing.T) {
    71  	r := require.New(t)
    72  	a := New(Options{})
    73  	rr := render.New(render.Options{})
    74  
    75  	a.GET("/", func(c Context) error {
    76  		c.Flash().Add("something", "something to say!")
    77  		return c.Render(http.StatusCreated, rr.String(keyTPL))
    78  	})
    79  
    80  	w := httptest.New(a)
    81  	res := w.HTML("/").Get()
    82  	r.Contains(res.Body.String(), "something to say!")
    83  }
    84  
    85  const keyTPL = `<%= for (k, v) in flash { %>
    86  	Flash:
    87  		<%= k %>:<%= v %>
    88  <% } %>
    89  `
    90  
    91  func Test_FlashRenderCustomKey(t *testing.T) {
    92  	r := require.New(t)
    93  	a := New(Options{})
    94  	rr := render.New(render.Options{})
    95  
    96  	a.GET("/", func(c Context) error {
    97  		c.Flash().Add("something", "something to say!")
    98  		return c.Render(http.StatusCreated, rr.String(keyTPL))
    99  	})
   100  
   101  	w := httptest.New(a)
   102  	res := w.HTML("/").Get()
   103  	r.Contains(res.Body.String(), "something to say!")
   104  }
   105  
   106  func Test_FlashRenderCustomKeyNotDefined(t *testing.T) {
   107  	r := require.New(t)
   108  	a := New(Options{})
   109  	rr := render.New(render.Options{})
   110  
   111  	a.GET("/", func(c Context) error {
   112  		return c.Render(http.StatusCreated, rr.String(customKeyTPL))
   113  	})
   114  
   115  	w := httptest.New(a)
   116  	res := w.HTML("/").Get()
   117  	r.NotContains(res.Body.String(), "something to say!")
   118  }
   119  
   120  const customKeyTPL = `
   121  	{{#each flash.other as |k value|}}
   122  		{{value}}
   123  	{{/each}}`