UNPKG

@darkobits/formation

Version:
371 lines (291 loc) 14.6 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _unity = require('@darkobits/unity'); var _interfaces = require('../../etc/interfaces'); var _index = require('../../index'); var _index2 = _interopRequireDefault(_index); var _FormationControl = require('../../classes/FormationControl'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } describe('FormGroupController', function () { var T = void 0; var logSpy = jest.fn(); beforeEach(function () { (0, _unity.module)(_index2.default, { mock: { $log: { log: logSpy } } }); T = (0, _unity.directive)('fmGroup', { template: '<fm-group></fm-group>', wrap: '<fm></fm>' }); }); // ----- Interfaces ---------------------------------------------------------- describe('[Interface] RegisterForm', function () { it('should implement the RegisterForm interface', function () { expect(_typeof(T.fmGroup[_interfaces.RegisterForm])).toEqual('function'); }); describe('when another child form with the same name is present', function () { beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group name="formGroup">\n <fm name="childForm"></fm>\n </fm>\n ', wrap: '<fm></fm>' }); }); it('should throw an error', function () { var childForm = (0, _unity.compile)({ template: '<fm name="childForm"></fm>', scope: T.$scope.$new() }); var childFormCtrl = childForm.controller('fm'); expect(function () { T.fmGroup[_interfaces.RegisterForm](childFormCtrl); }).toThrow('another child form with this name already exists'); }); }); describe('configuring the child form', function () { var configKey = 'config'; var childFormName = 'childForm'; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group name="formGroup" controls="' + configKey + '">\n <transclude></transclude>\n </fm-group>\n ', scope: _defineProperty({}, configKey, _defineProperty({}, childFormName, { foo: 'bar' })), wrap: '<fm></fm>' }); }); it('should pass configuration data to the child form', function () { // Note: Add tests for this. }); }); }); describe('[Interface] Configure', function () { var formGroupName = 'formGroup'; var childFormA = 'foo'; var childFormB = 'bar'; var controlA = 'controlA'; var controlB = 'controlB'; var errors = [['foo', 'bar']]; var config = [_defineProperty({}, controlA, { errors: errors }), _defineProperty({}, controlB, { errors: errors })]; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group name="' + formGroupName + '">\n <fm name="' + childFormA + '">\n <fm-input name="' + controlA + '"></fm-input>\n </fm>\n <transclude></transclude>\n </fm-group>\n ', wrap: '<fm></fm>' }); T.fmGroup[_interfaces.Configure](config); }); it('should implement the Configure interface', function () { expect(_typeof(T.fmGroup[_interfaces.Configure])).toEqual('function'); }); it('should throw an error when not passed an array', function () { expect(function () { T.fmGroup[_interfaces.Configure]('foo'); }).toThrow('expected configuration to be of type Array or Undefined'); }); it('should configure known entities', function () { expect(T.fmGroup.getForm(childFormA).getControl(controlA).getErrorMessages()).toEqual(errors); }); it('should cache configuration data and pass it to newly-registered forms', function () { (0, _unity.compile)({ template: '\n <fm name="' + childFormB + '">\n <fm-input name="' + controlB + '""></fm-input>\n </fm>\n ', insertAt: T.$element.find('transclude') }); expect(T.fmGroup.getForm(childFormB).getControl(controlB).getErrorMessages()).toEqual(errors); }); }); describe('[Interface] GetModelValue / SetModelValue', function () { var spies = {}; var controlA = 'controlA'; var controlB = 'controlB'; var modelValues = [_defineProperty({}, controlA, 'bar'), _defineProperty({}, controlB, 'bar')]; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group name="group">\n <fm name="grouped1">\n <fm-input name="' + controlA + '"></fm-input>\n </fm>\n <fm name="grouped2">\n <fm-input name="' + controlB + '"></fm-input>\n </fm>\n </fm-group>\n ', wrap: '<fm></fm>' }); (0, _unity.digest)(); Object.assign(spies, { group: { getModelValue: jest.spyOn(T.fmGroup, _interfaces.GetModelValue), setModelValue: jest.spyOn(T.fmGroup, _interfaces.SetModelValue), grouped1: _defineProperty({ getModelValue: jest.spyOn(T.fmGroup.getForm('grouped1'), _interfaces.GetModelValue), setModelValue: jest.spyOn(T.fmGroup.getForm('grouped1'), _interfaces.SetModelValue) }, controlA, { getModelValue: jest.spyOn(T.fmGroup.getForm('grouped1').getControl(controlA), _interfaces.GetModelValue), setModelValue: jest.spyOn(T.fmGroup.getForm('grouped1').getControl(controlA), _interfaces.SetModelValue) }), grouped2: _defineProperty({ getModelValue: jest.spyOn(T.fmGroup.getForm('grouped2'), _interfaces.GetModelValue), setModelValue: jest.spyOn(T.fmGroup.getForm('grouped2'), _interfaces.SetModelValue) }, controlB, { getModelValue: jest.spyOn(T.fmGroup.getForm('grouped2').getControl(controlB), _interfaces.GetModelValue), setModelValue: jest.spyOn(T.fmGroup.getForm('grouped2').getControl(controlB), _interfaces.SetModelValue) }) } }); }); it('should implement the SetModelValue interface', function () { expect(_typeof(T.fmGroup[_interfaces.SetModelValue])).toEqual('function'); }); it('should delegate to the SetModelValue interface of registry members', function () { T.fmGroup[_interfaces.SetModelValue](modelValues); (0, _unity.digest)(); expect(spies.group.setModelValue.mock.calls.length).toEqual(1); expect(spies.group.setModelValue.mock.calls[0]).toEqual([modelValues]); expect(spies.group.grouped1.setModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped1.setModelValue.mock.calls[0]).toEqual([modelValues[0]]); expect(spies.group.grouped1[controlA].setModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped1[controlA].setModelValue.mock.calls[0]).toEqual([modelValues[0][controlA]]); expect(spies.group.grouped2.setModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped2.setModelValue.mock.calls[0]).toEqual([modelValues[1]]); expect(spies.group.grouped2[controlB].setModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped2[controlB].setModelValue.mock.calls[0]).toEqual([modelValues[1][controlB]]); }); it('should implement the GetModelValue interface', function () { expect(_typeof(T.fmGroup[_interfaces.GetModelValue])).toEqual('function'); }); it('should delegate to the GetModelValue interface of registry members', function () { T.fmGroup.setModelValues(modelValues); (0, _unity.digest)(); var result = T.fmGroup[_interfaces.GetModelValue](); expect(result).toEqual(modelValues); // Forms and form groups should have had GetModelValues invoked once. expect(spies.group.getModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped1.getModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped2.getModelValue.mock.calls.length).toEqual(1); // Child controls will have had GetModelValue invoked 3 times. expect(spies.group.grouped1[controlA].getModelValue.mock.calls.length).toEqual(1); expect(spies.group.grouped2[controlB].getModelValue.mock.calls.length).toEqual(1); }); it('SetModelValue should throw an error if not passed an array', function () { expect(function () { T.fmGroup[_interfaces.SetModelValue]('foo'); }).toThrow('expected model values to be of type Array or Undefined'); }); }); describe('[Interface] SetCustomErrorMessage', function () { var formName = 'foo'; var controlName = 'control'; var errorMessage = 'bar'; var errorData = [_defineProperty({}, controlName, errorMessage)]; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group>\n <fm name="' + formName + '">\n <fm-input name="' + controlName + '"></fm-input>\n </fm>\n </fm-group>\n ', wrap: '<fm></fm>' }); }); it('should implement the SetCustomErrorMessage interface', function () { expect(_typeof(T.fmGroup[_interfaces.SetCustomErrorMessage])).toEqual('function'); }); it('should delegate to the SetCustomErrorMessage interface of known registry members', function () { expect(T.fmGroup.getForm(formName).getControl(controlName)[_FormationControl.CUSTOM_ERROR_MESSAGE_KEY]).toBeFalsy(); T.fmGroup[_interfaces.SetCustomErrorMessage](errorData); expect(T.fmGroup.getForm(formName).getControl(controlName)[_FormationControl.CUSTOM_ERROR_MESSAGE_KEY]).toEqual(errorMessage); }); it('should throw an error if not passed an array', function () { expect(function () { T.fmGroup[_interfaces.SetCustomErrorMessage]('foo'); }).toThrow('expected error messages to be of type Array or Undefined'); }); }); describe('[Interface] ClearCustomErrorMessage', function () { var formName = 'foo'; var controlName = 'control'; var errorMessage = 'bar'; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group>\n <fm name="' + formName + '">\n <fm-input name="' + controlName + '"></fm-input>\n </fm>\n </fm-group>\n ', wrap: '<fm></fm>' }); T.fmGroup.getForm(formName).getControl(controlName)[_interfaces.SetCustomErrorMessage](errorMessage); }); it('should implement the ClearCustomErrorMessage interface', function () { expect(_typeof(T.fmGroup[_interfaces.ClearCustomErrorMessage])).toEqual('function'); }); it('should clear custom error messages from registry members', function () { T.fmGroup[_interfaces.ClearCustomErrorMessage](); expect(T.fmGroup.getForm(formName).getControl(controlName)[_FormationControl.CUSTOM_ERROR_MESSAGE_KEY]).toBeFalsy(); }); }); describe('[Interface] Reset', function () { var formName = 'foo'; var controlName = 'control'; var resetSpy = void 0; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '\n <fm-group>\n <fm name="' + formName + '">\n <fm-input name="' + controlName + '"></fm-input>\n </fm>\n </fm-group>\n ', wrap: '<fm></fm>' }); resetSpy = jest.spyOn(T.fmGroup.getForm(formName).getControl(controlName), _interfaces.Reset); }); it('should implement the Reset interface', function () { expect(_typeof(T.fmGroup[_interfaces.Reset])).toEqual('function'); }); it('should delegate model values to known controls', function () { var value = 'bar'; var modelValues = [_defineProperty({}, controlName, value)]; T.fmGroup[_interfaces.Reset](modelValues); (0, _unity.digest)(); expect(T.fmGroup.getForm(formName).getControl(controlName)[_interfaces.GetModelValue]()).toEqual(value); expect(resetSpy.mock.calls[0]).toEqual([value]); }); it('should throw an error if passed a truthy value that is not an array', function () { expect(function () { T.fmGroup[_interfaces.Reset](); }).not.toThrow(); expect(function () { T.fmGroup[_interfaces.Reset]('foo'); }).toThrow('expected model values to be of type Array or Undefined'); }); }); // ----- Semi-Private Methods ------------------------------------------------ describe('$debug', function () { var message = 'foo'; beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '<fm-group debug></fm-group>', wrap: '<fm></fm>' }); }); it('should log debug messages when "$debugging" is true', function () { T.fmGroup.$debug(message); expect(logSpy.mock.calls[0]).toEqual(expect.arrayContaining([message])); }); }); // ----- Public Methods ------------------------------------------------------ describe('isDisabled', function () { describe('when "$disabled" is truthy', function () { beforeEach(function () { T.fmGroup.disable(); }); it('should return true', function () { expect(T.fmGroup.isDisabled()).toBe(true); }); }); describe('when "$ngDisabled" is truthy', function () { beforeEach(function () { T = (0, _unity.directive)('fmGroup', { template: '<fm-group ng-disabled="true"></fm-group>', wrap: '<fm></fm>' }); }); it('should return true', function () { expect(T.fmGroup.isDisabled()).toBe(true); }); }); describe('when neither "$disabled" nor "$ngDisabled" are truthy', function () { it('should return falsy', function () { expect(T.fmGroup.isDisabled()).toBeFalsy(); }); }); }); });