@@ -55,4 +55,58 @@ describe('md-input-container directive', function() {
5555 expect ( el . find ( 'input' ) . attr ( 'id' ) ) . toBeTruthy ( ) ;
5656 expect ( el . find ( 'label' ) . attr ( 'for' ) ) . toBe ( el . find ( 'input' ) . attr ( 'id' ) ) ;
5757 } ) ) ;
58+
59+ ddescribe ( 'md-maxlength' , function ( ) {
60+ function getCharCounter ( el ) {
61+ return angular . element ( el [ 0 ] . querySelector ( '.md-char-counter' ) ) ;
62+ }
63+
64+ it ( 'should work with a constant' , inject ( function ( $rootScope , $compile ) {
65+ var el = $compile ( '<form name="form">' +
66+ '<md-input-container>' +
67+ '<input md-maxlength="5" ng-model="foo" name="foo">' +
68+ '</md-input-container>' +
69+ '</form>' ) ( $rootScope ) ;
70+ $rootScope . $apply ( ) ;
71+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
72+ expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '0/5' ) ;
73+
74+ $rootScope . $apply ( 'foo = "abcde"' ) ;
75+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
76+ expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '5/5' ) ;
77+
78+ $rootScope . $apply ( 'foo = "abcdef"' ) ;
79+ el . find ( 'input' ) . triggerHandler ( 'input' ) ;
80+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBe ( true ) ;
81+ expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '6/5' ) ;
82+
83+ $rootScope . $apply ( 'foo = "abc"' ) ;
84+ el . find ( 'input' ) . triggerHandler ( 'input' ) ;
85+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
86+ expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '3/5' ) ;
87+ } ) ) ;
88+
89+ it ( 'should add and remove maxlength element & error with expression' , inject ( function ( $rootScope , $compile ) {
90+ var el = $compile ( '<form name="form">' +
91+ '<md-input-container>' +
92+ '<input md-maxlength="max" ng-model="foo" name="foo">' +
93+ '</md-input-container>' +
94+ '</form>' ) ( $rootScope ) ;
95+
96+ $rootScope . $apply ( ) ;
97+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
98+ expect ( getCharCounter ( el ) . length ) . toBe ( 0 ) ;
99+
100+ $rootScope . $apply ( 'max = 5' ) ;
101+ $rootScope . $apply ( 'foo = "abcdef"' ) ;
102+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeTruthy ( ) ;
103+ expect ( getCharCounter ( el ) . length ) . toBe ( 1 ) ;
104+ expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '6/5' ) ;
105+
106+ $rootScope . $apply ( 'max = -1' ) ;
107+ $rootScope . $apply ( 'foo = "abcdefg"' ) ;
108+ expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
109+ expect ( getCharCounter ( el ) . length ) . toBe ( 0 ) ;
110+ } ) ) ;
111+ } ) ;
58112} ) ;
0 commit comments