autocomplete.spec.js 11 KB

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