From 91b9c0bcf99c89f9e3193fb5c366a0e603779d39 Mon Sep 17 00:00:00 2001 From: Carlos Date: Fri, 13 Oct 2017 16:26:47 +0200 Subject: [PATCH] client side unit test for multi check until switchChecks() --- client/core/src/multi-check/multi-check.js | 3 +- .../core/src/multi-check/multi-check.spec.js | 112 ++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 client/core/src/multi-check/multi-check.spec.js diff --git a/client/core/src/multi-check/multi-check.js b/client/core/src/multi-check/multi-check.js index 3447e9b46..010c66aed 100644 --- a/client/core/src/multi-check/multi-check.js +++ b/client/core/src/multi-check/multi-check.js @@ -38,7 +38,8 @@ export default class MultiCheck { let checked; if (this.type.id && this.type.id !== 'all' && this.type.id !== 'any') { if (this.type.id.length > 3 && this.type.id.substr(0, 3) === 'no-') { - checked = el[this.type.id.replace('no-', '')] == null; + let label = this.type.id.replace('no-', ''); + checked = el[label] == null; } else if (this.type.id.length > 6 && this.type.id.substr(0, 6) === 'equal-') { let label = this.type.id.replace('equal-', ''); checked = (el[label] && el[label] === this.type.name); diff --git a/client/core/src/multi-check/multi-check.spec.js b/client/core/src/multi-check/multi-check.spec.js new file mode 100644 index 000000000..7b81ef1e3 --- /dev/null +++ b/client/core/src/multi-check/multi-check.spec.js @@ -0,0 +1,112 @@ +import './multi-check.js'; + +describe('Component vnMultiCheck', () => { + let $componentController; + + beforeEach(() => { + angular.mock.module('client'); + }); + + beforeEach(angular.mock.inject(_$componentController_ => { + $componentController = _$componentController_; + })); + + describe('models()', () => { + it(`should set controller _models property with the argument received`, () => { + let controller = $componentController('vnMultiCheck', {}); + let argument = 'I am the model'; + controller.models = argument; + + expect(controller._models).toEqual(argument); + }); + }); + + describe('checkAll()', () => { + it(`should set controller _checkAll property with the argument received then call switchChecks()`, () => { + let controller = $componentController('vnMultiCheck', {}); + let argument = 'I am the model'; + spyOn(controller, 'switchChecks'); + controller.checkAll = argument; + + expect(controller._checkAll).toEqual(argument); + expect(controller.switchChecks).toHaveBeenCalledWith(); + }); + }); + + describe('switchChecks()', () => { + it(`should set checked property inside each existing elemenet when begings with no-`, () => { + let controller = $componentController('vnMultiCheck', {}); + controller.type = {id: 'no-name'}; + controller.models = [ + {name: 'name'}, + {name: null} + ]; + + expect(controller._models[0].checked).not.toBeDefined(); + expect(controller._models[1].checked).not.toBeDefined(); + controller._checkAll = 1; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeFalsy(); + expect(controller._models[1].checked).toBeTruthy(); + }); + + it(`should set checked property inside each existing elemenet when begings with equal-`, () => { + let controller = $componentController('vnMultiCheck', {}); + controller.type = {id: 'equal-name', name: 'name'}; + controller.models = [ + {name: null}, + {name: 'name'} + ]; + + expect(controller._models[0].checked).not.toBeDefined(); + expect(controller._models[1].checked).not.toBeDefined(); + controller._checkAll = 1; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeFalsy(); + expect(controller._models[1].checked).toBeTruthy(); + }); + + it(`should set checked property inside each existing elemenet when begings with anything but any, all, no- or equal-`, () => { + let controller = $componentController('vnMultiCheck', {}); + controller.type = {id: 'name'}; + controller.models = [ + {name: null}, + {name: 'name'} + ]; + + expect(controller._models[0].checked).not.toBeDefined(); + expect(controller._models[1].checked).not.toBeDefined(); + controller._checkAll = 1; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeFalsy(); + expect(controller._models[1].checked).toBeTruthy(); + }); + + describe('when id is all or any', () => { + it('should set element checked property based on controller._checkAll', () => { + let controller = $componentController('vnMultiCheck', {}); + controller.type = {id: 'any'}; + controller.models = [ + {name: 'name'} + ]; + + expect(controller._models[0].checked).not.toBeDefined(); + controller._checkAll = 1; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeTruthy(); + controller._checkAll = 0; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeFalsy(); + controller._checkAll = 2; + controller.switchChecks(); + + expect(controller._models[0].checked).toBeFalsy(); + }); + }); + }); +});