autocomplete.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { createVue } from '../util';
  2. describe('Autocomplete', () => {
  3. it('create', done => {
  4. const vm = createVue({
  5. template: `
  6. <el-autocomplete
  7. v-model="state"
  8. :fetch-suggestions="querySearch"
  9. placeholder="请输入内容autocomplete1"
  10. ></el-autocomplete>
  11. `,
  12. data() {
  13. return {
  14. restaurants: [],
  15. state: ''
  16. };
  17. },
  18. methods: {
  19. querySearch(queryString, cb) {
  20. var restaurants = this.restaurants;
  21. var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
  22. cb(results);
  23. },
  24. createFilter(queryString) {
  25. return (restaurant) => {
  26. return (restaurant.value.indexOf(queryString.toLowerCase()) === 0);
  27. };
  28. },
  29. loadAll() {
  30. return [
  31. { 'value': '三全鲜食(北新泾店)', 'address': '长宁区新渔路144号' },
  32. { 'value': 'Hot honey 首尔炸鸡(仙霞路)', 'address': '上海市长宁区淞虹路661号' },
  33. { 'value': '新旺角茶餐厅', 'address': '上海市普陀区真北路988号创邑金沙谷6号楼113' },
  34. { 'value': '泷千家(天山西路店)', 'address': '天山西路438号' }
  35. ];
  36. }
  37. },
  38. mounted() {
  39. this.restaurants = this.loadAll();
  40. }
  41. }, true);
  42. let elm = vm.$el;
  43. let inputElm = elm.querySelector('input');
  44. inputElm.focus();
  45. expect(inputElm.getAttribute('placeholder')).to.be.equal('请输入内容autocomplete1');
  46. setTimeout(_ => {
  47. let suggestionsList = elm.querySelector('.el-autocomplete__suggestions');
  48. expect(suggestionsList).to.exist;
  49. expect(suggestionsList.children.length).to.be.equal(4);
  50. document.body.click();
  51. setTimeout(_ => {
  52. expect(elm.querySelector('.el-autocomplete__suggestions')).to.not.exist;
  53. done();
  54. }, 500);
  55. }, 500);
  56. });
  57. it('select', done => {
  58. const vm = createVue({
  59. template: `
  60. <el-autocomplete
  61. v-model="state"
  62. :fetch-suggestions="querySearch"
  63. placeholder="请输入内容autocomplete2"
  64. @select="handleSelect"
  65. ></el-autocomplete>
  66. `,
  67. data() {
  68. return {
  69. restaurants: [],
  70. state: '',
  71. onceSelected: false
  72. };
  73. },
  74. methods: {
  75. querySearch(queryString, cb) {
  76. var restaurants = this.restaurants;
  77. var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
  78. cb(results);
  79. },
  80. createFilter(queryString) {
  81. return (restaurant) => {
  82. return (restaurant.value.indexOf(queryString.toLowerCase()) === 0);
  83. };
  84. },
  85. loadAll() {
  86. return [
  87. { 'value': '三全鲜食(北新泾店)', 'address': '长宁区新渔路144号' },
  88. { 'value': 'Hot honey 首尔炸鸡(仙霞路)', 'address': '上海市长宁区淞虹路661号' },
  89. { 'value': '新旺角茶餐厅', 'address': '上海市普陀区真北路988号创邑金沙谷6号楼113' },
  90. { 'value': '泷千家(天山西路店)', 'address': '天山西路438号' }
  91. ];
  92. },
  93. handleSelect() {
  94. this.onceSelected = true;
  95. }
  96. },
  97. mounted() {
  98. this.restaurants = this.loadAll();
  99. }
  100. }, true);
  101. let elm = vm.$el;
  102. let inputElm = elm.querySelector('input');
  103. inputElm.focus();
  104. setTimeout(_ => {
  105. let suggestionsList = elm.querySelector('.el-autocomplete__suggestions');
  106. suggestionsList.children[1].click();
  107. setTimeout(_ => {
  108. expect(inputElm.value).to.be.equal('Hot honey 首尔炸鸡(仙霞路)');
  109. expect(vm.state).to.be.equal('Hot honey 首尔炸鸡(仙霞路)');
  110. expect(vm.onceSelected).to.be.true;
  111. expect(elm.querySelector('.el-autocomplete__suggestions')).to.not.exist;
  112. done();
  113. }, 500);
  114. }, 500);
  115. });
  116. it('highlight', done => {
  117. const vm = createVue({
  118. template: `
  119. <el-autocomplete
  120. ref="autocomplete"
  121. v-model="state"
  122. :fetch-suggestions="querySearch"
  123. placeholder="请输入内容autocomplete3"
  124. @select="handleSelect"
  125. ></el-autocomplete>
  126. `,
  127. data() {
  128. return {
  129. restaurants: [],
  130. state: '',
  131. onceSelected: false
  132. };
  133. },
  134. methods: {
  135. querySearch(queryString, cb) {
  136. var restaurants = this.restaurants;
  137. var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
  138. cb(results);
  139. },
  140. createFilter(queryString) {
  141. return (restaurant) => {
  142. return (restaurant.value.indexOf(queryString.toLowerCase()) === 0);
  143. };
  144. },
  145. loadAll() {
  146. return [
  147. { 'value': '三全鲜食(北新泾店)', 'address': '长宁区新渔路144号' },
  148. { 'value': 'Hot honey 首尔炸鸡(仙霞路)', 'address': '上海市长宁区淞虹路661号' },
  149. { 'value': '新旺角茶餐厅', 'address': '上海市普陀区真北路988号创邑金沙谷6号楼113' },
  150. { 'value': '泷千家(天山西路店)', 'address': '天山西路438号' },
  151. { 'value': '胖仙女纸杯蛋糕(上海凌空店)', 'address': '上海市长宁区金钟路968号1幢18号楼一层商铺18-101' },
  152. { 'value': '贡茶', 'address': '上海市长宁区金钟路633号' },
  153. { 'value': '豪大大香鸡排超级奶爸', 'address': '上海市嘉定区曹安公路曹安路1685号' },
  154. { 'value': '茶芝兰(奶茶,手抓饼)', 'address': '上海市普陀区同普路1435号' },
  155. { 'value': '十二泷町', 'address': '上海市北翟路1444弄81号B幢-107' },
  156. { 'value': '星移浓缩咖啡', 'address': '上海市嘉定区新郁路817号' },
  157. { 'value': '阿姨奶茶/豪大大', 'address': '嘉定区曹安路1611号' },
  158. { 'value': '新麦甜四季甜品炸鸡', 'address': '嘉定区曹安公路2383弄55号' },
  159. { 'value': 'Monica摩托主题咖啡店', 'address': '嘉定区江桥镇曹安公路2409号1F,2383弄62号1F' },
  160. { 'value': '浮生若茶(凌空soho店)', 'address': '上海长宁区金钟路968号9号楼地下一层' },
  161. { 'value': 'NONO JUICE 鲜榨果汁', 'address': '上海市长宁区天山西路119号' },
  162. { 'value': 'CoCo都可(北新泾店)', 'address': '上海市长宁区仙霞西路' },
  163. { 'value': '快乐柠檬(神州智慧店)', 'address': '上海市长宁区天山西路567号1层R117号店铺' },
  164. { 'value': 'Merci Paul cafe', 'address': '上海市普陀区光复西路丹巴路28弄6号楼819' },
  165. { 'value': '猫山王(西郊百联店)', 'address': '上海市长宁区仙霞西路88号第一层G05-F01-1-306' },
  166. { 'value': '枪会山', 'address': '上海市普陀区棕榈路' },
  167. { 'value': '纵食', 'address': '元丰天山花园(东门) 双流路267号' },
  168. { 'value': '钱记', 'address': '上海市长宁区天山西路' }
  169. ];
  170. },
  171. handleSelect() {
  172. this.onceSelected = true;
  173. }
  174. },
  175. mounted() {
  176. this.restaurants = this.loadAll();
  177. }
  178. }, true);
  179. let elm = vm.$el;
  180. let inputElm = elm.querySelector('input');
  181. inputElm.focus();
  182. setTimeout(_ => {
  183. vm.$refs.autocomplete.highlight(8);
  184. vm.$nextTick(_ => {
  185. let suggestionsList = elm.querySelector('.el-autocomplete__suggestions');
  186. let highlightedItem = suggestionsList.children[8];
  187. expect(highlightedItem.className).to.be.equal('highlighted');
  188. expect(suggestionsList.scrollTop === highlightedItem.scrollHeight).to.be.true;
  189. vm.$refs.autocomplete.highlight(8);
  190. done();
  191. });
  192. }, 500);
  193. });
  194. it('highlight out of bounds', done => {
  195. const vm = createVue({
  196. template: `
  197. <el-autocomplete
  198. ref="autocomplete"
  199. v-model="state"
  200. :fetch-suggestions="querySearch"
  201. placeholder="请输入内容autocomplete3"
  202. @select="handleSelect"
  203. ></el-autocomplete>
  204. `,
  205. data() {
  206. return {
  207. restaurants: [],
  208. state: '',
  209. onceSelected: false
  210. };
  211. },
  212. methods: {
  213. querySearch(queryString, cb) {
  214. var restaurants = this.restaurants;
  215. var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
  216. cb(results);
  217. },
  218. createFilter(queryString) {
  219. return (restaurant) => {
  220. return (restaurant.value.indexOf(queryString.toLowerCase()) === 0);
  221. };
  222. },
  223. loadAll() {
  224. return [
  225. { 'value': '三全鲜食(北新泾店)', 'address': '长宁区新渔路144号' },
  226. { 'value': 'Hot honey 首尔炸鸡(仙霞路)', 'address': '上海市长宁区淞虹路661号' },
  227. { 'value': '新旺角茶餐厅', 'address': '上海市普陀区真北路988号创邑金沙谷6号楼113' },
  228. { 'value': '泷千家(天山西路店)', 'address': '天山西路438号' },
  229. { 'value': '胖仙女纸杯蛋糕(上海凌空店)', 'address': '上海市长宁区金钟路968号1幢18号楼一层商铺18-101' },
  230. { 'value': '贡茶', 'address': '上海市长宁区金钟路633号' },
  231. { 'value': '豪大大香鸡排超级奶爸', 'address': '上海市嘉定区曹安公路曹安路1685号' },
  232. { 'value': '茶芝兰(奶茶,手抓饼)', 'address': '上海市普陀区同普路1435号' },
  233. { 'value': '十二泷町', 'address': '上海市北翟路1444弄81号B幢-107' },
  234. { 'value': '星移浓缩咖啡', 'address': '上海市嘉定区新郁路817号' },
  235. { 'value': '阿姨奶茶/豪大大', 'address': '嘉定区曹安路1611号' },
  236. { 'value': '新麦甜四季甜品炸鸡', 'address': '嘉定区曹安公路2383弄55号' }
  237. ];
  238. },
  239. handleSelect() {
  240. this.onceSelected = true;
  241. }
  242. },
  243. mounted() {
  244. this.restaurants = this.loadAll();
  245. }
  246. }, true);
  247. let elm = vm.$el;
  248. let inputElm = elm.querySelector('input');
  249. inputElm.focus();
  250. setTimeout(_ => {
  251. vm.$refs.autocomplete.highlight(15);
  252. vm.$nextTick(_ => {
  253. let suggestionsList = elm.querySelector('.el-autocomplete__suggestions');
  254. let highlightedItem = suggestionsList.children[11];
  255. expect(highlightedItem.className).to.be.equal('highlighted');
  256. vm.$refs.autocomplete.highlight(-5);
  257. vm.$nextTick(_ => {
  258. let highlightedItem = suggestionsList.children[0];
  259. expect(highlightedItem.className).to.be.equal('highlighted');
  260. });
  261. done();
  262. });
  263. }, 500);
  264. });
  265. });