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