autocomplete.spec.js 11 KB

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