Skip to content

Commit

Permalink
Add Mocha for JS testing and initial spec for the tabs module
Browse files Browse the repository at this point in the history
  • Loading branch information
ediblecode committed Sep 23, 2016
1 parent af8ac0f commit c949d34
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .grunt-tasks/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
"webpack.production.config.js",
".grunt-tasks/**/*.js",
"web/server/**/*.js",
"web/client/**/*.js"
"web/client/**/*.js",
"test/**/*.js"
]
},
options: {
Expand Down
8 changes: 8 additions & 0 deletions .grunt-tasks/mochaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
test : {
src: ["test/**/*.js"],
options: {
require: "babel-core/register"
}
}
};
3 changes: 0 additions & 3 deletions .grunt-tasks/webpack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
var _ = require("lodash"),
webpack = require("webpack");

var webpackConfig = require("./../webpack.config.js"),
webpackProdConfig = require("./../webpack.production.config.js");

Expand Down
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = grunt => {
// Generate documentation form comments in SASS and JS
r("docs", ["sassdoc", "documentation"]);

r("test", ["mochaTest"]);

// For deploying the web app. Builds minified SASS/JS
r("dist", ["env:dist", "clean", "sass:dist", "webpack:dist"]);

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ Before you run any tasks, you'll have to run the following from the command line
| Task | Description |
| ---- | ----------- |
| `grunt` | Default. Lints, builds everything in parallel and runs an express server and a watch task for dev changes. |
| `grunt lint` | Lints SASS (sasslint) and JS (eslint) |
| `grunt lint` | Lints SASS and JS |
| `grunt test` | Runs JS tests |
| `grunt dist` | Builds documentation, modernizr, CSS and JS in production mode (minified etc). Fro deploying the web app itself. |
| `grunt prepublish` | Builds minified code for publishing to npm etc |

Expand All @@ -154,10 +155,11 @@ There are a set of npm scripts within package.json, for convenience. However, it

| Task | Description |
| ---- | ----------- |
| `npm start` | Simply runs `grunt` under the hood |
| `npm test` | TODO |
| `npm run lint` | Lints SASS and JS (uses `grunt lint` under the hood) |
| `npm run serve` | Spins up an express server through Node directly (NOT via Grunt) on port *54321* |
| `npm start` | Simply runs `grunt` under the hood |
| `npm test` | Runs JS tests |
| `npm run test:watch` | Runs JS test tests and watches for changes. Useful to run in development alongside grunt. |
| `npm run lint` | Lints SASS and JS (uses `grunt lint` under the hood) |
| `npm run serve` | Spins up an express server through Node directly (NOT via Grunt) on port *54321* |

#### Node

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"start": "grunt",
"webpack:dev": "SET \"NODE_ENV=development\" && webpack --progress --colors --display-error-details",
"webpack:dist": "SET \"NODE_ENV=production\" && webpack --config ./webpack.production.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha test/**/*.spec.js --require babel-core/register",
"test:watch": "mocha test/**/*.spec.js --require babel-core/register --reporter min --watch",
"lint": "grunt lint",
"serve": "set PORT=54321&& node web/server",
"prepublish": "grunt prepublish"
},
"config": {
"//": "Banner is used for prepending to compiled JS and CSS",
"banner": "NICE Experience ${ version }\n© Copyright NICE 2015-${ year }\nLicensed under MIT (https://github.com/nhsevidence/nice-experience/blob/master/LICENSE)"
"//": "Banner is used for prepending to compiled JS and CSS",
"banner": "NICE Experience ${ version }\n© Copyright NICE 2015-${ year }\nLicensed under MIT (https://github.com/nhsevidence/nice-experience/blob/master/LICENSE)"
},
"author": "NICE Digital Services (https://github.com/nhsevidence/nice-experience/graphs/contributors)",
"contributors": [
Expand Down Expand Up @@ -72,6 +73,7 @@
"grunt-env": "^0.4.4",
"grunt-eslint": "^19.0.0",
"grunt-jsdoc": "^2.1.0",
"grunt-mocha-test": "^0.13.2",
"grunt-modernizr": "^1.0.2",
"grunt-newer": "^1.2.0",
"grunt-sass": "^1.2.1",
Expand All @@ -82,14 +84,17 @@
"jit-grunt": "^0.10.0",
"jquery": "^3.1.0",
"jsdoc-babel": "^0.2.1",
"jsdom": "^9.5.0",
"load-grunt-config": "^0.19.2",
"load-grunt-tasks": "^3.5.0",
"lodash": "^4.13.1",
"mocha": "^3.0.2",
"node-common-errors": "^0.4.0",
"node-sass": "^3.10.0",
"nunjucks": "^2.5.2",
"sassdoc-extras": "^2.4.1",
"serve-favicon": "^2.3.0",
"should": "^11.1.0",
"string-replace-webpack-plugin": "0.0.3",
"webpack": "^1.13.2"
},
Expand Down
158 changes: 158 additions & 0 deletions test/unit/tabs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* eslint-env node, mocha */

const should = require("should"),
jsdom = require("jsdom");

describe("Tabs", function() {

var $,
document,
Tabs;

var tabsHTML = `
<div class="tabs" data-nice-plugin="tabs">
<nav>
<ul class="tabs__list" role="tablist">
<li class="tabs__tab" role="presentation">
<button class="tabs__tab-btn" type="button" role="tab">
Tab 1
</button>
</li>
<li class="tabs__tab" role="presentation">
<button class="tabs__tab-btn"type="button" role="tab">
Tab 2
</abutton>
</li>
<li class="tabs__tab" role="presentation">
<button class="tabs__tab-btn" type="button" role="tab">
Tab 3
</abutton>
</li>
</ul>
</nav>
<div class="tabs__content">
<div lass="tabs__pane" role="tabpanel">
<h2>First tab</h2>
<p>This is the content for tab 1</p>
</div>
<div class="tabs__pane" role="tabpanel">
<h2>Second tab</h2>
<p>This is the content for tab 2</p>
</div>
<div class="tabs__pane" role="tabpanel">
<h2>Third tab</h2>
<p>This is the content for tab 3</p>
</div>
</div>
</div>
`;

before(function () {
document = global.document = jsdom.jsdom("<html><head></head><body></body></html>");
global.window = document.defaultView;
$ = global.jQuery = global.$ = require("jquery");

Tabs = require("../../src/javascripts/tabs.js").default;
});


describe("jQuery integration", function() {

it("defined on jquery object", function() {
$(document).tabs.should.be.ok();
});

it("have defaults", function() {
$(document).tabs.should.have.property("defaults");
});

// TODO: Test no conflict

it("return jquery collection containing the element", function() {
var $el = $("<div/>");
var $tabs = $el.tabs();

$tabs.should.be.an.instanceOf($, "returns jquery collection");
$tabs[0].should.equal($el[0], "collection contains element");
});

it("tab can be activated by index", function() {
var $el = $(tabsHTML).tabs();
$el.tabs("activate", 1);
($el.tabs("getCurrentIndex")).should.equal(1);
});

it("last tab can be activated", function() {
var $el = $(tabsHTML).tabs();
$el.tabs("last");
($el.tabs("getCurrentIndex")).should.equal(2);
});

it("first tab can be activated", function() {
var $el = $(tabsHTML).tabs();
$el.tabs("last").tabs("first");
($el.tabs("getCurrentIndex")).should.equal(0);
});

it("next tab can be activated", function() {
var $el = $(tabsHTML).tabs();
$el.tabs("next");
($el.tabs("getCurrentIndex")).should.equal(1);
});

it("previous tab can be activated", function() {
var $el = $(tabsHTML).tabs();
$el.tabs("last").tabs("previous");
($el.tabs("getCurrentIndex")).should.equal(1);
});
});


describe("Tabs", function() {

it("have defaults", function() {
Tabs.should.have.property("defaults");
});

it("throw without element", function() {
should.throws(() => new Tabs);
});

it("first tab selected by default", function() {
var $el = $(tabsHTML);
var t = new Tabs($el);
t.getCurrentIndex().should.equal(0);
});

it("tab can be activated by index", function() {
var $el = $(tabsHTML);
var t = new Tabs($el);
t.activate(1);
t.getCurrentIndex().should.equal(1);
});
});


describe("Accessibility", function() {

it("active tab is aria-expanded", function() {
var $tabs = $(tabsHTML);
$tabs.tabs();
$("[role='tab']:eq(0)", $tabs).attr("aria-expanded").should.equal("true");
$("[role='tab']:eq(0)", $tabs).attr("aria-selected").should.equal("true");
});

it("inactive tabs are not aria-expanded", function() {
var $tabs = $(tabsHTML);
$tabs.tabs();
$("[role='tab']:gt(0)", $tabs).attr("aria-expanded").should.equal("false");
$("[role='tab']:gt(0)", $tabs).attr("aria-selected").should.equal("false");
});

});

describe("Keyboard", function() {


});
});

0 comments on commit c949d34

Please sign in to comment.