@@ -17,7 +17,6 @@ describe('md-calendar', function() {
1717 */
1818 function applyDateChange ( ) {
1919 $timeout . flush ( ) ;
20- pageScope . $apply ( ) ;
2120 $material . flushOutstandingAnimations ( ) ;
2221
2322 // Internally, the calendar sets scrollTop to scroll to the month for a change.
@@ -57,7 +56,7 @@ describe('md-calendar', function() {
5756 /**
5857 * Finds a month `tbody` in the calendar element given a date.
5958 */
60- function findMonthElement ( date ) {
59+ function findMonthElement ( element , date ) {
6160 var months = element . querySelectorAll ( '[md-calendar-month-body]' ) ;
6261 var monthHeader = dateLocale . monthHeaderFormatter ( date ) ;
6362 var month ;
@@ -72,8 +71,8 @@ describe('md-calendar', function() {
7271 }
7372
7473 /** Find the `tbody` for a year in the calendar. */
75- function findYearElement ( year ) {
76- var node = element [ 0 ] || element ;
74+ function findYearElement ( parent , year ) {
75+ var node = parent [ 0 ] || parent ;
7776 var years = node . querySelectorAll ( '[md-calendar-year-body]' ) ;
7877 var yearHeader = year . toString ( ) ;
7978 var target ;
@@ -415,9 +414,9 @@ describe('md-calendar', function() {
415414 newScope . $apply ( ) ;
416415 element = createElement ( newScope ) [ 0 ] ;
417416
418- expect ( findMonthElement ( new Date ( 2014 , JUL , 1 ) ) ) . not . toBeNull ( ) ;
419- expect ( findMonthElement ( new Date ( 2014 , JUN , 1 ) ) ) . not . toBeNull ( ) ;
420- expect ( findMonthElement ( new Date ( 2014 , MAY , 1 ) ) ) . toBeNull ( ) ;
417+ expect ( findMonthElement ( element , new Date ( 2014 , JUL , 1 ) ) ) . not . toBeNull ( ) ;
418+ expect ( findMonthElement ( element , new Date ( 2014 , JUN , 1 ) ) ) . not . toBeNull ( ) ;
419+ expect ( findMonthElement ( element , new Date ( 2014 , MAY , 1 ) ) ) . toBeNull ( ) ;
421420 } ) ;
422421 } ) ;
423422
@@ -514,8 +513,27 @@ describe('md-calendar', function() {
514513 element . controller ( 'mdCalendar' ) . setCurrentView ( 'year' ) ;
515514 applyDateChange ( ) ;
516515
517- expect ( findYearElement ( 2014 ) ) . not . toBeNull ( ) ;
518- expect ( findYearElement ( 2013 ) ) . toBeNull ( ) ;
516+ expect ( findYearElement ( element , 2014 ) ) . not . toBeNull ( ) ;
517+ expect ( findYearElement ( element , 2013 ) ) . toBeNull ( ) ;
518+ } ) ;
519+
520+ it ( 'should highlight the proper cell, even when the date is not the ' +
521+ 'first day of the month' , function ( ) {
522+ var newScope = $rootScope . $new ( ) ;
523+
524+ newScope . myDate = new Date ( 2015 , MAY , 1 ) ;
525+ element = createElement ( newScope ) [ 0 ] ;
526+ angular . element ( element ) . controller ( 'mdCalendar' ) . setCurrentView ( 'year' ) ;
527+ applyDateChange ( ) ;
528+
529+ var yearElement = findYearElement ( element , 2015 ) ;
530+
531+ expect ( findCellByLabel ( yearElement , 'May' ) ) . toHaveClass ( 'md-calendar-selected-date' ) ;
532+
533+ newScope . myDate = new Date ( 2015 , JUL , 15 ) ;
534+ applyDateChange ( ) ;
535+
536+ expect ( findCellByLabel ( yearElement , 'Jul' ) ) . toHaveClass ( 'md-calendar-selected-date' ) ;
519537 } ) ;
520538
521539 it ( 'should ensure that all year elements have a height when the ' +
@@ -737,6 +755,106 @@ describe('md-calendar', function() {
737755 } ) ;
738756 } ) ;
739757
758+ describe ( 'md-mode support' , function ( ) {
759+ var element , controller ;
760+
761+ function compileElement ( attrs ) {
762+ ngElement . remove ( ) ;
763+ element = createElement ( pageScope , '<md-calendar ng-model="myDate" ' + ( attrs || '' ) + '></md-calendar>' ) ;
764+ controller = element . controller ( 'mdCalendar' ) ;
765+ }
766+
767+ it ( 'should go to the corresponding view' , function ( ) {
768+ compileElement ( 'md-mode="month"' ) ;
769+
770+ expect ( element . find ( 'md-calendar-month' ) . length ) . toBe ( 0 ) ;
771+ expect ( element . find ( 'md-calendar-year' ) . length ) . toBe ( 1 ) ;
772+ expect ( controller . currentView ) . toBe ( 'year' ) ;
773+ } ) ;
774+
775+ it ( 'should override md-current-view' , function ( ) {
776+ compileElement ( 'md-mode="day" md-current-view="year"' ) ;
777+
778+ expect ( element . find ( 'md-calendar-year' ) . length ) . toBe ( 0 ) ;
779+ expect ( element . find ( 'md-calendar-month' ) . length ) . toBe ( 1 ) ;
780+ expect ( controller . currentView ) . toBe ( 'month' ) ;
781+ } ) ;
782+
783+ it ( 'should not allow users to go to a different view' , function ( ) {
784+ compileElement ( 'md-mode="day"' ) ;
785+
786+ expect ( controller . currentView ) . toBe ( 'month' ) ;
787+
788+ element [ 0 ] . querySelector ( '.md-calendar-month-label' ) . click ( ) ;
789+ applyDateChange ( ) ;
790+
791+ expect ( controller . currentView ) . toBe ( 'month' ) ;
792+ } ) ;
793+
794+ it ( 'should allow users to navigate to a different view if the md-mode is not supported' , function ( ) {
795+ compileElement ( 'md-mode="invalid-mode"' ) ;
796+
797+ expect ( controller . currentView ) . toBe ( 'month' ) ;
798+ expect ( controller . mode ) . toBeFalsy ( ) ;
799+
800+ element [ 0 ] . querySelector ( '.md-calendar-month-label' ) . click ( ) ;
801+ applyDateChange ( ) ;
802+
803+ expect ( controller . currentView ) . toBe ( 'year' ) ;
804+ } ) ;
805+
806+ it ( 'should update the model when clicking on a cell in the year view' , function ( ) {
807+ pageScope . myDate = new Date ( 2015 , MAY , 15 ) ;
808+
809+ compileElement ( 'md-mode="month"' ) ;
810+
811+ expect ( controller . currentView ) . toBe ( 'year' ) ;
812+
813+ var yearElement = findYearElement ( element [ 0 ] , 2015 ) ;
814+ var monthCell = findCellByLabel ( yearElement , 'Sep' ) ;
815+ var expectedDate = new Date ( 2015 , SEP , 1 ) ;
816+
817+ monthCell . click ( ) ;
818+ applyDateChange ( ) ;
819+
820+ expect ( pageScope . myDate ) . toBeSameDayAs ( expectedDate ) ;
821+ expect ( controller . currentView ) . toBe ( 'year' ) ;
822+ } ) ;
823+
824+ it ( 'should update the model when clicking on a cell in the day view' , function ( ) {
825+ pageScope . myDate = new Date ( 2015 , MAY , 15 ) ;
826+
827+ compileElement ( 'md-mode="day"' ) ;
828+
829+ var monthElement = findMonthElement ( element [ 0 ] , pageScope . myDate ) ;
830+ var monthCell = findCellByLabel ( monthElement , '28' ) ;
831+ var expectedDate = new Date ( 2015 , MAY , 28 ) ;
832+
833+ monthCell . click ( ) ;
834+ applyDateChange ( ) ;
835+
836+ expect ( pageScope . myDate ) . toBeSameDayAs ( expectedDate ) ;
837+ } ) ;
838+ } ) ;
839+
840+ describe ( 'md-current-view support' , function ( ) {
841+ beforeEach ( function ( ) {
842+ ngElement && ngElement . remove ( ) ;
843+ } ) ;
844+
845+ it ( 'should have a configurable default view' , function ( ) {
846+ var calendar = createElement ( null , '<md-calendar ng-model="myDate" md-current-view="year"></md-calendar>' ) [ 0 ] ;
847+
848+ expect ( calendar . querySelector ( 'md-calendar-month' ) ) . toBeFalsy ( ) ;
849+ expect ( calendar . querySelector ( 'md-calendar-year' ) ) . toBeTruthy ( ) ;
850+ } ) ;
851+
852+ it ( 'should default to the month view if no view is supploied' , function ( ) {
853+ var calendar = createElement ( null , '<md-calendar ng-model="myDate"></md-calendar>' ) ;
854+ expect ( calendar . controller ( 'mdCalendar' ) . currentView ) . toBe ( 'month' ) ;
855+ } ) ;
856+ } ) ;
857+
740858 it ( 'should render one single-row month of disabled cells after the max date' , function ( ) {
741859 ngElement . remove ( ) ;
742860 var newScope = $rootScope . $new ( ) ;
@@ -745,11 +863,11 @@ describe('md-calendar', function() {
745863 newScope . $apply ( ) ;
746864 element = createElement ( newScope ) [ 0 ] ;
747865
748- expect ( findMonthElement ( new Date ( 2014 , MAR , 1 ) ) ) . not . toBeNull ( ) ;
749- expect ( findMonthElement ( new Date ( 2014 , APR , 1 ) ) ) . not . toBeNull ( ) ;
866+ expect ( findMonthElement ( element , new Date ( 2014 , MAR , 1 ) ) ) . not . toBeNull ( ) ;
867+ expect ( findMonthElement ( element , new Date ( 2014 , APR , 1 ) ) ) . not . toBeNull ( ) ;
750868
751869 // First date of May 2014 on Thursday (i.e. has 3 dates on the first row).
752- var nextMonth = findMonthElement ( new Date ( 2014 , MAY , 1 ) ) ;
870+ var nextMonth = findMonthElement ( element , new Date ( 2014 , MAY , 1 ) ) ;
753871 expect ( nextMonth ) . not . toBeNull ( ) ;
754872 expect ( nextMonth . querySelector ( '.md-calendar-month-label' ) ) . toHaveClass (
755873 'md-calendar-month-label-disabled' ) ;
@@ -764,12 +882,4 @@ describe('md-calendar', function() {
764882 }
765883 }
766884 } ) ;
767-
768- it ( 'should have a configurable default view' , function ( ) {
769- ngElement . remove ( ) ;
770- var calendar = createElement ( null , '<md-calendar ng-model="myDate" md-current-view="year"></md-calendar>' ) [ 0 ] ;
771-
772- expect ( calendar . querySelector ( 'md-calendar-month' ) ) . toBeFalsy ( ) ;
773- expect ( calendar . querySelector ( 'md-calendar-year' ) ) . toBeTruthy ( ) ;
774- } ) ;
775885} ) ;
0 commit comments