Skip to content

Commit

Permalink
Use mockPersister
Browse files Browse the repository at this point in the history
Use a mock persister when there's no need for either a local or remote
persister in a test. These tests do not need to validate that a
screenshot was saved.
  • Loading branch information
ankur22 committed Feb 13, 2024
1 parent ab27742 commit 1456485
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 9 additions & 4 deletions tests/element_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package tests

import (
"bytes"
"context"
_ "embed"
"fmt"
"image/png"
"io"
"testing"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/storage"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -344,6 +345,12 @@ func TestElementHandleQueryAll(t *testing.T) {
})
}

type mockPersister struct{}

func (m *mockPersister) Persist(ctx context.Context, path string, data io.Reader) (err error) {
return nil
}

func TestElementHandleScreenshot(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -375,11 +382,9 @@ func TestElementHandleScreenshot(t *testing.T) {
elem, err := p.Query("div")
require.NoError(t, err)

// TODO: Use a mock instead of a LocalFilePersister when there's no need to
// persist files.
buf, err := elem.Screenshot(
common.NewElementHandleScreenshotOptions(elem.Timeout()),
&storage.LocalFilePersister{},
&mockPersister{},
)
require.NoError(t, err)

Expand Down
5 changes: 1 addition & 4 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/grafana/xk6-browser/browser"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext/k6test"
"github.com/grafana/xk6-browser/storage"
)

type emulateMediaOpts struct {
Expand Down Expand Up @@ -476,9 +475,7 @@ func TestPageScreenshotFullpage(t *testing.T) {

opts := common.NewPageScreenshotOptions()
opts.FullPage = true
// TODO: Use a mock instead of a LocalFilePersister when there's no need to
// persist files.
buf, err := p.Screenshot(opts, &storage.LocalFilePersister{})
buf, err := p.Screenshot(opts, &mockPersister{})
require.NoError(t, err)

reader := bytes.NewReader(buf)
Expand Down

0 comments on commit 1456485

Please sign in to comment.