Chaining compose statements

This tutorial chains compose statements to test that the sum of the times listed for each unit in a Trailhead trail matches the total time listed for the trail.

Click the DOM Tree Viewer. There are three web components that have corresponding JSON page objects.

  • example-trailhead—This root component describes the trail and displays the Trailhead modules or projects.

  • example-trail-module—This component displays a Trailhead module or project.

  • example-trail-unit—This component displays a unit in a Trailhead module or project.

The test imports the root trailhead page object and calls the getUnitDurations compose method to get the unit durations across all modules in the trail.

The trailhead page object chains the statements in the getUnitDurations compose method by setting the chain property to true:

    "methods": [
        {
            "name": "getUnitDurations",
            "compose": [
                {
                    "element": "trailModules"
                },
                {
                    "returnType": "tutorial/trailUnit",
                    "returnAll": true,
                    "chain": true,
                    "element": "trailUnits"
                },
                {
                    "returnType": "string",
                    "chain": true,
                    "apply": "getUnitDurationText"
                }
            ]
        }
    ]

A chain of compose statements builds a chain of calls where any chained statement is applied to the result of the previous statement.

In the example, the first statement accesses an element (trailModules) from the current page object (trailhead). The second statement accesses a public element (trailUnits) declared in the trailModule page object. The third statement calls a getUnitDurationText compose method from the trailUnit page object.

If the public element returns a list, the statement in the chain should set returnAll to true.

The test extracts the unit durations and asserts that the sum of unit durations equals the trail duration.

For more details on chain compose statements, see the grammar.