0

I created the following checkGrid function which takes a count and then another two parameters. It's a solution which hardcodes in the selectOption calls and is not very flexible:

checkGrid = function(expectCount, typeParam, selectParam) {

   it('Check', function () {
      selectOption('examTypeSelect', typeParam).click();
      selectOption('examStatusSelect', selectParam).click();
    });

}
checkGrid(10, '*', '*');

Now I would like to make this more flexible so that it accepts arguments for any number of selectOption functions. So I was thinking of something that I could call like this. HEre what I would need is for the checkGrid function to call the selectOption three times:

checkGrid(10, [{'examTypeSelect','*'}
               {'examStatusSelect', '*'},
               {'abcSelect','29'}]);

How could I take an array object that's the second parameter for checkGrid and make it so that it calls any number of selectOption functions depending on how many elements there are in the array?

2

1 Answer 1

2

By using a loop over the array and .apply to call each function with a variable number of arguments.

checkGrid = function(expectCount, stuff) {
    it('Check', function () {
        for (var i = 0; i < stuff.length; ++i) {
            selectOption.apply(this, stuff[i]).click();
        }
    });
}

You would call this not with your proposed syntax (which is invalid), but like

checkGrid(10, [
                ['examTypeSelect','*'],
                ['examStatusSelect', '*'],
                ['abcSelect','29','and','perhaps','more','parameters']
              ]
          );

Finally, it's unclear what expectCount is supposed to do but I left it there because your original code presumably does something with it.

Sign up to request clarification or add additional context in comments.

6 Comments

@cookiemonster: I wouldn't change the value of this inside the body of selectOption.
thanks. I did not know about the need for an apply. As far as the syntax in my call goes. Is that syntax correct as a way to call the function. Yes you are correct about expectCount. That's needed but I didn't include that code so I could make it a simpler question. Thanks
Yeah, I was wrong, though the proper this would actually be the default since the original code doesn't pass the enclosed this.
@SamanthaJ: {'examTypeSelect','*'} etc is incorrect syntax, period.
@SamanthaJ: You should be aware that you don't really need .apply() here to do what you want, as long as you know each call will have 2 arguments. selectOption(stuff[0], stuff[1]);. Also, you don't need an Array of Arrays (or Objects). You can pass as many objects as you want to checkGrid, and then loop the arguments object starting at index 1. This way you're using checkGrid as a variadic function, meaning the number of arguments it receives is determined when invoked.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.