UNPKG

@darkobits/formation

Version:
185 lines (146 loc) 6.82 kB
'use strict'; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _unity = require('@darkobits/unity'); var _index = require('../../index'); var _index2 = _interopRequireDefault(_index); var _constants = require('../../../src/etc/constants'); var _Form = require('../../../src/components/Form/Form'); var _config = require('../../../src/etc/config'); 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; } /** * Duck typing to determine if a value is an instance of ngForm. */ function assertIsNgFormController(value) { expect(Object.keys(value)).toEqual(expect.arrayContaining(['$$parentForm', '$submitted'])); } /** * Duck typing to determine if a value is an instance of ngModel. */ function assertIsNgModelController(value) { expect(Object.keys(value)).toEqual(expect.arrayContaining(['$parsers', '$formatters', '$validators', '$asyncValidators', '$options'])); } describe('Configurator', function () { describe('prior to Angular bootstrapping', function () { it('should require an object as its argument', function () { expect(function () { (0, _config.FormationConfigurator)({}); }).not.toThrow(); expect(function () { (0, _config.FormationConfigurator)('foo'); }).toThrow('FormationConfigurator expected options to be of type Object'); }); it('should require showErrorsOn to be a string or nil', function () { expect(function () { (0, _config.FormationConfigurator)({ showErrorsOn: 'foo' }); }).not.toThrow(); expect(function () { (0, _config.FormationConfigurator)({ showErrorsOn: [] }); }).toThrow('FormationConfigurator expected showErrorsOn to be of type String or Undefined'); }); it('should require prefix to be a string or nil', function () { expect(function () { (0, _config.FormationConfigurator)({ prefix: 'foo' }); }).not.toThrow(); expect(function () { (0, _config.FormationConfigurator)({ prefix: [] }); }).toThrow('FormationConfigurator expected prefix to be of type String or Undefined'); }); }); describe('after Angular bootstrapping', function () { beforeEach(function () { (0, _unity.module)(_index2.default); }); it('should throw an error', function () { expect(function () { (0, _config.FormationConfigurator)(); }).toThrow('Formation cannot be configured once Angular has bootstrapped'); }); }); }); describe('Decorators', function () { var T = void 0; var componentNames = ['foo', 'bar', 'baz']; beforeEach(function () { componentNames.forEach(function (componentName) { return (0, _config.$registerComponent)(componentName, {}); }); (0, _unity.module)(_index2.default); }); describe('Decorating ngForm', function () { it('should require the Formation form controller', function () { var _get = (0, _unity.get)('formDirective'), _get2 = _slicedToArray(_get, 1), ngFormDirective = _get2[0]; expect(ngFormDirective.require).toEqual(expect.arrayContaining(['?^^' + _constants.FORM_COMPONENT_NAME])); }); describe('registering with a Formation form', function () { beforeEach(function () { T = (0, _unity.directive)('fm', { template: '\n <fm></fm>\n ' }); (0, _unity.get)('$rootScope').$digest(); (0, _unity.get)('$rootScope').$digest(); (0, _unity.get)('$rootScope').$digest(); (0, _unity.get)('$rootScope').$digest(); }); it('should register with the Formation form, if present', function () { var ngFormController = T.fm[_Form.NG_FORM_CONTROLLER]; assertIsNgFormController(ngFormController); }); }); }); describe('Decorating ngModel', function () { it('should require registered Formation controls', function () { var _get3 = (0, _unity.get)('ngModelDirective'), _get4 = _slicedToArray(_get3, 1), ngModelDirective = _get4[0]; var registeredComponents = componentNames.map(function (componentName) { return '?^^' + componentName; }); expect(ngModelDirective.require).toEqual(expect.arrayContaining(registeredComponents)); }); it('should require Formation forms', function () { var _get5 = (0, _unity.get)('ngModelDirective'), _get6 = _slicedToArray(_get5, 1), ngModelDirective = _get6[0]; expect(ngModelDirective.require).toEqual(expect.arrayContaining(['?^^fm'])); }); describe('registering with a Formation form', function () { var controlName = 'foo'; var value = 'bar'; beforeEach(function () { T = (0, _unity.directive)('fm', { template: '\n <fm>\n <input name="' + controlName + '"\n type="text"\n ng-model="' + controlName + '"\n </fm>\n ', scope: _defineProperty({}, controlName, value) }); }); it('should register with the Formation form, if present', function () { var control = T.fm.getControl(controlName); assertIsNgModelController(control[_constants.NG_MODEL_CTRL]); expect(T.fm.getModelValues()).toEqual(expect.objectContaining(_defineProperty({}, controlName, value))); }); }); describe('registering with a Formation control', function () { var controlName = 'foo'; beforeEach(function () { T = (0, _unity.directive)('fmInput', { template: '\n <fm-input name="' + controlName + '"></fm-input>', wrap: '\n <fm></fm>\n ' }); }); it('should register with the Formation form, if present', function () { var control = T.fmInput; assertIsNgModelController(control[_constants.NG_MODEL_CTRL]); }); }); }); });