-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_test.go
62 lines (48 loc) · 1.55 KB
/
render_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
package atsemail_test
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/bufbuild/protovalidate-go"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
. "github.com/onsi/gomega"
"github.com/crewlinker/atsemail"
)
func AssertEmailRender[T atsemail.EmailData](
tb testing.TB, templateName string, caseIdx int, data T, expf func(g Gomega, txtbuf, htbuf *bytes.Buffer),
) {
tb.Helper()
g, ctx := NewWithT(tb), context.Background()
val, err := protovalidate.New()
g.Expect(err).ToNot(HaveOccurred())
render, err := atsemail.New[T](templateName)
g.Expect(err).ToNot(HaveOccurred())
var txtbuf, htbuf bytes.Buffer
g.Expect(render.Render(val, &txtbuf, &htbuf, data)).To(Succeed())
expf(g, &htbuf, &txtbuf)
SaveScreenshot(ctx, g, fmt.Sprintf("%s_%d", templateName, caseIdx), &htbuf)
}
func SaveScreenshot(ctx context.Context, g Gomega, name string, htbuf *bytes.Buffer) {
ctx, cancel := chromedp.NewContext(ctx)
defer cancel()
var buf []byte
g.Expect(chromedp.Run(ctx,
chromedp.Navigate("about:blank"),
chromedp.ActionFunc(func(ctx context.Context) error {
ft, err := page.GetFrameTree().Do(ctx)
if err != nil {
return fmt.Errorf("failed to get frame tree: %w", err)
}
if err := page.SetDocumentContent(ft.Frame.ID, htbuf.String()).Do(ctx); err != nil {
return fmt.Errorf("failed to set docment content: %w", err)
}
return nil
}),
chromedp.FullScreenshot(&buf, 100),
)).To(Succeed())
g.Expect(os.WriteFile(filepath.Join("screenshots", name+".png"), buf, 0o644)).To(Succeed())
}