Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit fec4f6e

Browse files
author
Kent C. Dodds
committed
Refactoring some tests slightly and implementing a deprecation for #390
1 parent 2cb7d2e commit fec4f6e

9 files changed

+131
-80
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 6.22.1
2+
3+
## Deprecations
4+
5+
- You will now get a deprecation warning if you specify the `skipNgModelAttrsManipulator` property under the `data` property (it has moved to `extras`). [#390](/../../issues/390)
6+
17
# 6.22.0
28

39
## New Features

other/ERRORS_AND_WARNINGS.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ the apiCheck checkers are never even created. Not much we can do about the coupl
142142
a big issue. For more info, see [#334](https://github.com/formly-js/angular-formly/issues/334). Note, this will be
143143
removed in a major release.
144144

145+
# skipNgModelAttrsManipulator moved
146+
147+
This property has been moved from the `data` property to the `extras` property.
148+
149+
Before:
150+
151+
```javascript
152+
{
153+
template: '<hr />',
154+
data: {
155+
skipNgModelAttrsManipulator: true
156+
}
157+
}
158+
```
159+
160+
After:
161+
162+
```javascript
163+
{
164+
template: '<hr />',
165+
extras: {
166+
skipNgModelAttrsManipulator: true
167+
}
168+
}
169+
```
170+
145171
# Notes
146172

147173
It is recommended to disable warnings in production using `formlyConfigProvider.disableWarnings = true`. Note: This will

src/directives/formly-custom-validation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ function formlyCustomValidation(formlyConfig, formlyUtil, $q, formlyWarn) {
4444
const isPossiblyAsync = !angular.isString(validator);
4545
let validatorCollection = (isPossiblyAsync || isAsync) ? '$asyncValidators' : '$validators';
4646

47+
// UPDATE IN 7.0.0
4748
// this is temporary until we can have a breaking change. Allow people to get the wins of the explicitAsync api
4849
if (formlyConfig.extras.explicitAsync && !isAsync) {
4950
validatorCollection = '$validators';
5051
}
5152

5253
ctrl[validatorCollection][name] = function evalValidity(modelValue, viewValue) {
5354
const value = formlyUtil.formlyEval(scope, validator, modelValue, viewValue);
55+
// UPDATE IN 7.0.0
5456
// In the next breaking change, this code should simply return the value
5557
if (isAsync) {
5658
return value;
@@ -71,6 +73,7 @@ function formlyCustomValidation(formlyConfig, formlyUtil, $q, formlyWarn) {
7173
let inFlightValidator;
7274
ctrl.$parsers.unshift(function evalValidityOfParser(viewValue) {
7375
const isValid = formlyUtil.formlyEval(scope, validator, ctrl.$modelValue, viewValue);
76+
// UPDATE IN 7.0.0
7477
// In the next breaking change, rather than checking for isPromiseLike, it should just check for isAsync.
7578

7679
if (isAsync || isPromiseLike(isValid)) {

src/directives/formly-custom-validation.test.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import _ from 'lodash';
33
import angular from 'angular-fix';
44

5+
import testUtils from '../test.utils.js';
6+
7+
const {shouldWarnWithLog} = testUtils;
8+
59
describe(`formly-custom-validation`, function() {
610
let $compile, $timeout, $q, scope, $log, formlyConfig;
711
const formTemplate = `<form name="myForm">TEMPLATE</form>`;
@@ -104,7 +108,7 @@ describe(`formly-custom-validation`, function() {
104108
scope.options,
105109
/validators-returning-promises-should-use-asyncvalidators/
106110
];
107-
shouldWarn(logArgs, () => {
111+
shouldWarnWithLog($log, logArgs, () => {
108112
validate(() => $q.when(), true);
109113
});
110114
});
@@ -164,18 +168,4 @@ describe(`formly-custom-validation`, function() {
164168
scope.$digest();
165169
expect(field.$valid).to.eq(pass);
166170
}
167-
168-
function shouldWarn(logArgs, test) {
169-
/* eslint no-console:0 */
170-
test();
171-
expect($log.warn.logs, '$log should have only been called once').to.have.length(1);
172-
const log = $log.warn.logs[0];
173-
_.each(logArgs, (arg, index) => {
174-
if (_.isRegExp(arg)) {
175-
expect(log[index]).to.match(arg);
176-
} else {
177-
expect(log[index]).to.equal(arg);
178-
}
179-
});
180-
}
181171
});

src/directives/formly-field.test.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/* eslint no-shadow:0 */
22
/* eslint max-statements:[2, 50] */
33
/* eslint max-len:0 */
4-
/* eslint no-console:0 */
54
import angular from 'angular-fix';
65
import apiCheck from 'api-check';
76
import testUtils from '../test.utils.js';
87
import _ from 'lodash';
98

10-
const {getNewField, input, basicForm, multiNgModelField} = testUtils;
9+
const {getNewField, input, basicForm, multiNgModelField, shouldWarn, shouldNotWarn} = testUtils;
1110

1211
describe('formly-field', function() {
1312
/* jshint maxstatements:100 */
@@ -1678,31 +1677,4 @@ describe('formly-field', function() {
16781677
function getNgModelCtrl(index = 0) {
16791678
return angular.element(getFieldNgModelNode(index)).controller('ngModel');
16801679
}
1681-
1682-
function shouldWarn(match, test) {
1683-
const originalWarn = console.warn;
1684-
let calledArgs;
1685-
console.warn = function() {
1686-
calledArgs = arguments;
1687-
};
1688-
test();
1689-
expect(calledArgs, 'expected warning and there was none').to.exist;
1690-
expect(Array.prototype.join.call(calledArgs, ' ')).to.match(match);
1691-
console.warn = originalWarn;
1692-
}
1693-
1694-
1695-
function shouldNotWarn(test) {
1696-
const originalWarn = console.warn;
1697-
let calledArgs;
1698-
console.warn = function() {
1699-
calledArgs = arguments;
1700-
};
1701-
test();
1702-
if (calledArgs) {
1703-
console.log(calledArgs);
1704-
throw new Error('Expected no warning, but there was one', calledArgs);
1705-
}
1706-
console.warn = originalWarn;
1707-
}
17081680
});

src/providers/formlyConfig.test.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import angular from 'angular-fix';
77
import testUtils from '../test.utils.js';
88

9-
const {getNewField, basicForm} = testUtils;
9+
const {getNewField, basicForm, shouldWarn, shouldNotWarn} = testUtils;
1010

1111
describe('formlyConfig', () => {
1212
beforeEach(window.module('formly'));
@@ -766,25 +766,4 @@ describe('formlyConfig', () => {
766766
]);
767767
});
768768
});
769-
770-
function shouldWarn(match, test) {
771-
const originalWarn = console.warn;
772-
let calledArgs;
773-
console.warn = function() {
774-
calledArgs = arguments;
775-
};
776-
test();
777-
expect(calledArgs, 'expected warning and there was none').to.exist;
778-
expect(Array.prototype.join.call(calledArgs, ' ')).to.match(match);
779-
console.warn = originalWarn;
780-
}
781-
782-
function shouldNotWarn(test) {
783-
const originalWarn = console.warn;
784-
let callCount = 0;
785-
console.warn = () => callCount++;
786-
test();
787-
expect(callCount).to.equal(0);
788-
console.warn = originalWarn;
789-
}
790769
});

src/run/formlyNgModelAttrsManipulator.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import angular from 'angular-fix';
33
export default addFormlyNgModelAttrsManipulator;
44

55
// @ngInject
6-
function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate) {
6+
function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn) {
77
if (formlyConfig.extras.disableNgModelAttrsManipulator) {
88
return;
99
}
@@ -12,14 +12,13 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate) {
1212

1313
function ngModelAttrsManipulator(template, options, scope) {
1414
const node = document.createElement('div');
15-
const data = options.data;
16-
if (data.skipNgModelAttrsManipulator === true) {
15+
const skip = getSkip(options);
16+
if (skip === true) {
1717
return template;
1818
}
19-
2019
node.innerHTML = template;
2120

22-
const modelNodes = getNgModelNodes(node, data.skipNgModelAttrsManipulator);
21+
const modelNodes = getNgModelNodes(node, skip);
2322
if (!modelNodes || !modelNodes.length) {
2423
return template;
2524
}
@@ -128,6 +127,22 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate) {
128127
return node.querySelectorAll(query);
129128
}
130129

130+
function getSkip(options) {
131+
// UPDATE IN 7.0.0
132+
let skip = options.extras && options.extras.skipNgModelAttrsManipulator;
133+
if (!angular.isDefined(skip)) {
134+
skip = options.data && options.data.skipNgModelAttrsManipulator;
135+
if (angular.isDefined(skip)) {
136+
formlyWarn(
137+
'skipngmodelattrsmanipulator-moved',
138+
'The skipNgModelAttrsManipulator property has been moved from the `data` property to the `extras` property',
139+
options
140+
);
141+
}
142+
}
143+
return skip;
144+
}
145+
131146
function getBuiltInAttributes() {
132147
const ngModelAttributes = {
133148
focus: {

src/run/formlyNgModelAttrsManipulator.test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,53 @@
22
import angular from 'angular';
33
import _ from 'lodash';
44

5+
import testUtils from '../test.utils.js';
6+
7+
const {shouldWarnWithLog} = testUtils;
8+
59
describe('formlyNgModelAttrsManipulator', () => {
610
beforeEach(window.module('formly'));
711

8-
let formlyConfig, manipulator, scope, field, result, resultEl, resultNode;
12+
let formlyConfig, manipulator, scope, field, result, resultEl, resultNode, $log;
913
const template = '<input ng-model="model[options.key]" />';
1014

11-
beforeEach(inject((_formlyConfig_, $rootScope) => {
15+
beforeEach(inject((_formlyConfig_, $rootScope, _$log_) => {
16+
$log = _$log_;
1217
formlyConfig = _formlyConfig_;
1318
manipulator = formlyConfig.templateManipulators.preWrapper[0];
1419
scope = $rootScope.$new();
1520
scope.id = 'id';
1621
field = {
22+
extras: {},
1723
data: {},
1824
validation: {},
1925
templateOptions: {}
2026
};
2127
}));
2228

2329
describe(`skipping`, () => {
24-
it(`should allow you to skip the manipulator wholesale for the field`, () => {
30+
it(`should give a deprecation warning when using the data property`, () => {
2531
field.data.skipNgModelAttrsManipulator = true;
32+
const logArgs = [
33+
'Formly Warning:',
34+
'The skipNgModelAttrsManipulator property has been moved from the `data` property to the `extras` property',
35+
field,
36+
/skipngmodelattrsmanipulator-moved/
37+
];
38+
shouldWarnWithLog($log, logArgs, manipulate);
39+
});
40+
41+
42+
it(`should allow you to skip the manipulator wholesale for the field`, () => {
43+
field.extras.skipNgModelAttrsManipulator = true;
2644
manipulate();
2745
expect(result).to.equal(template);
2846
});
2947

3048
it(`should allow you to specify a selector for specific elements to skip`, () => {
3149
const className = 'ignored-thing' + _.random(0, 10);
3250
field.templateOptions.required = true;
33-
field.data.skipNgModelAttrsManipulator = `.${className}`;
51+
field.extras.skipNgModelAttrsManipulator = `.${className}`;
3452
manipulate(`
3553
<div>
3654
<input class="first-thing" ng-model="model[options.key]" />
@@ -60,7 +78,7 @@ describe('formlyNgModelAttrsManipulator', () => {
6078

6179
it(`should not skip by selector if skipNgModelAttrsManipulator is a boolean value`, () => {
6280
field.templateOptions.required = true;
63-
field.data.skipNgModelAttrsManipulator = false;
81+
field.extras.skipNgModelAttrsManipulator = false;
6482
manipulate(`
6583
<div>
6684
<input class="first-thing" ng-model="model[options.key]" />
@@ -76,7 +94,7 @@ describe('formlyNgModelAttrsManipulator', () => {
7694
it(`should allow you to skip using both the special attribute and the custom selector`, () => {
7795
const className = 'ignored-thing' + _.random(0, 10);
7896
field.templateOptions.required = true;
79-
field.data.skipNgModelAttrsManipulator = `.${className}`;
97+
field.extras.skipNgModelAttrsManipulator = `.${className}`;
8098
manipulate(`
8199
<div>
82100
<input class="first-thing" ng-model="model[options.key]" />

src/test.utils.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {merge} from 'lodash';
1+
/* eslint no-console:0 */
2+
import _ from 'lodash';
23

34
let key = 0;
45

@@ -10,9 +11,50 @@ const multiNgModelField = `
1011
const basicForm = '<formly-form form="theForm" model="model" fields="fields" options="options"></formly-form>';
1112

1213
export default {
13-
getNewField, input, multiNgModelField, basicForm
14+
getNewField, input, multiNgModelField, basicForm, shouldWarn, shouldNotWarn, shouldWarnWithLog
1415
};
1516

1617
function getNewField(options) {
17-
return merge({template: input, key: key++}, options);
18+
return _.merge({template: input, key: key++}, options);
19+
}
20+
21+
function shouldWarn(match, test) {
22+
const originalWarn = console.warn;
23+
let calledArgs;
24+
console.warn = function() {
25+
calledArgs = arguments;
26+
};
27+
test();
28+
expect(calledArgs, 'expected warning and there was none').to.exist;
29+
expect(Array.prototype.join.call(calledArgs, ' ')).to.match(match);
30+
console.warn = originalWarn;
31+
}
32+
33+
34+
function shouldNotWarn(test) {
35+
const originalWarn = console.warn;
36+
let calledArgs;
37+
console.warn = function() {
38+
calledArgs = arguments;
39+
};
40+
test();
41+
if (calledArgs) {
42+
console.log(calledArgs);
43+
throw new Error('Expected no warning, but there was one', calledArgs);
44+
}
45+
console.warn = originalWarn;
46+
}
47+
48+
function shouldWarnWithLog($log, logArgs, test) {
49+
/* eslint no-console:0 */
50+
test();
51+
expect($log.warn.logs, '$log should have only been called once').to.have.length(1);
52+
const log = $log.warn.logs[0];
53+
_.each(logArgs, (arg, index) => {
54+
if (_.isRegExp(arg)) {
55+
expect(log[index]).to.match(arg);
56+
} else {
57+
expect(log[index]).to.equal(arg);
58+
}
59+
});
1860
}

0 commit comments

Comments
 (0)