@@ -1798,6 +1798,26 @@ describe('<md-autocomplete>', function() {
1798
1798
1799
1799
describe ( 'dropdown position' , function ( ) {
1800
1800
1801
+ var DEFAULT_MAX_ITEMS = 5 ;
1802
+ var DEFAULT_ITEM_HEIGHT = 48 ;
1803
+
1804
+ var dropdownItems = DEFAULT_MAX_ITEMS ;
1805
+
1806
+ /**
1807
+ * Function to create fake matches with the given dropdown items.
1808
+ * Useful when running tests against the dropdown max items calculations.
1809
+ * @returns {Array } Fake matches.
1810
+ */
1811
+ function fakeItemMatch ( ) {
1812
+ var matches = [ ] ;
1813
+
1814
+ for ( var i = 0 ; i < dropdownItems ; i ++ ) {
1815
+ matches . push ( 'Item ' + i ) ;
1816
+ }
1817
+
1818
+ return matches ;
1819
+ }
1820
+
1801
1821
it ( 'should adjust the width when the window resizes' , inject ( function ( $timeout , $window ) {
1802
1822
var scope = createScope ( ) ;
1803
1823
@@ -1998,6 +2018,104 @@ describe('<md-autocomplete>', function() {
1998
2018
document . body . removeChild ( parent [ 0 ] ) ;
1999
2019
} ) ) ;
2000
2020
2021
+ it ( 'should calculate the height from the default max items' , inject ( function ( $timeout ) {
2022
+ var scope = createScope ( ) ;
2023
+
2024
+ scope . match = fakeItemMatch ;
2025
+
2026
+ var template =
2027
+ '<div>' +
2028
+ '<md-autocomplete ' +
2029
+ 'md-search-text="searchText" ' +
2030
+ 'md-items="item in match(searchText)" ' +
2031
+ 'md-item-text="item" ' +
2032
+ 'md-min-length="0" ' +
2033
+ 'placeholder="placeholder">' +
2034
+ '<span md-highlight-text="searchText">{{item}}</span>' +
2035
+ '</md-autocomplete>' +
2036
+ '</div>' ;
2037
+
2038
+ var parent = compile ( template , scope ) ;
2039
+ var element = parent . find ( 'md-autocomplete' ) ;
2040
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
2041
+
2042
+ // Add container to the DOM to be able to test the rect calculations.
2043
+ document . body . appendChild ( parent [ 0 ] ) ;
2044
+
2045
+ $timeout . flush ( ) ;
2046
+
2047
+ // Focus the autocomplete and trigger a query to be able to open the dropdown.
2048
+ ctrl . focus ( ) ;
2049
+ scope . $apply ( 'searchText = "Query 1"' ) ;
2050
+ waitForVirtualRepeat ( element ) ;
2051
+
2052
+ var scrollContainer = document . body . querySelector ( '.md-virtual-repeat-container' ) ;
2053
+
2054
+ expect ( scrollContainer ) . toBeTruthy ( ) ;
2055
+ expect ( scrollContainer . style . maxHeight ) . toBe ( DEFAULT_MAX_ITEMS * DEFAULT_ITEM_HEIGHT + 'px' ) ;
2056
+
2057
+ dropdownItems = 6 ;
2058
+
2059
+ // Trigger a new query to request an update of the items and dropdown.
2060
+ scope . $apply ( 'searchText = "Query 2"' ) ;
2061
+
2062
+ // The dropdown should not increase its height because of the new extra item.
2063
+ expect ( scrollContainer . style . maxHeight ) . toBe ( DEFAULT_MAX_ITEMS * DEFAULT_ITEM_HEIGHT + 'px' ) ;
2064
+
2065
+ document . body . removeChild ( parent [ 0 ] ) ;
2066
+ } ) ) ;
2067
+
2068
+ it ( 'should calculate its height from the specified max items' , inject ( function ( $timeout ) {
2069
+ var scope = createScope ( ) ;
2070
+ var maxDropdownItems = 2 ;
2071
+
2072
+ // Set the current dropdown items to the new maximum.
2073
+ dropdownItems = maxDropdownItems ;
2074
+ scope . match = fakeItemMatch ;
2075
+
2076
+ var template =
2077
+ '<div>' +
2078
+ '<md-autocomplete ' +
2079
+ 'md-search-text="searchText" ' +
2080
+ 'md-items="item in match(searchText)" ' +
2081
+ 'md-item-text="item" ' +
2082
+ 'md-min-length="0" ' +
2083
+ 'md-dropdown-items="' + maxDropdownItems + '"' +
2084
+ 'placeholder="placeholder">' +
2085
+ '<span md-highlight-text="searchText">{{item}}</span>' +
2086
+ '</md-autocomplete>' +
2087
+ '</div>' ;
2088
+
2089
+ var parent = compile ( template , scope ) ;
2090
+ var element = parent . find ( 'md-autocomplete' ) ;
2091
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
2092
+
2093
+ // Add container to the DOM to be able to test the rect calculations.
2094
+ document . body . appendChild ( parent [ 0 ] ) ;
2095
+
2096
+ $timeout . flush ( ) ;
2097
+
2098
+ // Focus the autocomplete and trigger a query to be able to open the dropdown.
2099
+ ctrl . focus ( ) ;
2100
+ scope . $apply ( 'searchText = "Query 1"' ) ;
2101
+ waitForVirtualRepeat ( element ) ;
2102
+
2103
+ var scrollContainer = document . body . querySelector ( '.md-virtual-repeat-container' ) ;
2104
+
2105
+ expect ( scrollContainer ) . toBeTruthy ( ) ;
2106
+ expect ( scrollContainer . style . maxHeight ) . toBe ( maxDropdownItems * DEFAULT_ITEM_HEIGHT + 'px' ) ;
2107
+
2108
+ dropdownItems = 6 ;
2109
+
2110
+ // Trigger a new query to request an update of the items and dropdown.
2111
+ scope . $apply ( 'searchText = "Query 2"' ) ;
2112
+
2113
+ // The dropdown should not increase its height because of the new extra item.
2114
+ expect ( scrollContainer . style . maxHeight ) . toBe ( maxDropdownItems * DEFAULT_ITEM_HEIGHT + 'px' ) ;
2115
+
2116
+ document . body . removeChild ( parent [ 0 ] ) ;
2117
+ } ) ) ;
2118
+
2001
2119
} ) ;
2002
2120
2003
2121
describe ( 'md-highlight-text' , function ( ) {
0 commit comments