autocomplete.spec.js 13 KB

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