From 2cba1c9306e620389d3d38c88297a80a09674d1e Mon Sep 17 00:00:00 2001 From: Keegan Date: Sat, 6 Feb 2016 18:31:09 -0800 Subject: [PATCH] add tests for manipulators --- src/scripts/lib/manipulators.js | 48 ++++++++++--------- test/spec/lib/manipulators.js | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 test/spec/lib/manipulators.js diff --git a/src/scripts/lib/manipulators.js b/src/scripts/lib/manipulators.js index cb33836..9e0d999 100755 --- a/src/scripts/lib/manipulators.js +++ b/src/scripts/lib/manipulators.js @@ -1,13 +1,29 @@ 'use strict'; +/** + * @returns {ClayEvents} + */ +function disable() { + this.$manipulatorTarget.set('disabled', true); + return this.trigger('disabled'); +} + +/** + * @returns {ClayEvents} + */ +function enable() { + this.$manipulatorTarget.set('disabled', false); + return this.trigger('enabled'); +} + module.exports = { html: { get: function() { return this.$manipulatorTarget.get('innerHTML'); }, set: function(value) { - return this.$manipulatorTarget.set('innerHTML', value) - .trigger('change'); + this.$manipulatorTarget.set('innerHTML', value); + return this.trigger('change'); } }, val: { @@ -15,33 +31,21 @@ module.exports = { return this.$manipulatorTarget.get('value'); }, set: function(value) { - return this.$manipulatorTarget.set('value', value) - .trigger('change'); + this.$manipulatorTarget.set('value', value) + return this.trigger('change'); }, - disable: function() { - return this.$manipulatorTarget.set('disabled', true) - .trigger('disabled'); - }, - enable: function() { - return this.$manipulatorTarget.set('disabled', false) - .trigger('enabled'); - } + disable: disable, + enable: enable }, checked: { get: function() { return this.$manipulatorTarget.get('checked'); }, set: function(value) { - return this.$manipulatorTarget.set('checked', value) - .trigger('change'); + this.$manipulatorTarget.set('checked', value) + return this.trigger('change'); }, - disable: function() { - return this.$manipulatorTarget.set('disabled', true) - .trigger('change'); - }, - enable: function() { - return this.$manipulatorTarget.set('disabled', false) - .trigger('change'); - } + disable: disable, + enable: enable } }; diff --git a/test/spec/lib/manipulators.js b/test/spec/lib/manipulators.js new file mode 100644 index 0000000..ace3b9f --- /dev/null +++ b/test/spec/lib/manipulators.js @@ -0,0 +1,83 @@ +'use strict'; + +var assert = require('chai').assert; +var sinon = require('sinon'); +var fixture = require('../../fixture'); + +describe('manipulators', function() { + + /** + * @param {string} itemType + * @param {*} value + * @return {void} + */ + function testSetGet(itemType, value) { + describe('.set() and .get()', function() { + it('sets and gets the value then triggers a "change" event', function() { + var handlerSpy = sinon.spy(); + var clayItem = fixture.clayItem(itemType); + clayItem.on('change', handlerSpy); + + clayItem.set(value); + assert.strictEqual(clayItem.get(), value); + assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once'); + assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem'); + }); + }); + } + + /** + * @param {string} itemType + * @return {void} + */ + function testDisable(itemType) { + describe('.disable()', function() { + it('disables the field then triggers a "disabled" event', function() { + var handlerSpy = sinon.spy(); + var clayItem = fixture.clayItem(itemType); + clayItem.on('disabled', handlerSpy); + + clayItem.disable(); + assert.strictEqual(clayItem.$manipulatorTarget.get('disabled'), true); + assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once'); + assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem'); + }); + }); + } + + /** + * @param {string} itemType + * @return {void} + */ + function testEnable(itemType) { + describe('.disable()', function() { + it('disables the field then triggers an "enabled" event', function() { + var handlerSpy = sinon.spy(); + var clayItem = fixture.clayItem(itemType); + clayItem.on('enabled', handlerSpy); + + clayItem.enable(); + assert.strictEqual(clayItem.$manipulatorTarget.get('disabled'), false); + assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once'); + assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem'); + }); + }); + } + + describe('html', function() { + testSetGet('footer', 'test123'); + }); + + describe('val', function() { + testSetGet('input', 'test321'); + testDisable('input'); + testEnable('input'); + }); + + describe('checked', function() { + testSetGet('toggle', true); + testSetGet('toggle', false); + testDisable('toggle'); + testEnable('toggle'); + }); +});