Coding Edgar
Coding Edgar

Reputation: 1465

Define Transformer for Table in Cucumberjs

Having the following Scenario

Scenario: Mutate multiple User Skills at the same time
    Given the follogin "User Skills":
      | ID | name     | level |
      | 1  | Pilot    | A     |
      | 1  | Sealer   | B     |
      | 2  | Pilot    | A     |
      | 2  | Sealer   | B     |
    When I send "Update Users Skills Mutation"
    Then the "Users" must contain the following "Skills" correspondingly:
      | ID | name     | level |
      | 1  | Pilot    | A     |
      | 1  | Sealer   | B     |
      | 2  | Pilot    | A     |
      | 2  | Sealer   | B     |

How can I define a parameter type that match the table?

defineParameterType({
   name: 'user skill table',
   regexp: ???,
   transformer: (table) =>{
      return world.createSkillFromTable(table)
   }
})

In The Ruby docs is a match for data tables like /^table:column1,column2$/, but I can find a way to match tables in CucumberJS, is there a way to do it?

Upvotes: 1

Views: 1347

Answers (1)

paulitto
paulitto

Reputation: 4673

Here is one way to handle tables in a cucubmer-js step definition.

Using your first Given step as an example, the first parameter would be "User Skills", the second paramter will be an object that holds your table, with its (case-sensitive) keys taken from the table header.

Given(/the following "(.*)":$/, function (type, table){        
    const rows = table.hashes();
    rows.forEach( (row) => {
        console.log("ID:", row.ID);
    });        
);

See here for other possible ways of retrieving data from the table parameter.

Upvotes: 1

Related Questions