From ea330548b1ec0341a3c57e74526cfc745a34f7f2 Mon Sep 17 00:00:00 2001 From: Rowan Manning Date: Fri, 22 Nov 2013 11:05:27 +0000 Subject: [PATCH] Write tests for task pages (and related actions) --- test/functional/helper/webservice.js | 14 ++++ test/functional/route/index.js | 96 ++++++++++++------------- test/functional/route/new.js | 104 +++++++++++++-------------- test/functional/route/task/delete.js | 61 ++++++++++++++++ test/functional/route/task/index.js | 94 ++++++++++++++++++++++++ test/functional/route/task/run.js | 31 ++++++++ test/functional/setup.js | 2 + view/partial/result.html | 14 ++-- view/partial/task-header.html | 4 +- view/task/delete.html | 2 +- view/task/index.html | 2 +- 11 files changed, 314 insertions(+), 110 deletions(-) create mode 100644 test/functional/helper/webservice.js create mode 100644 test/functional/route/task/delete.js create mode 100644 test/functional/route/task/index.js create mode 100644 test/functional/route/task/run.js diff --git a/test/functional/helper/webservice.js b/test/functional/helper/webservice.js new file mode 100644 index 0000000..5aafecc --- /dev/null +++ b/test/functional/helper/webservice.js @@ -0,0 +1,14 @@ +'use strict'; + +var createClient = require('pa11y-webservice-client-node'); + +module.exports = createWebserviceClient; + +// Create a webservice client +function createWebserviceClient (config) { + var webserviceUrl = config.webservice; + if (typeof webserviceUrl == 'object') { + webserviceUrl = 'http://' + webserviceUrl.host + ':' + webserviceUrl.port + '/'; + } + return createClient(webserviceUrl); +} diff --git a/test/functional/route/index.js b/test/functional/route/index.js index 20f4224..41087f8 100644 --- a/test/functional/route/index.js +++ b/test/functional/route/index.js @@ -6,64 +6,60 @@ var assert = require('proclaim'); describe('GET /', function () { - describe('with no query', function () { + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/' + }; + this.navigate(req, done); + }); - beforeEach(function (done) { - var req = { - method: 'GET', - endpoint: '/' - }; - this.navigate(req, done); - }); + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); - it('should send a 200 status', function () { - assert.strictEqual(this.last.status, 200); - }); + it('should display an "Add new URL" button', function () { + var elem = this.last.dom.querySelectorAll('[data-test=add-task]'); + assert.strictEqual(elem.length, 1); + assert.strictEqual(elem[0].getAttribute('href'), '/new'); + }); - it('should have an "Add new URL" button', function () { - var elem = this.last.dom.querySelectorAll('[data-test=add-task]'); - assert.strictEqual(elem.length, 1); - assert.strictEqual(elem[0].getAttribute('href'), '/new'); - }); + it('should display all of the expected tasks', function () { + var tasks = this.last.dom.querySelectorAll('[data-test=task]'); + assert.strictEqual(tasks.length, 3); + assert.match(tasks[0].textContent, /npg home\s+\(wcag2aa\)/i); + assert.match(tasks[1].textContent, /npg home\s+\(wcag2aaa\)/i); + assert.match(tasks[2].textContent, /nature news\s+\(section508\)/i); + }); - it('should display all of the expected tasks', function () { - var tasks = this.last.dom.querySelectorAll('[data-test=task]'); - assert.strictEqual(tasks.length, 3); - assert.match(tasks[0].textContent, /npg home\s+\(wcag2aa\)/i); - assert.match(tasks[1].textContent, /npg home\s+\(wcag2aaa\)/i); - assert.match(tasks[2].textContent, /nature news\s+\(section508\)/i); - }); + it('should have links to each task', function () { + var tasks = this.last.dom.querySelectorAll('[data-test=task]'); + assert.strictEqual(tasks[0].querySelectorAll('[href="/abc000000000000000000001"]').length, 1); + assert.strictEqual(tasks[1].querySelectorAll('[href="/abc000000000000000000002"]').length, 1); + assert.strictEqual(tasks[2].querySelectorAll('[href="/abc000000000000000000003"]').length, 1); + }); - it('should have links to each task', function () { - var tasks = this.last.dom.querySelectorAll('[data-test=task]'); - assert.strictEqual(tasks[0].querySelectorAll('[href="/abc000000000000000000001"]').length, 1); - assert.strictEqual(tasks[1].querySelectorAll('[href="/abc000000000000000000002"]').length, 1); - assert.strictEqual(tasks[2].querySelectorAll('[href="/abc000000000000000000003"]').length, 1); - }); + it('should display a delete button for each task', function () { + var tasks = this.last.dom.querySelectorAll('[data-test=task]'); + assert.strictEqual(tasks[0].querySelectorAll('[href="/abc000000000000000000001/delete"]').length, 1); + assert.strictEqual(tasks[1].querySelectorAll('[href="/abc000000000000000000002/delete"]').length, 1); + assert.strictEqual(tasks[2].querySelectorAll('[href="/abc000000000000000000003/delete"]').length, 1); + }); - it('should display a delete button for each task', function () { - var tasks = this.last.dom.querySelectorAll('[data-test=task]'); - assert.strictEqual(tasks[0].querySelectorAll('[href="/abc000000000000000000001/delete"]').length, 1); - assert.strictEqual(tasks[1].querySelectorAll('[href="/abc000000000000000000002/delete"]').length, 1); - assert.strictEqual(tasks[2].querySelectorAll('[href="/abc000000000000000000003/delete"]').length, 1); - }); + it('should display the task result counts if the task has been run', function () { + var tasks = this.last.dom.querySelectorAll('[data-test=task]'); + assert.match(tasks[0].textContent, /1\s*errors/i); + assert.match(tasks[0].textContent, /2\s*warnings/i); + assert.match(tasks[0].textContent, /3\s*notices/i); + }); - it('should display the task result counts if the task has been run', function () { - var tasks = this.last.dom.querySelectorAll('[data-test=task]'); - assert.match(tasks[0].textContent, /1\s*errors/i); - assert.match(tasks[0].textContent, /2\s*warnings/i); - assert.match(tasks[0].textContent, /3\s*notices/i); - }); - - it('should display a message indicating that there are no results if the task has not been run', function () { - var tasks = this.last.dom.querySelectorAll('[data-test=task]'); - assert.match(tasks[2].textContent, /no results/i); - }); - - it('should not display an alert message', function () { - assert.strictEqual(this.last.dom.querySelectorAll('[data-test=alert]').length, 0); - }); + it('should display a message indicating that there are no results if the task has not been run', function () { + var tasks = this.last.dom.querySelectorAll('[data-test=task]'); + assert.match(tasks[2].textContent, /no results/i); + }); + it('should not display an alert message', function () { + assert.strictEqual(this.last.dom.querySelectorAll('[data-test=alert]').length, 0); }); }); diff --git a/test/functional/route/new.js b/test/functional/route/new.js index d40d7e9..c5bc40d 100644 --- a/test/functional/route/new.js +++ b/test/functional/route/new.js @@ -6,63 +6,59 @@ var assert = require('proclaim'); describe('GET /new', function () { - describe('with no query', function () { + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/new' + }; + this.navigate(req, done); + }); - beforeEach(function (done) { - var req = { - method: 'GET', - endpoint: '/new' - }; - this.navigate(req, done); + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should not display an error message', function () { + assert.strictEqual(this.last.dom.querySelectorAll('[data-test=error]').length, 0); + }); + + it('should have an "Add new URL" form', function () { + var form = this.last.dom.querySelectorAll('[data-test=new-url-form]')[0]; + assert.isDefined(form); + assert.strictEqual(form.getAttribute('action'), '/new'); + assert.strictEqual(form.getAttribute('method'), 'post'); + }); + + describe('"Add New URL" form', function () { + + beforeEach(function () { + this.form = this.last.dom.querySelectorAll('[data-test=new-url-form]')[0]; }); - it('should send a 200 status', function () { - assert.strictEqual(this.last.status, 200); + it('should have a "name" field', function () { + var field = this.form.querySelectorAll('input[name=name]')[0]; + assert.isDefined(field); + assert.strictEqual(field.getAttribute('type'), 'text'); + assert.strictEqual(field.getAttribute('value'), ''); }); - it('should not display an error message', function () { - assert.strictEqual(this.last.dom.querySelectorAll('[data-test=error]').length, 0); + it('should have a "url" field', function () { + var field = this.form.querySelectorAll('input[name=url]')[0]; + assert.isDefined(field); + assert.strictEqual(field.getAttribute('type'), 'url'); + assert.strictEqual(field.getAttribute('value'), ''); }); - it('should have an "Add new URL" form', function () { - var form = this.last.dom.querySelectorAll('[data-test=new-url-form]')[0]; - assert.isDefined(form); - assert.strictEqual(form.getAttribute('action'), '/new'); - assert.strictEqual(form.getAttribute('method'), 'post'); + it('should have a "standard" field', function () { + var field = this.form.querySelectorAll('select[name=standard]')[0]; + assert.isDefined(field); + assert.strictEqual(field.querySelectorAll('option').length, 4); }); - describe('"Add New URL" form', function () { - - beforeEach(function () { - this.form = this.last.dom.querySelectorAll('[data-test=new-url-form]')[0]; - }); - - it('should have a "name" field', function () { - var field = this.form.querySelectorAll('input[name=name]')[0]; - assert.isDefined(field); - assert.strictEqual(field.getAttribute('type'), 'text'); - assert.strictEqual(field.getAttribute('value'), ''); - }); - - it('should have a "url" field', function () { - var field = this.form.querySelectorAll('input[name=url]')[0]; - assert.isDefined(field); - assert.strictEqual(field.getAttribute('type'), 'url'); - assert.strictEqual(field.getAttribute('value'), ''); - }); - - it('should have a "standard" field', function () { - var field = this.form.querySelectorAll('select[name=standard]')[0]; - assert.isDefined(field); - assert.strictEqual(field.querySelectorAll('option').length, 4); - }); - - it('should have "ignore" fields', function () { - var fields = this.form.querySelectorAll('input[name="ignore[]"]'); - assert.isDefined(fields); - assert.notStrictEqual(fields.length, 0); - }); - + it('should have "ignore" fields', function () { + var fields = this.form.querySelectorAll('input[name="ignore[]"]'); + assert.isDefined(fields); + assert.notStrictEqual(fields.length, 0); }); }); @@ -114,11 +110,15 @@ describe('POST /new', function () { assert.strictEqual(this.last.status, 200); }); + it('should create the task', function (done) { + this.webservice.tasks.get({}, function (err, tasks) { + assert.strictEqual(tasks.length, 4); + done(); + }); + }); + it('should redirect me to the new URL page', function () { - var title = this.last.dom.querySelectorAll('title')[0]; - assert.isDefined(title); - assert.match(title.textContent, /example.com/i); - assert.match(title.textContent, /wcag2aa/i); + assert.match(this.last.request.uri.pathname, /^\/[a-z0-9]{24}$/i); }); it('should not display an error message', function () { diff --git a/test/functional/route/task/delete.js b/test/functional/route/task/delete.js new file mode 100644 index 0000000..bbc674b --- /dev/null +++ b/test/functional/route/task/delete.js @@ -0,0 +1,61 @@ +/* global beforeEach, describe, it */ +/* jshint maxlen: false, maxstatements: false */ +'use strict'; + +var assert = require('proclaim'); + +describe('GET //delete', function () { + + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/abc000000000000000000001/delete' + }; + this.navigate(req, done); + }); + + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should have a "Delete URL" form', function () { + var form = this.last.dom.querySelectorAll('[data-test=delete-url-form]')[0]; + assert.isDefined(form); + assert.strictEqual(form.getAttribute('action'), '/abc000000000000000000001/delete'); + assert.strictEqual(form.getAttribute('method'), 'post'); + }); + +}); + +describe('POST //delete', function () { + + beforeEach(function (done) { + var req = { + method: 'POST', + endpoint: '/abc000000000000000000001/delete' + }; + this.navigate(req, done); + }); + + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should delete the task', function (done) { + this.webservice.task('abc000000000000000000001').get({}, function (err) { + assert.strictEqual(err.message, 'Error 404'); + done(); + }); + }); + + it('should redirect me to the home page', function () { + assert.strictEqual(this.last.request.uri.pathname, '/'); + }); + + it('should display a success message', function () { + var alert = this.last.dom.querySelectorAll('[data-test=alert]')[0]; + assert.isDefined(alert); + assert.match(alert.textContent, /been deleted/i); + }); + +}); diff --git a/test/functional/route/task/index.js b/test/functional/route/task/index.js new file mode 100644 index 0000000..8970aff --- /dev/null +++ b/test/functional/route/task/index.js @@ -0,0 +1,94 @@ +/* global beforeEach, describe, it */ +/* jshint maxlen: false, maxstatements: false */ +'use strict'; + +var assert = require('proclaim'); + +describe('GET /', function () { + + describe('when task has results', function () { + + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/abc000000000000000000001' + }; + this.navigate(req, done); + }); + + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should display a "Run" button', function () { + var elem = this.last.dom.querySelectorAll('[data-test=run-task]'); + assert.strictEqual(elem.length, 1); + assert.strictEqual(elem[0].getAttribute('href'), '/abc000000000000000000001/run'); + }); + + it('should display a "Download CSV" button for the latest result', function () { + var elem = this.last.dom.querySelectorAll('[data-test=download-csv]'); + assert.strictEqual(elem.length, 1); + assert.strictEqual(elem[0].getAttribute('href'), '/abc000000000000000000001/def000000000000000000001.csv'); + }); + + it('should display a "Download JSON" button for the latest result', function () { + var elem = this.last.dom.querySelectorAll('[data-test=download-json]'); + assert.strictEqual(elem.length, 1); + assert.strictEqual(elem[0].getAttribute('href'), '/abc000000000000000000001/def000000000000000000001.json'); + }); + + it('should display links to all results', function () { + assert.isDefined(this.last.dom.querySelectorAll('[href="/abc000000000000000000001/def000000000000000000001"]')[0]); + assert.isDefined(this.last.dom.querySelectorAll('[href="/abc000000000000000000001/def000000000000000000003"]')[0]); + }); + + it('should display errors', function () { + var elem = this.last.dom.querySelectorAll('[data-test=task-errors]')[0]; + assert.isDefined(elem); + assert.match(elem.textContent, /errors \( 1 \)/i); + }); + + it('should display warnings', function () { + var elem = this.last.dom.querySelectorAll('[data-test=task-warnings]')[0]; + assert.isDefined(elem); + assert.match(elem.textContent, /warnings \( 2 \)/i); + }); + + it('should display notices', function () { + var elem = this.last.dom.querySelectorAll('[data-test=task-notices]')[0]; + assert.isDefined(elem); + assert.match(elem.textContent, /notices \( 3 \)/i); + }); + + }); + + describe('when task has no results', function () { + + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/abc000000000000000000003' + }; + this.navigate(req, done); + }); + + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should display a "Run" button', function () { + var elem = this.last.dom.querySelectorAll('[data-test=run-task]'); + assert.strictEqual(elem.length, 1); + assert.strictEqual(elem[0].getAttribute('href'), '/abc000000000000000000003/run'); + }); + + it('should display a message indicating that there are no results', function () { + var alert = this.last.dom.querySelectorAll('[data-test=alert]')[0]; + assert.isDefined(alert); + assert.match(alert.textContent, /there are no results to show/i); + }); + + }); + +}); diff --git a/test/functional/route/task/run.js b/test/functional/route/task/run.js new file mode 100644 index 0000000..c70a1f0 --- /dev/null +++ b/test/functional/route/task/run.js @@ -0,0 +1,31 @@ +/* global beforeEach, describe, it */ +/* jshint maxlen: false, maxstatements: false */ +'use strict'; + +var assert = require('proclaim'); + +describe('GET //run', function () { + + beforeEach(function (done) { + var req = { + method: 'GET', + endpoint: '/abc000000000000000000001/run' + }; + this.navigate(req, done); + }); + + it('should send a 200 status', function () { + assert.strictEqual(this.last.status, 200); + }); + + it('should redirect me to the task page', function () { + assert.strictEqual(this.last.request.uri.pathname, '/abc000000000000000000001'); + }); + + it('should display a success message', function () { + var alert = this.last.dom.querySelectorAll('[data-test=alert]')[0]; + assert.isDefined(alert); + assert.match(alert.textContent, /new results are being generated/i); + }); + +}); diff --git a/test/functional/setup.js b/test/functional/setup.js index a8ffabc..d089ee9 100644 --- a/test/functional/setup.js +++ b/test/functional/setup.js @@ -4,6 +4,7 @@ var config = require('../../config/test.json'); var createNavigator = require('./helper/navigate'); +var createWebserviceClient = require('./helper/webservice'); var loadFixtures = require('pa11y-webservice/data/fixture/load'); var request = require('request'); @@ -12,6 +13,7 @@ before(function (done) { this.baseUrl = 'http://localhost:' + config.port; this.last = {}; this.navigate = createNavigator(this.baseUrl, this.last); + this.webservice = createWebserviceClient(config); assertTestAppIsRunning(this.baseUrl, function () { loadFixtures('test', config.webservice, done); }); diff --git a/view/partial/result.html b/view/partial/result.html index ea4dba7..2bf2b4e 100644 --- a/view/partial/result.html +++ b/view/partial/result.html @@ -12,10 +12,14 @@ @@ -37,7 +41,7 @@
{{#if mainResult.count.error}} -
+
- Errors ( {{mainResult.count.error}} )
@@ -58,7 +62,7 @@ {{/if}} {{#if mainResult.count.warning}} -
+
+ Warnings ( {{mainResult.count.warning}} )
@@ -79,7 +83,7 @@ {{/if}} {{#if mainResult.count.notice}} -
+
+ Notices ( {{mainResult.count.notice}} )
diff --git a/view/partial/task-header.html b/view/partial/task-header.html index 409d6f1..3c09f16 100644 --- a/view/partial/task-header.html +++ b/view/partial/task-header.html @@ -7,7 +7,9 @@
{{#unless readonly}} - Run + + Run + {{/unless}} {{#if mainResult}}
Last run : {{date-format mainResult.date format="DD MMM YYYY"}}
diff --git a/view/task/delete.html b/view/task/delete.html index 75b5c9b..c8c6e31 100644 --- a/view/task/delete.html +++ b/view/task/delete.html @@ -3,7 +3,7 @@ Delete {{task.url}} ({{task.standard}}) {{/content}} -
+
Delete URL ({{simplify-url task.url}})
diff --git a/view/task/index.html b/view/task/index.html index f7069a1..82605e3 100644 --- a/view/task/index.html +++ b/view/task/index.html @@ -41,7 +41,7 @@ {{#if mainResult}} {{> result}} {{else}} -
+

There are no results to show

pa11y has not been run against this URL yet so there are no results to show.