Skip to content

Added DataTable and DataTableArgument types #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/data/dataTableArgument.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DataTableArgument {

/** Transposed the data */
transpose() {
this.rawData = this.rawData[0].map((x, i) => this.rawData.map((y) => y[i]));
this.rawData = this.rawData[0].map((_, i) => this.rawData.map((y) => y[i]));
}
}

Expand Down
8 changes: 5 additions & 3 deletions lib/data/table.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const DataTableArgument = require("./dataTableArgument");

/**
* Datatable class to provide data driven testing
*/
class DataTable {
/** @param {Array<*>} array */
/** @param {Array<DataTableArgument>} array */
constructor(array) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is correct
As you can actually pass any array into DataTable
DataTableArgument works only with Gherkin tables while DataTable works with classical tables as well

this.array = array;
this.rows = new Array(0);
}

/** @param {Array<*>} array */
/** @param {Array<DataTableArgument>} array */
add(array) {
if (array.length !== this.array.length) throw new Error(`There is too many elements in given data array. Please provide data in this format: ${this.array}`);
const tempObj = {};
Expand All @@ -21,7 +23,7 @@ class DataTable {
this.rows.push({ skip: false, data: tempObj });
}

/** @param {Array<*>} array */
/** @param {Array<DataTableArgument>} array */
xadd(array) {
if (array.length !== this.array.length) throw new Error(`There is too many elements in given data array. Please provide data in this format: ${this.array}`);
const tempObj = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const I = actor();

Given('I have products in my cart', (table) => { // eslint-disable-line
Given('I have products in my cart', (table) => {
for (const id in table.rows) {
if (id < 1) {
continue;
Expand Down
22 changes: 22 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ declare namespace CodeceptJS {
useForSnippets?: boolean
preferForRegexpMatch?: boolean
}

interface DataTableArgument {
rawData: string[][];
raw(): string[][];
rows(): string[][];
hashes(): { [columnHeader: string]: string }[];
rowsHash(): { [columnHeader: string]: string };
transpose(): string[][];
}

interface DataTable {
array: DataTableArgument[];
rows: {
[key: string]: {
skip: boolean;
data: DataTableArgument;
}
};
add: (array: DataTableArgument[]) => void;
xadd: (array: DataTableArgument[]) => void;
filter: (func: (item: DataTableArgument) => boolean) => DataTableArgument[];
}
}

// Globals
Expand Down