I am writing test cases in JavaScript and jQuery, the framework is Jasmine and Karma.

describe('When a user selects a multiple-response Hotspot response', function () {

    it('Then allow highlighting multiple responses', function (done) {
        utils.clickHotspotElementAtIndex(view, 1, function() {});
        utils.clickHotspotElementAtIndex(view, 0, function() {});
        utils.clickHotspotElementAtIndex(view, 2, function() {});
        setTimeout(function() {
            expect(utils.verifyHotSpotHasBeenHiglightedAtIndex(view, 0)).toBe('true');
            expect(utils.verifyHotSpotHasBeenHiglightedAtIndex(view, 1)).toBe('true');
            expect(utils.verifyHotSpotHasBeenHiglightedAtIndex(view, 2)).toBe('true');
        }), 2000});

What it does is:

  1. click on element 1, sync action
  2. click on element 0, sync action
  3. click on element 2. sync action
  4. When the main thread is free, wait for 2 seconds and then
  5. expect element 0 has been highlighted
  6. expect element 1 has been highlighted
  7. expect element 2 has been highlighted

Is there a more elegant way to do this?

  • \$\begingroup\$ @200_success, thanks, this question has nothing to do with jQuery directly. Most of the methods use jQuery in their helper files. \$\endgroup\$
    – Yu Zhang
    Oct 3, 2018 at 5:19

1 Answer 1


The anonymous functions and hardcoded values can be abstracted. For example:

function click(value){
    utils.clickHotspotElementAtIndex(view, value, Function);

function tests(value){
    expect(utils.verifyHotSpotHasBeenHiglightedAtIndex(view, value)).toBe('true');

function test () {
    [0, 1, 2].map(tests);

Then the asynchronous code can be modernized via async/await:

async function sequencer() {
    const clicks = [1, 0, 2].map(click);
    await Promise.all(clicks);

function run() {
    it('Then allow highlighting multiple responses', test);

And the call can simply be a one-liner:

describe('When a user selects a multiple-response Hotspot response', run);



Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.