'use strict'; /* globals generateInputCompilerHelper: false */ describe('ngList', function() { var helper = {}, $rootScope; generateInputCompilerHelper(helper); beforeEach(inject(function(_$rootScope_) { $rootScope = _$rootScope_; })); it('should parse text into an array', function() { var inputElm = helper.compileInput(''); // model -> view $rootScope.$apply('list = [\'x\', \'y\', \'z\']'); expect(inputElm.val()).toBe('x, y, z'); // view -> model helper.changeInputValueTo('1, 2, 3'); expect($rootScope.list).toEqual(['1', '2', '3']); }); it('should not clobber text if model changes due to itself', function() { // When the user types 'a,b' the 'a,' stage parses to ['a'] but if the // $parseModel function runs it will change to 'a', in essence preventing // the user from ever typing ','. var inputElm = helper.compileInput(''); helper.changeInputValueTo('a '); expect(inputElm.val()).toEqual('a '); expect($rootScope.list).toEqual(['a']); helper.changeInputValueTo('a ,'); expect(inputElm.val()).toEqual('a ,'); expect($rootScope.list).toEqual(['a']); helper.changeInputValueTo('a , '); expect(inputElm.val()).toEqual('a , '); expect($rootScope.list).toEqual(['a']); helper.changeInputValueTo('a , b'); expect(inputElm.val()).toEqual('a , b'); expect($rootScope.list).toEqual(['a', 'b']); }); it('should convert empty string to an empty array', function() { helper.compileInput(''); helper.changeInputValueTo(''); expect($rootScope.list).toEqual([]); }); it('should be invalid if required and empty', function() { var inputElm = helper.compileInput(''); helper.changeInputValueTo(''); expect($rootScope.list).toBeUndefined(); expect(inputElm).toBeInvalid(); helper.changeInputValueTo('a,b'); expect($rootScope.list).toEqual(['a','b']); expect(inputElm).toBeValid(); }); describe('with a custom separator', function() { it('should split on the custom separator', function() { helper.compileInput(''); helper.changeInputValueTo('a,a'); expect($rootScope.list).toEqual(['a,a']); helper.changeInputValueTo('a:b'); expect($rootScope.list).toEqual(['a', 'b']); }); it('should join the list back together with the custom separator', function() { var inputElm = helper.compileInput(''); $rootScope.$apply(function() { $rootScope.list = ['x', 'y', 'z']; }); expect(inputElm.val()).toBe('x : y : z'); }); }); describe('(with ngTrim undefined or true)', function() { it('should ignore separator whitespace when splitting', function() { helper.compileInput(''); helper.changeInputValueTo('a|b'); expect($rootScope.list).toEqual(['a', 'b']); }); it('should trim whitespace from each list item', function() { helper.compileInput(''); helper.changeInputValueTo('a | b'); expect($rootScope.list).toEqual(['a', 'b']); }); }); describe('(with ngTrim set to false)', function() { it('should use separator whitespace when splitting', function() { helper.compileInput(''); helper.changeInputValueTo('a|b'); expect($rootScope.list).toEqual(['a|b']); helper.changeInputValueTo('a | b'); expect($rootScope.list).toEqual(['a','b']); }); it('should not trim whitespace from each list item', function() { helper.compileInput(''); helper.changeInputValueTo('a | b'); expect($rootScope.list).toEqual(['a ',' b']); }); it('should support splitting on newlines', function() { helper.compileInput(''); helper.changeInputValueTo('a\nb'); expect($rootScope.list).toEqual(['a','b']); }); it('should support splitting on whitespace', function() { helper.compileInput(''); helper.changeInputValueTo('a b'); expect($rootScope.list).toEqual(['a','b']); }); }); });