-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_container_error_test.go
97 lines (81 loc) · 2.17 KB
/
render_container_error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package veun_test
import (
"context"
"errors"
"fmt"
"html/template"
"testing"
"github.com/alecthomas/assert/v2"
. "github.com/stanistan/veun"
)
type FailingView struct {
Err error
}
func (v FailingView) View(_ context.Context) (*View, error) {
return nil, fmt.Errorf("FailingView.Renderable(): %w", v.Err)
}
type FallibleView struct {
CapturesErr error
Child AsView
}
func (v FallibleView) View(_ context.Context) (*View, error) {
return V(v.Child).WithErrorHandler(v), nil
}
func (v FallibleView) ViewForError(ctx context.Context, err error) (AsView, error) {
if v.CapturesErr == nil {
return nil, err
}
if errors.Is(err, v.CapturesErr) {
return ChildView1{}, nil
}
return nil, nil
}
func TestRenderContainerWithFailingView(t *testing.T) {
_, err := Render(context.Background(), ContainerView2{
Heading: ChildView1{},
Body: FailingView{
Err: fmt.Errorf("construction: %w", errSomethingFailed),
},
})
assert.IsError(t, err, errSomethingFailed)
}
func TestRenderContainerWithCapturedError(t *testing.T) {
t.Run("errors_bubble_out", func(t *testing.T) {
_, err := Render(context.Background(), ContainerView2{
Heading: ChildView1{},
Body: FallibleView{
Child: FailingView{Err: errSomethingFailed},
},
})
assert.IsError(t, err, errSomethingFailed)
})
t.Run("errors_can_push_replacement_views", func(t *testing.T) {
html, err := Render(context.Background(), ContainerView2{
Heading: ChildView1{},
Body: FallibleView{
Child: FailingView{Err: errSomethingFailed},
CapturesErr: errSomethingFailed,
},
})
assert.NoError(t, err)
assert.Equal(t, template.HTML(`<div>
<div class="heading">HEADING</div>
<div class="body">HEADING</div>
</div>`), html)
})
t.Run("errors_can_return_nil_views", func(t *testing.T) {
html, err := Render(context.Background(), ContainerView2{
Heading: ChildView1{},
Body: FallibleView{
Child: FailingView{Err: errors.New("hi")},
CapturesErr: errSomethingFailed,
},
})
assert.NoError(t, err)
assert.Equal(t, template.HTML(`<div>
<div class="heading">HEADING</div>
<div class="body"></div>
</div>`), html)
})
}
var errSomethingFailed = errors.New("an error")