From f1d3c9ad8a6b0d9c36a2b303f713cfa0aa251ebe Mon Sep 17 00:00:00 2001 From: Chuck Carpenter Date: Wed, 22 Aug 2018 12:31:16 -0400 Subject: [PATCH] Testing for Evented module (#215) * add evented tests * change naming order of tests to follow modules --- src/js/evented.js | 4 +- test/test.evented.js | 40 ++++++++++++++++ test/test.step.js | 74 ++++++++++++++-------------- test/test.tour.js | 112 +++++++++++++++++++++---------------------- test/test.utils.js | 56 +++++++++++----------- 5 files changed, 160 insertions(+), 126 deletions(-) create mode 100644 test/test.evented.js diff --git a/src/js/evented.js b/src/js/evented.js index 75d073d19..bed70c3fc 100644 --- a/src/js/evented.js +++ b/src/js/evented.js @@ -21,7 +21,7 @@ export class Evented { off(event, handler) { if (typeof this.bindings === 'undefined' || typeof this.bindings[event] === 'undefined') { - return; + return false; } if (typeof handler === 'undefined') { @@ -68,4 +68,4 @@ export class Evented { } } -} \ No newline at end of file +} diff --git a/test/test.evented.js b/test/test.evented.js new file mode 100644 index 000000000..8d0ce1d57 --- /dev/null +++ b/test/test.evented.js @@ -0,0 +1,40 @@ +/* global describe,it */ +import { assert } from 'chai'; +import { Evented } from '../src/js/evented'; + +describe('Evented', function() { + const testEvent = new Evented(); + let testOnTriggered = false; + describe('on()', function(){ + it('adds a new event binding', function(){ + testEvent.on('testOn', () => testOnTriggered = true ); + assert.ok(testEvent.bindings.testOn, 'custom event added'); + }); + }); + + describe('trigger()', function(){ + it('triggers a created event', function(){ + testEvent.trigger('testOn'); + assert.ok(testOnTriggered, 'true is returned from event trigger'); + }); + }); + + describe('off()', function(){ + it('removes an event binding', function(){ + testEvent.off('testOn'); + assert.notOk(testEvent.bindings.testOn, 'custom event removed'); + }); + + it('does not remove uncreated events', function(){ + assert.notOk(testEvent.off('testBlank'), 'returns false for non created events'); + }); + }); + + describe('once()', function(){ + it('adds a new event binding that only triggers once', function(){ + testEvent.once('testOnce', () => true ); + testEvent.trigger('testOnce') + assert.ok(testEvent.bindings.testOnce, 'custom event removed after one trigger'); + }); + }); +}); diff --git a/test/test.step.js b/test/test.step.js index 40c8a6a59..47e11f4b5 100644 --- a/test/test.step.js +++ b/test/test.step.js @@ -4,52 +4,50 @@ import Shepherd from '../src/js/shepherd'; // since importing non UMD, needs assignment window.Shepherd = Shepherd; -describe('Shepherd', function() { - describe('.Step()', function() { - const instance = new Shepherd.Tour({ - defaults: { - classes: 'shepherd-theme-arrows', - scrollTo: true +describe('Step', function() { + const instance = new Shepherd.Tour({ + defaults: { + classes: 'shepherd-theme-arrows', + scrollTo: true + } + }); + + const testStep = instance.addStep('test', { + id: 'test', + text: 'This is a step for testing', + classes: 'example-step-extra-class', + buttons: [ + { + text: 'Next', + action: instance.next } - }); + ] + }); - const testStep = instance.addStep('test', { - id: 'test', - text: 'This is a step for testing', - classes: 'example-step-extra-class', - buttons: [ - { - text: 'Next', - action: instance.next - } - ] - }); - - const showTestStep = instance.addStep('test2', { - id: 'test2', - text: 'Another Step' - }); + const showTestStep = instance.addStep('test2', { + id: 'test2', + text: 'Another Step' + }); - it('has all the correct properties', function() { - const values = ['classes', 'scrollTo', 'id', 'text', 'buttons']; - assert.deepEqual(values, Object.keys(testStep.options)); - }); + it('has all the correct properties', function() { + const values = ['classes', 'scrollTo', 'id', 'text', 'buttons']; + assert.deepEqual(values, Object.keys(testStep.options)); + }); - describe('.hide()', function() { - it('shows step evoking method, regardless of order', function() { - instance.start(); - testStep.hide(); + describe('.hide()', function() { + it('shows step evoking method, regardless of order', function() { + instance.start(); + testStep.hide(); - assert.notEqual(document.querySelector('[data-id=test]').getAttribute('hidden'), null); - }); + assert.notEqual(document.querySelector('[data-id=test]').getAttribute('hidden'), null); }); + }); - describe('.show()', function() { - it('shows step evoking method, regardless of order', function() { - showTestStep.show(); + describe('.show()', function() { + it('shows step evoking method, regardless of order', function() { + showTestStep.show(); - assert.equal(document.querySelector('[data-id=test2]').dataset.id, 'test2'); - }); + assert.equal(document.querySelector('[data-id=test2]').dataset.id, 'test2'); }); }); }); diff --git a/test/test.tour.js b/test/test.tour.js index 86126e02c..ad42edc55 100644 --- a/test/test.tour.js +++ b/test/test.tour.js @@ -4,86 +4,84 @@ import Shepherd from '../src/js/shepherd'; // since importing non UMD, needs assignment window.Shepherd = Shepherd; -describe('Shepherd', function() { +describe('Tour', function() { const defaults = { classes: 'shepherd-theme-arrows', scrollTo: true }; - describe('.Tour()', function() { - after(function() { - instance.cancel(); - }); - - const instance = new Shepherd.Tour({ - defaults, - }); + after(function() { + instance.cancel(); + }); - it('creates a new tour instance', function() { - assert.isOk(instance instanceof Shepherd.Tour); - }); + const instance = new Shepherd.Tour({ + defaults, + }); - it('returns the default options on the instance', function() { - assert.isOk(instance.options); - }); + it('creates a new tour instance', function() { + assert.isOk(instance instanceof Shepherd.Tour); + }); - describe('.addStep()', function() { - it('adds tour steps', function() { - instance.addStep('test', { - id: 'test', - title: 'This is a test step for our tour' - }); + it('returns the default options on the instance', function() { + assert.isOk(instance.options); + }); - assert.equal(instance.steps.length, 1); + describe('.addStep()', function() { + it('adds tour steps', function() { + instance.addStep('test', { + id: 'test', + title: 'This is a test step for our tour' }); - // this is not working as documented - it('returns the step options', function() { - assert.equal(instance.options.defaults, defaults); - }); + assert.equal(instance.steps.length, 1); + }); - it('returns the step by ID with the right title', function() { - instance.addStep('test2', { - id: 'test2', - title: 'Another Step' - }); - - instance.addStep('test3', { - id: 'test3', - title: 'Yet, another test step' - }); - assert.equal(instance.steps.length, 3); - assert.equal(instance.getById('test').options.title, 'This is a test step for our tour'); + // this is not working as documented + it('returns the step options', function() { + assert.equal(instance.options.defaults, defaults); + }); + + it('returns the step by ID with the right title', function() { + instance.addStep('test2', { + id: 'test2', + title: 'Another Step' }); + instance.addStep('test3', { + id: 'test3', + title: 'Yet, another test step' + }); + assert.equal(instance.steps.length, 3); + assert.equal(instance.getById('test').options.title, 'This is a test step for our tour'); }); - describe('.start()', function() { - it('starts a tour that is the current active', function() { - instance.start(); + }); + + describe('.start()', function() { + it('starts a tour that is the current active', function() { + instance.start(); - assert.equal(instance, Shepherd.activeTour); - }); + assert.equal(instance, Shepherd.activeTour); }); + }); - describe('.getCurrentStep()', function() { - it('returns the currently shown step', function() { - assert.equal(instance.getCurrentStep().id, 'test'); - }); + describe('.getCurrentStep()', function() { + it('returns the currently shown step', function() { + assert.equal(instance.getCurrentStep().id, 'test'); }); + }); - describe('.next()', function() { - it('goes to the next step after next() is invoked', function() { - instance.next(); - assert.equal(instance.getCurrentStep().id, 'test2'); - }); + describe('.next()', function() { + it('goes to the next step after next() is invoked', function() { + instance.next(); + assert.equal(instance.getCurrentStep().id, 'test2'); }); + }); - describe('.back()', function() { - it('goes to the previous step after back() is invoked', function() { - instance.back(); - assert.equal(instance.getCurrentStep().id, 'test'); - }); + describe('.back()', function() { + it('goes to the previous step after back() is invoked', function() { + instance.back(); + assert.equal(instance.getCurrentStep().id, 'test'); }); }); }); diff --git a/test/test.utils.js b/test/test.utils.js index d861ad2c2..9f893bd6f 100644 --- a/test/test.utils.js +++ b/test/test.utils.js @@ -5,39 +5,37 @@ import { parseShorthand } from '../src/js/utils'; -describe('Shepherd', function() { - describe('Utils', function() { - describe('parsePosition', function() { - it('attachTo as an object', function() { - const attachTo = { - element: '.foo', - on: 'bottom' - }; - assert.equal(parsePosition(attachTo), attachTo, 'when attachTo already includes `element` and `on` return as is'); - assert.equal(parsePosition({}), null, 'when attachTo does not include `element` and `on`, return null'); - }); +describe('Utils', function() { + describe('parsePosition', function() { + it('attachTo as an object', function() { + const attachTo = { + element: '.foo', + on: 'bottom' + }; + assert.equal(parsePosition(attachTo), attachTo, 'when attachTo already includes `element` and `on` return as is'); + assert.equal(parsePosition({}), null, 'when attachTo does not include `element` and `on`, return null'); + }); - it('attachTo as a string', function() { - let attachTo = '.foo bottom'; - assert.deepEqual(parsePosition(attachTo), { element: '.foo', on: 'bottom' }, 'when attachTo is a string, return as object with `element` and `on`'); + it('attachTo as a string', function() { + let attachTo = '.foo bottom'; + assert.deepEqual(parsePosition(attachTo), { element: '.foo', on: 'bottom' }, 'when attachTo is a string, return as object with `element` and `on`'); - attachTo = '.foo notValid'; - assert.equal(parsePosition(attachTo), null, 'when `on` is not a valid direction, return null'); - }); + attachTo = '.foo notValid'; + assert.equal(parsePosition(attachTo), null, 'when `on` is not a valid direction, return null'); }); + }); - describe('parseShorthand', function() { - it('null or undefined', function() { - assert.equal(parseShorthand(null), null, 'null returns null'); - assert.equal(parseShorthand(undefined), undefined, 'undefined returns undefined'); - }); + describe('parseShorthand', function() { + it('null or undefined', function() { + assert.equal(parseShorthand(null), null, 'null returns null'); + assert.equal(parseShorthand(undefined), undefined, 'undefined returns undefined'); + }); - it('string of values', function() { - const values = '.foo click'; - const { event, selector } = parseShorthand(values, ['selector', 'event']); - assert.equal(event, 'click', 'maps event from string to event prop'); - assert.equal(selector, '.foo', 'maps selector from string to selector prop'); - }); + it('string of values', function() { + const values = '.foo click'; + const { event, selector } = parseShorthand(values, ['selector', 'event']); + assert.equal(event, 'click', 'maps event from string to event prop'); + assert.equal(selector, '.foo', 'maps selector from string to selector prop'); }); }); -}); \ No newline at end of file +});