Michael
Michael

Reputation: 1231

How to expand the Start task with new scenarios

I've just learned about serenity-js and am giving it a go. I'm following the tutorial and noticed the follow example:

james.attemptsTo(
    Start.withAnEmptyTodoList(),
    AddATodoItem.called('Buy some milk')
)

The task for Start:

export class Start implements Task {

    static withATodoListContaining(items: string[]) {       // static method to improve the readability
        return new Start(items);
    }

    performAs(actor: PerformsTasks): PromiseLike<void> {    // required by the Task interface
        return actor.attemptsTo(                            // delegates the work to lower-level tasks
            // todo: add each item to the Todo List
        );
    }

    constructor(private items: string[]) {                  // constructor assigning the list of items
    }                                                       // to a private field
}

I really like this syntax and would like to continue this setup with more starting scenario's. What would be the proper approach to accomplish this?

Upvotes: 0

Views: 54

Answers (1)

Michael
Michael

Reputation: 1231

For anyone having the same question this is how I resolved it (found a similar setup going through the serenity-js repo):

// Start.ts
export class Start {
    public static withATodoListContaining = (items: string[]): StartWithATodoListContaining => new StartWithATodoListContaining(items);
}

// StartWithATodoListContaining.ts
export class StartWithATodoListContaining implements Task {

    static withATodoListContaining(items: string[]) {       
        return new StartWithATodoListContaining(items);
    }

    performAs(actor: PerformsTasks): PromiseLike<void> {    
        return actor.attemptsTo(                            
            // todo: add each item to the Todo List
        );
    }

    constructor(private items: string[]) {                  
    }                                                       
}

Upvotes: 0

Related Questions