From c65c34ebfe0f70c83a45f283654c8558802752cf Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Tue, 17 Apr 2012 15:12:09 -0700 Subject: [PATCH] test(selectSpec): clean up and simplify specs --- test/ng/directive/selectSpec.js | 80 ++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 611f30c96d7d..5a9d017819e8 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -10,12 +10,34 @@ describe('select', function() { scope.$apply(); } + beforeEach(inject(function($injector, $rootScope) { scope = $rootScope; $compile = $injector.get('$compile'); formElement = element = null; })); + + beforeEach(function() { + this.addMatchers({ + toEqualSelect: function(expected){ + var actualValues = [], + expectedValues = [].slice.call(arguments); + + forEach(this.actual.find('option'), function(option) { + actualValues.push(option.selected ? [option.value] : option.value); + }); + + this.message = function() { + return 'Expected ' + toJson(actualValues) + ' to equal ' + toJson(expectedValues) + '.'; + }; + + return equals(expectedValues, actualValues); + } + }); + }); + + afterEach(function() { dealoc(formElement); }); @@ -102,15 +124,13 @@ describe('select', function() { scope.selection = ['A']; }); - expect(element.find('option')[0].selected).toEqual(true); - expect(element.find('option')[1].selected).toEqual(false); + expect(element).toEqualSelect(['A'], 'B'); scope.$apply(function() { scope.selection.push('B'); }); - expect(element.find('option')[0].selected).toEqual(true); - expect(element.find('option')[1].selected).toEqual(true); + expect(element).toEqualSelect(['A'], ['B']); }); @@ -817,47 +837,27 @@ describe('select', function() { describe('OPTION value', function() { - beforeEach(function() { - this.addMatchers({ - toHaveValue: function(expected){ - this.message = function() { - return 'Expected "' + this.actual.html() + '" to have value="' + expected + '".'; - }; - - var value; - htmlParser(this.actual.html(), { - start:function(tag, attrs){ - value = attrs.value; - }, - end:noop, - chars:noop - }); - return trim(value) == trim(expected); - } - }); - }); - - it('should populate value attribute on OPTION', inject(function($rootScope, $compile) { - element = $compile('')($rootScope) - expect(element).toHaveValue('abc'); - })); + it('should populate value attribute on OPTION', function() { + compile(''); + expect(element).toEqualSelect('abc'); + }); - it('should ignore value if already exists', inject(function($rootScope, $compile) { - element = $compile('')($rootScope) - expect(element).toHaveValue('abc'); - })); + it('should ignore value if already exists', function() { + compile(''); + expect(element).toEqualSelect('abc'); + }); - it('should set value even if newlines present', inject(function($rootScope, $compile) { - element = $compile('')($rootScope) - expect(element).toHaveValue('\nabc\n'); - })); + it('should set value even if newlines present', function() { + compile(''); + expect(element).toEqualSelect('\nabc\n'); + }); - it('should set value even if self closing HTML', inject(function($rootScope, $compile) { + it('should set value even if self closing HTML', function() { // IE removes the \n from option, which makes this test pointless if (msie) return; - element = $compile('')($rootScope) - expect(element).toHaveValue('\n'); - })); + compile(''); + expect(element).toEqualSelect('\n'); + }); }); });