diff --git a/exercises/03_acceptance/cucumber/cucumberTest.sh b/exercises/03_acceptance/cucumber/cucumberTest.sh index cb5dab2..43588b6 100755 --- a/exercises/03_acceptance/cucumber/cucumberTest.sh +++ b/exercises/03_acceptance/cucumber/cucumberTest.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash +mkdir -p screenshots node index.js& node_modules/.bin/cucumber-js ./features -r ./steps & -sleep 2 +sleep 5 pkill node diff --git a/exercises/03_acceptance/cucumber/features/add.feature b/exercises/03_acceptance/cucumber/features/add.feature new file mode 100644 index 0000000..e187c3d --- /dev/null +++ b/exercises/03_acceptance/cucumber/features/add.feature @@ -0,0 +1,23 @@ +Feature: Adding Items + The user should be able to add items to the list. + + Scenario: add single item + Given The browser is open on the home page + When I enter "bread" in the "item" field + When I enter "42" in the "qty" field + When I click on the submit button + Then the heading should be "ToDo List" + Then the list should contain "1" rows + Then the list should contain a single entry for "bread" + Then the item should be "bread" + Then the "bread" quantity should be "42" + + Scenario: add multiple items + Given The browser is open on the home page + When I enter "butter" in the "item" field + When I enter "24" in the "qty" field + When I click on the submit button + Then the heading should be "ToDo List" + Then the list should contain "2" rows + Then the list should contain a single entry for "bread" + Then the item should be "bread" diff --git a/exercises/03_acceptance/cucumber/features/add_one.feature b/exercises/03_acceptance/cucumber/features/add_one.feature deleted file mode 100644 index 1b85d07..0000000 --- a/exercises/03_acceptance/cucumber/features/add_one.feature +++ /dev/null @@ -1,11 +0,0 @@ -Feature: Adding Items - The user should be able to add a single item to the list. - - Scenario: add item via webpage - Given The browser is open on the home page - When I enter "bread" in the "item" field - When I enter "42" in the "qty" field - When I click on the submit button - Then the heading should be "ToDo List" - Then the list should contain "1" row - Then the item should be "bread" diff --git a/exercises/03_acceptance/cucumber/steps/todo.steps.js b/exercises/03_acceptance/cucumber/steps/todo.steps.js index 5080004..682e1e3 100644 --- a/exercises/03_acceptance/cucumber/steps/todo.steps.js +++ b/exercises/03_acceptance/cucumber/steps/todo.steps.js @@ -41,6 +41,10 @@ When('I click on the submit button', async() => { await page.click('#submit') }) +Then('take a screenshot called {string}', async filename => { + await page.screenshot({ path: `screenshots/${filename}.png` }) +}) + Then('the heading should be {string}', async heading => { const text = await page.evaluate( () => { const dom = document.querySelector('h1') @@ -49,7 +53,8 @@ Then('the heading should be {string}', async heading => { assert.equal(heading, text) }) -Then('the list should contain {string} row', async rowCount => { +Then('the list should contain {string} rows', async rowCount => { + rowCount = Number(rowCount) const items = await page.evaluate( () => { const dom = document.querySelectorAll('table tr td:first-child') const arr = Array.from(dom) @@ -66,3 +71,22 @@ Then('the item should be {string}', async item => { }) assert.equal(item, items[0]) }) + +Then('the list should contain a single entry for {string}', async item => { + const items = await page.evaluate( () => { + const dom = document.querySelectorAll('table tr td:first-child') + const arr = Array.from(dom).map(td => td.innerText) + return arr + }) + const count = items.reduce( (acc, val) => (val === item ? acc += 1 : acc), 0) + assert.equal(count, 1) +}) + +Then('the {string} quantity should be {string}', async(item, qty) => { + const items = await page.evaluate( () => { + const dom = document.querySelectorAll('table tr') + // const arr = Array.from(dom) + return dom + }) + assert.equal(2, 2) +}) diff --git a/exercises/03_acceptance/todo/acceptance tests/ui.test.js b/exercises/03_acceptance/todo/acceptance tests/ui.test.js index 0f0a964..43aa288 100644 --- a/exercises/03_acceptance/todo/acceptance tests/ui.test.js +++ b/exercises/03_acceptance/todo/acceptance tests/ui.test.js @@ -68,14 +68,20 @@ describe('todo list', () => { const arr = Array.from(dom) return arr.map(td => td.innerText) }) + const numRows = items.reduce( (acc, val) => (val === 'bread' ? acc += 1 : acc), 0) - // this is a more concise way to achieve the same result... + console.log(`numRows: ${numRows}`) + + // returns the number of rows containing the given string const items2 = await page.evaluate(() => Array .from(document.querySelectorAll('table tr td:first-child')) - .map(td => td.innerHTML) ) + .map(td => td.innerHTML) + .reduce( (acc, val) => (val === 'bread' ? acc += 1 : acc), 0) + ) expect(items.length).toBe(1) expect(items[0]).toBe('bread') + expect(numRows).toBe(1) // grab a screenshot const image = await page.screenshot() diff --git a/exercises/03_acceptance/todo/package.json b/exercises/03_acceptance/todo/package.json index 7f99bd8..3b1f5df 100644 --- a/exercises/03_acceptance/todo/package.json +++ b/exercises/03_acceptance/todo/package.json @@ -4,11 +4,13 @@ "description": "", "main": "index.js", "scripts": { + "start-server": "node index.js", "linter": "node_modules/.bin/eslint .", - "test": "node_modules/.bin/jest --coverage --runInBand --detectOpenHandles", + "test": "./node_modules/.bin/jest --runInBand --detectOpenHandles 'acceptance tests'/*", "watch": "node_modules/.bin/jest --coverage --watchAll", "acceptance": "./test.sh", - "profiler": "./node_modules/.bin/0x -o index.js" + "profiler": "./node_modules/.bin/0x -o index.js", + "ci": "./node_modules/.bin/start-server-and-test start-server http://localhost:8080 test" }, "author": "", "license": "ISC", @@ -30,6 +32,7 @@ "jest-puppeteer": "^4.3.0", "puppeteer": "^1.20.0", "puppeteer-har": "^1.1.1", + "start-server-and-test": "^1.10.6", "supertest": "^4.0.2" }, "jest": {