From 098f71172d86fdd7130e9e44ed88c2f6c198d287 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Fri, 29 Sep 2017 13:26:47 +0100 Subject: [PATCH] Fix further tests --- config/test.js | 37 ++++++++++++++----------- src/scripts/src/choices.spec.js | 8 +++--- src/scripts/src/components/container.js | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/config/test.js b/config/test.js index 2da2edd..6ad56e9 100644 --- a/config/test.js +++ b/config/test.js @@ -20,30 +20,35 @@ function ignoreExtensions(extensions = [], returnValue = {}) { }); } -function mockStorage() { - return { - removeItem: function(key) { - delete this[key]; - }, - getItem: function(key) { - return this[key]; - }, - setItem: function(key, value) { - this[key] = value; - }, - clear: function() {} - } +function mockRAF(global) { + let callbacksQueue = []; + + global.setInterval(() => { + for (let i = 0; i < callbacksQueue.length; i++) { + if (callbacksQueue[i] !== false) { + callbacksQueue[i].call(null); + } + } + + callbacksQueue = []; + }, 1000 / 60); + + global.requestAnimationFrame = callback => callbacksQueue.push(callback) - 1; + + global.cancelAnimationFrame = (id) => { + callbacksQueue[id] = false; + }; } global.window = window; global.document = window.document; global.navigator = { - userAgent: 'node.js' + userAgent: 'node.js', }; global.HTMLElement = window.HTMLElement; -global.window.localStorage = mockStorage; -global.window.sessionStorage = mockStorage; copyProps(window, global); +mockRAF(global); + ignoreExtensions(['.scss', '.css']); ignoreExtensions(['.jpg', '.png', '.svg'], ''); diff --git a/src/scripts/src/choices.spec.js b/src/scripts/src/choices.spec.js index a4f1e65..522c467 100644 --- a/src/scripts/src/choices.spec.js +++ b/src/scripts/src/choices.spec.js @@ -380,7 +380,7 @@ describe('Choices', () => { preventDefault: () => {}, }); - const returnValue = onChangeStub.calls.mostRecent().args[0].detail.value; + const returnValue = onChangeStub.lastCall.args[0].detail.value; expect(returnValue).to.be.a('string'); expect(onChangeStub.callCount).to.equal(1); expect(addSpyStub.callCount).to.equal(1); @@ -473,7 +473,7 @@ describe('Choices', () => { passedElement.addEventListener('search', onSearchStub); instance.input.element.focus(); - instance.input.value = '3 '; + instance.input.element.value = '3 '; // Key down to search instance._onKeyUp({ @@ -482,9 +482,9 @@ describe('Choices', () => { ctrlKey: false, }); - const mostAccurateResult = instance.currentState.choices.filter(choice => choice.active); + const mostAccurateResult = instance.currentState.choices.find(choice => choice.active); - expect(instance.isSearching && mostAccurateResult[0].value === 'Value 3').to.be.true; + expect(instance.isSearching && mostAccurateResult.value === 'Value 3').to.be.true; expect(onSearchStub.callCount).to.equal(1); }); diff --git a/src/scripts/src/components/container.js b/src/scripts/src/components/container.js index b5689f1..8015a06 100644 --- a/src/scripts/src/components/container.js +++ b/src/scripts/src/components/container.js @@ -52,7 +52,7 @@ export default class Container { * @returns */ shouldFlip(dropdownPos) { - if (!dropdownPos) { + if (dropdownPos === undefined) { return false; }