From 24af9f40628d9fa6c90f893ea3e63e7d87dc969e Mon Sep 17 00:00:00 2001 From: Autumn Calhoun Date: Fri, 14 Apr 2023 07:35:01 -0400 Subject: [PATCH 1/2] Added another function to Table that will process async callback functions to resolve issue #570. Also had to bump the ecmaVersion to 8 in eslint to support async await Signed-off-by: Autumn Calhoun --- .eslintrc.json | 2 +- CONTRIBUTING.md | 4 ++-- js.json | 2 +- package-lock.json | 7 +++---- package.json | 2 +- src/table.js | 8 ++++++++ test/table.js | 31 +++++++++++++++++++++++++++++++ 7 files changed, 47 insertions(+), 9 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 918671b1..d17d9e4d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,7 +9,7 @@ }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 6 + "ecmaVersion": 8 }, "rules": { "curly": "error", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8112b3b0..4b54c654 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ ## Issues -If you find any issues or have any feature requests, please file them in the [issue tracker](https://github.com/getgauge-contrib/gauge-js/issues). +If you find any issues or have any feature requests, please file them in the [issue tracker](https://github.com/getgauge/gauge-js/issues). If you are filing issues, please provide the version of `gauge` core and `gauge-js` plugin that you have installed. You can find it by doing: @@ -13,7 +13,7 @@ $ gauge -v **Download** ```sh -$ git clone git://github.com/getgauge-contrib/gauge-js --recursive +$ git clone git://github.com/getgauge/gauge-js --recursive ``` **Setup**: diff --git a/js.json b/js.json index 95d014f9..eebd5ba5 100644 --- a/js.json +++ b/js.json @@ -51,7 +51,7 @@ "--init" ] }, - "version": "2.4.0", + "version": "2.4.1", "gaugeVersionSupport": { "minimum": "1.0.7", "maximum": "" diff --git a/package-lock.json b/package-lock.json index fbce8bd8..c71462c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gauge-js", - "version": "2.4.0", + "version": "2.4.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gauge-js", - "version": "2.4.0", + "version": "2.4.1", "license": "MIT", "dependencies": { "@grpc/grpc-js": "^1.5.5", @@ -2528,8 +2528,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "ajv": { "version": "6.12.6", diff --git a/package.json b/package.json index 59f62db1..9dfd5b20 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gauge-js", - "version": "2.4.0", + "version": "2.4.1", "description": "JavaScript runner for Gauge", "main": "index.js", "scripts": { diff --git a/src/table.js b/src/table.js index 3a25cb57..da71403e 100644 --- a/src/table.js +++ b/src/table.js @@ -8,6 +8,14 @@ var Table = function (protoTable) { callback(entry); } }; + + this.asyncEntries = async function (callback) { + for (let row of this.rows) { + let entry = {}; + row.cells.forEach((cell, index) => entry[this.headers.cells[index]] = cell); + await callback(entry); + } + }; }; module.exports = Table; diff --git a/test/table.js b/test/table.js index 096711a5..3e856910 100644 --- a/test/table.js +++ b/test/table.js @@ -1,5 +1,7 @@ var Table = require("../src/table"); var expect = require("chai").expect; +const util = require("util"); +const setTimeoutPromise = util.promisify(setTimeout); describe("ProtoTable parsing", function() { @@ -17,6 +19,14 @@ describe("ProtoTable parsing", function() { var table = new Table(protoTable); + let getRowData = function(entry) { + const rowData = setTimeoutPromise(500, entry).then((value) => { + return {cells: [value["Product"], value["Description"]]}; + }); + return rowData; + + }; + it("Should get headers", function () { expect(table.headers).to.deep.equal(protoTable.headers); }); @@ -41,6 +51,27 @@ describe("ProtoTable parsing", function() { "Description": "Agile project management" }); }); + + it("Should process asynchronous callback action using asyncEntries", async function () { + let data = []; + + await table.asyncEntries(async function (entry) { + data.push(await getRowData(entry)); + }); + + expect(data).to.deep.equal(protoTable.rows); + }).timeout(10000); + + it("Should not process asynchronous callback action using entries", async function () { + let data = []; + + await table.entries(async function (entry) { + data.push(await getRowData(entry)); + }); + + expect(data).to.be.empty; + }); + }); }); From cfc960bd4c2a8464af40ecb72b5be4bb38e65b63 Mon Sep 17 00:00:00 2001 From: Autumn Calhoun Date: Thu, 20 Apr 2023 07:53:00 -0400 Subject: [PATCH 2/2] #570 updated per code review comments Signed-off-by: Autumn Calhoun --- src/table.js | 18 ++++++++---------- test/table.js | 16 ++-------------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/table.js b/src/table.js index da71403e..278849f3 100644 --- a/src/table.js +++ b/src/table.js @@ -1,19 +1,17 @@ var Table = function (protoTable) { Object.assign(this, protoTable); - this.entries = function (callback) { - for (var row of this.rows) { - let entry = {}; - row.cells.forEach((cell, index) => entry[this.headers.cells[index]] = cell); - callback(entry); - } - }; + this.entries = async function (callback) { + const AsyncFunction = (async () => {}).constructor; - this.asyncEntries = async function (callback) { - for (let row of this.rows) { + for (const row of this.rows) { let entry = {}; row.cells.forEach((cell, index) => entry[this.headers.cells[index]] = cell); - await callback(entry); + if(callback instanceof AsyncFunction) { + await callback(entry); + } else { + callback(entry); + } } }; }; diff --git a/test/table.js b/test/table.js index 3e856910..211a637e 100644 --- a/test/table.js +++ b/test/table.js @@ -52,26 +52,14 @@ describe("ProtoTable parsing", function() { }); }); - it("Should process asynchronous callback action using asyncEntries", async function () { + it("Should process an asynchronous callback action", async function () { let data = []; - await table.asyncEntries(async function (entry) { - data.push(await getRowData(entry)); - }); + await table.entries(async (entry) => data.push(await getRowData(entry))); expect(data).to.deep.equal(protoTable.rows); }).timeout(10000); - it("Should not process asynchronous callback action using entries", async function () { - let data = []; - - await table.entries(async function (entry) { - data.push(await getRowData(entry)); - }); - - expect(data).to.be.empty; - }); - }); });