Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Refactor integration keyboard tests to unit tests
Browse files Browse the repository at this point in the history
Some of the integration tests can easily be converted to unit tests
which makes running them quicker, as well as making the tests simpler.
It also helps group similar tests.

Resolves: #326 (comment)
  • Loading branch information
ankur22 committed Aug 2, 2022
1 parent 8ad3b44 commit 71ba9f5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
77 changes: 77 additions & 0 deletions common/keyboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package common

import (
"testing"

"github.com/grafana/xk6-browser/k6ext/k6test"
"github.com/stretchr/testify/assert"
)

func TestSplit(t *testing.T) {
type args struct {
keys string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "empty slice on empty string",
args: args{
keys: "",
},
want: []string{""},
},
{
name: "empty slice on string without separator",
args: args{
keys: "HelloWorld!",
},
want: []string{"HelloWorld!"},
},
{
name: "string split with separator",
args: args{
keys: "Hello+World+!",
},
want: []string{"Hello", "World", "!"},
},
{
name: "do not split on single +",
args: args{
keys: "+",
},
want: []string{"+"},
},
{
name: "split ++ to + and ''",
args: args{
keys: "++",
},
want: []string{"+", ""},
},
{
name: "split +++ to + and +",
args: args{
keys: "+++",
},
want: []string{"+", "+"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := split(tt.args.keys)

assert.Equal(t, tt.want, got)
})
}
}

func TestKeyboardPress(t *testing.T) {
t.Run("panics when '' empty key passed in", func(t *testing.T) {
vu := k6test.NewVU(t)
k := NewKeyboard(vu.Context(), nil)
assert.Panics(t, func() { k.Press("", nil) })
})
}
32 changes: 0 additions & 32 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,6 @@ func TestKeyboardPress(t *testing.T) {
assert.Equal(t, "L+m+KeyN", el.InputValue(nil))
})

t.Run("just +", func(t *testing.T) {
p := tb.NewPage(nil)
cp, ok := p.(*common.Page)
require.True(t, ok)
kb := cp.Keyboard

p.SetContent(`<textarea>`, nil)
el := p.Query("textarea")
p.Focus("textarea", nil)

kb.Press("+", nil)
kb.Press("+++", nil)
kb.Press("+++++", nil)
kb.Down("+")
kb.Up("+")
assert.Equal(t, "+++++++", el.InputValue(nil))
})

t.Run("not enough +", func(t *testing.T) {
p := tb.NewPage(nil)
cp, ok := p.(*common.Page)
require.True(t, ok)
kb := cp.Keyboard

p.SetContent(`<textarea>`, nil)
p.Focus("textarea", nil)

assert.Panics(t, func() { kb.Press("++", nil) })

assert.Panics(t, func() { kb.Press("Shift+++", nil) })
})

t.Run("capitalization", func(t *testing.T) {
p := tb.NewPage(nil)
cp, ok := p.(*common.Page)
Expand Down

0 comments on commit 71ba9f5

Please sign in to comment.