Skip to content

Commit

Permalink
Testing for Evented module (#215)
Browse files Browse the repository at this point in the history
* add evented tests

* change naming order of tests to follow modules
  • Loading branch information
chuckcarpenter authored and RobbieTheWagner committed Aug 22, 2018
1 parent c036552 commit f1d3c9a
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 126 deletions.
4 changes: 2 additions & 2 deletions src/js/evented.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -68,4 +68,4 @@ export class Evented {
}
}

}
}
40 changes: 40 additions & 0 deletions test/test.evented.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
74 changes: 36 additions & 38 deletions test/test.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
112 changes: 55 additions & 57 deletions test/test.tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
56 changes: 27 additions & 29 deletions test/test.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
});

0 comments on commit f1d3c9a

Please sign in to comment.