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

Commit 8deff89

Browse files
committed
test(ngOptions): ensure options are only painted once on compile
1 parent 6572838 commit 8deff89

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

test/ng/directive/ngOptionsSpec.js

+60-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe('ngOptions', function() {
44

5-
var scope, formElement, element, $compile, linkLog, ngModelCtrl;
5+
var scope, formElement, element, $compile, linkLog, childListMutationObserver, ngModelCtrl;
66

77
function compile(html) {
88
formElement = jqLite('<form name="form">' + html + '</form>');
@@ -151,7 +151,18 @@ describe('ngOptions', function() {
151151
$compile(element.contents())(scope);
152152
}
153153
};
154-
});
154+
})
155+
156+
.directive('observeChildList', function() {
157+
return {
158+
link: function(scope, element) {
159+
var config = { childList: true };
160+
161+
childListMutationObserver = new window.MutationObserver(noop);
162+
childListMutationObserver.observe(element[0], config);
163+
}
164+
};
165+
});
155166

156167
$provide.decorator('ngOptionsDirective', function($delegate) {
157168

@@ -801,6 +812,29 @@ describe('ngOptions', function() {
801812
expect(options[2]).toBeMarkedAsSelected();
802813
});
803814

815+
816+
if (window.MutationObserver) {
817+
//IE9 and IE10 do not support MutationObserver
818+
//Since the feature is only needed for a test, it's okay to skip these browsers
819+
it('should render the initial options only one time', function() {
820+
scope.value = 'black';
821+
scope.values = ['black', 'white', 'red'];
822+
// observe-child-list adds a MutationObserver that we will read out after ngOptions
823+
// has been compiled
824+
createSelect({
825+
'ng-model':'value',
826+
'ng-options':'value.name for value in values',
827+
'observe-child-list': ''
828+
});
829+
830+
var optionEls = element[0].querySelectorAll('option');
831+
var records = childListMutationObserver.takeRecords();
832+
833+
expect(records.length).toBe(1);
834+
expect(records[0].addedNodes).toEqual(optionEls);
835+
});
836+
}
837+
804838
describe('disableWhen expression', function() {
805839

806840
describe('on single select', function() {
@@ -2966,6 +3000,30 @@ describe('ngOptions', function() {
29663000
optionsSetSelected[0].calls.reset();
29673001
optionsSetSelected[1].calls.reset();
29683002
});
3003+
3004+
if (window.MutationObserver) {
3005+
//IE9 and IE10 do not support MutationObserver
3006+
//Since the feature is only needed for a test, it's okay to skip these browsers
3007+
it('should render the initial options only one time', function() {
3008+
scope.value = ['black'];
3009+
scope.values = ['black', 'white', 'red'];
3010+
// observe-child-list adds a MutationObserver that we will read out after ngOptions
3011+
// has been compiled
3012+
createSelect({
3013+
'ng-model':'selected',
3014+
'ng-options':'value.name for value in values',
3015+
'multiple': 'true',
3016+
'observe-child-list': ''
3017+
});
3018+
3019+
var optionEls = element[0].querySelectorAll('option');
3020+
var records = childListMutationObserver.takeRecords();
3021+
3022+
expect(records.length).toBe(1);
3023+
expect(records[0].addedNodes).toEqual(optionEls);
3024+
});
3025+
}
3026+
29693027
});
29703028

29713029

0 commit comments

Comments
 (0)