Skip to content

Commit

Permalink
Move tests and fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashdoughty committed Nov 1, 2019
1 parent 351c9b3 commit 9f7ae9f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 59 deletions.
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions tasks/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const gulpNunjucks = require('gulp-nunjucks');
const nunjucks = require('nunjucks');
const connect = require('gulp-connect');

var config = {
templates: ['app/_templates', 'packages'],
dest: 'dist/app',
const config = {
baseUrl: process.env.BASE_URL ? process.env.BASE_URL : '',
}
dest: 'dist/app',
templates: ['app/_templates', 'packages'],
};

/**
* Turn markdown into html with a nunjucks layout
Expand All @@ -26,34 +26,34 @@ function buildHtml() {
.pipe(rename({
extname: '.html',
}))
.pipe(gulp.dest(config.dest))
.pipe(gulp.dest(config.dest));
}

/**
* Copy built assets from dist into the documentation directory
*/
function copyBuiltAssets() {
return gulp.src('dist/*.{css,js}')
.pipe(gulp.dest(config.dest + '/assets/'))
.pipe(gulp.dest(`${config.dest}/assets/`));
}

/**
* Copy logos, icons and other binary assets
*/
function copyBinaryAssets() {
return gulp.src('packages/assets/**/*')
.pipe(gulp.dest(config.dest + '/assets/'))
.pipe(gulp.dest(`${config.dest}/assets/`));
}

/**
* Serve the static docs directory over localhost
*/
function serve() {
connect.server({
root: config.dest,
host: '0.0.0.0',
livereload: true,
port: 3000,
host: '0.0.0.0',
root: config.dest,
});
}

Expand All @@ -62,7 +62,7 @@ function serve() {
*/
function reload() {
return gulp.src(config.dest)
.pipe(connect.reload())
.pipe(connect.reload());
}

gulp.task('docs:build', gulp.series([
Expand All @@ -71,7 +71,8 @@ gulp.task('docs:build', gulp.series([
copyBinaryAssets,
reload,
]));

gulp.task('docs:serve', gulp.series([
'docs:build',
gulp.parallel(serve)
gulp.parallel(serve),
]));
43 changes: 22 additions & 21 deletions packages/common.test.js → tests/integration/common.test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
import { toggleAttribute, toggleClass } from './common';
import { toggleAttribute, toggleClass } from '../../packages/common';

// DOM Elements to be set
let testElement;
const undefinedElement = document.querySelector('.nhs-false-element');
describe('toggleAttribute util', () => {
const attr = 'aria-expanded';
let testElement;

// Helper to set DOM Elements
const initTest = (html = '<div class="nhsuk-test" />') => {
document.body.innerHTML = html;
testElement = document.querySelector('.nhsuk-test');
};
beforeEach(() => {
document.body.innerHTML = '<div class="nhsuk-test" />';
testElement = document.querySelector('.nhsuk-test');
});

describe('toggleAttribute util', () => {
describe('Does not throw an error', () => {
it('if the element does not exist', () => {
initTest('');
const undefinedElement = document.querySelector('.nhs-false-element');
expect(undefinedElement).toBeNull();
toggleAttribute(document.querySelector('.nhs-false-element'), 'aria-expanded');
toggleAttribute(undefinedElement, 'aria-expanded');
});
});

describe('Adds attribute with value of "true"', () => {
const attr = 'aria-expanded';

it('if the element currently has no attribute', () => {
initTest();
expect(testElement).toBeDefined();
toggleAttribute(testElement, attr);
expect(testElement.getAttribute(attr)).toEqual('true');
});

it('if the attributes current value is "false"', () => {
initTest(`<div class="nhsuk-test" ${attr}="false" />`);
document.body.innerHTML = `<div class="nhsuk-test" ${attr}="false" />`;
expect(testElement).toBeDefined();
toggleAttribute(testElement, attr);
expect(testElement.getAttribute(attr)).toEqual('true');
});
});

describe('Adds attribute with value of "false"', () => {
const attr = 'aria-expanded';

it('if the attributes current value is "true"', () => {
initTest(`<div class="nhsuk-test" ${attr}="true" />`);
document.body.innerHTML = `<div class="nhsuk-test" ${attr}="true" />`;
testElement = document.querySelector('.nhsuk-test');
expect(testElement).toBeDefined();
toggleAttribute(testElement, attr);
expect(testElement.getAttribute(attr)).toEqual('false');
Expand All @@ -51,18 +46,23 @@ describe('toggleAttribute util', () => {

describe('toggleClass util', () => {
const className = 'test-class';
let testElement;

beforeEach(() => {
document.body.innerHTML = '<div class="nhsuk-test" />';
testElement = document.querySelector('.nhsuk-test');
});

describe('Does not throw an error', () => {
it('if the element does not exist', () => {
initTest('');
const undefinedElement = document.querySelector('.nhs-false-element');
expect(undefinedElement).toBeNull();
toggleClass(undefinedElement, 'test-class');
});
});

describe('Adds class', () => {
it('if the class does not already exist on the element', () => {
initTest();
expect(testElement).toBeDefined();
toggleClass(testElement, className);
expect(testElement.classList.contains(className)).toEqual(true);
Expand All @@ -71,7 +71,8 @@ describe('toggleClass util', () => {

describe('Removes class', () => {
it('if the class already exists on the element', () => {
initTest(`<div class="nhsuk-test ${className}" />`);
document.body.innerHTML = `<div class="nhsuk-test ${className}" />`;
testElement = document.querySelector('.nhsuk-test');
expect(testElement).toBeDefined();
toggleClass(testElement, className);
expect(testElement.classList.contains(className)).toEqual(false);
Expand Down
Loading

0 comments on commit 9f7ae9f

Please sign in to comment.