Jasmine test involving a sequence of actions 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');
            done();
        }), 2000});
    });


What it does is:


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


Is there a more elegant way to do this?
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() {
    beforeEach(sequencer);
    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);


References


Jasmine Tutorial: Asynchronous work
JavaScript loops - how to handle async/await