Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
384-setInputFiles
  • Loading branch information
bandorko committed Nov 11, 2023
1 parent d9465da commit e9ba8ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ func (h *ElementHandle) SelectText(opts goja.Value) {
applySlowMo(h.ctx)
}

// SetInputFiles sets the Files given in the opts into the <input type="file"> element.
func (h *ElementHandle) SetInputFiles(opts goja.Value) {
actionOpts := NewElementHandleSetInputFilesOptions(h.defaultTimeout())
if err := actionOpts.Parse(h.ctx, opts); err != nil {
Expand Down Expand Up @@ -1267,11 +1268,9 @@ func (h *ElementHandle) setInputFiles(apiCtx context.Context, payload []File) er
if err != nil {
return err
}
switch result := result.(type) {
case string: // Either we're done or an error happened (returned as "error:..." from JS)
if result != "done" {
return errorFromDOMError(result)
}
if res, ok := result.(string); ok && res != "done" {
// Either we're done or an error happened (returned as "error:..." from JS)
return errorFromDOMError(res)
}

return nil
Expand Down
10 changes: 7 additions & 3 deletions common/element_handle_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -74,12 +75,14 @@ type ElementHandleHoverOptions struct {
Modifiers []string `json:"modifiers"`
}

// File is the descriptor of a single file.
type File struct {
Name string `json:"name"`
Buffer string `json:"buffer"`
Mime string `json:"mime"`
}

// ElementHandleSetInputFilesOption are options for ElementHandle.SetInputFiles.
type ElementHandleSetInputFilesOption struct {
ElementHandleBaseOptions
Payload []File `json:"payload"`
Expand Down Expand Up @@ -188,13 +191,15 @@ func (o *ElementHandleCheckOptions) Parse(ctx context.Context, opts goja.Value)
return o.ElementHandleBasePointerOptions.Parse(ctx, opts)
}

// NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption.
func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOption {
return &ElementHandleSetInputFilesOption{
ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout),
Payload: []File{},
}
}

// Parse parses the ElementHandleSetInputFilesOption from the given opts.
func (o *ElementHandleSetInputFilesOption) Parse(ctx context.Context, opts goja.Value) error {
rt := k6ext.Runtime(ctx)
if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil {
Expand All @@ -203,11 +208,10 @@ func (o *ElementHandleSetInputFilesOption) Parse(ctx context.Context, opts goja.
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "payload":
if k == "payload" {
var p []File
if err := rt.ExportTo(opts.Get(k), &p); err != nil {
return err
return fmt.Errorf("unable to parse SetInputFileOptions; reason: %w", err)
}
o.Payload = p
}
Expand Down

0 comments on commit e9ba8ba

Please sign in to comment.