@@ -17,7 +17,6 @@ describe('md-calendar', function() {
17
17
*/
18
18
function applyDateChange ( ) {
19
19
$timeout . flush ( ) ;
20
- pageScope . $apply ( ) ;
21
20
$material . flushOutstandingAnimations ( ) ;
22
21
23
22
// Internally, the calendar sets scrollTop to scroll to the month for a change.
@@ -57,7 +56,7 @@ describe('md-calendar', function() {
57
56
/**
58
57
* Finds a month `tbody` in the calendar element given a date.
59
58
*/
60
- function findMonthElement ( date ) {
59
+ function findMonthElement ( element , date ) {
61
60
var months = element . querySelectorAll ( '[md-calendar-month-body]' ) ;
62
61
var monthHeader = dateLocale . monthHeaderFormatter ( date ) ;
63
62
var month ;
@@ -72,8 +71,8 @@ describe('md-calendar', function() {
72
71
}
73
72
74
73
/** 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 ;
77
76
var years = node . querySelectorAll ( '[md-calendar-year-body]' ) ;
78
77
var yearHeader = year . toString ( ) ;
79
78
var target ;
@@ -415,9 +414,9 @@ describe('md-calendar', function() {
415
414
newScope . $apply ( ) ;
416
415
element = createElement ( newScope ) [ 0 ] ;
417
416
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 ( ) ;
421
420
} ) ;
422
421
} ) ;
423
422
@@ -514,8 +513,27 @@ describe('md-calendar', function() {
514
513
element . controller ( 'mdCalendar' ) . setCurrentView ( 'year' ) ;
515
514
applyDateChange ( ) ;
516
515
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' ) ;
519
537
} ) ;
520
538
521
539
it ( 'should ensure that all year elements have a height when the ' +
@@ -737,6 +755,106 @@ describe('md-calendar', function() {
737
755
} ) ;
738
756
} ) ;
739
757
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
+
740
858
it ( 'should render one single-row month of disabled cells after the max date' , function ( ) {
741
859
ngElement . remove ( ) ;
742
860
var newScope = $rootScope . $new ( ) ;
@@ -745,11 +863,11 @@ describe('md-calendar', function() {
745
863
newScope . $apply ( ) ;
746
864
element = createElement ( newScope ) [ 0 ] ;
747
865
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 ( ) ;
750
868
751
869
// 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 ) ) ;
753
871
expect ( nextMonth ) . not . toBeNull ( ) ;
754
872
expect ( nextMonth . querySelector ( '.md-calendar-month-label' ) ) . toHaveClass (
755
873
'md-calendar-month-label-disabled' ) ;
@@ -764,12 +882,4 @@ describe('md-calendar', function() {
764
882
}
765
883
}
766
884
} ) ;
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
- } ) ;
775
885
} ) ;
0 commit comments