search.e2e.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. 'use strict';
  2. describe('Search plugin', function () {
  3. var id = 'testContainer';
  4. beforeEach(function () {
  5. this.$container = $('<div id="' + id + '"></div>').appendTo('body');
  6. });
  7. afterEach(function () {
  8. if (this.$container) {
  9. destroy();
  10. this.$container.remove();
  11. }
  12. });
  13. describe('enabling/disabling plugin', function () {
  14. it('should expose `search` object when plugin is enabled', function () {
  15. var hot = handsontable({
  16. search: true
  17. });
  18. expect(hot.search).toBeDefined();
  19. });
  20. it('should NOT expose `search` object when plugin is disabled', function () {
  21. var hot = handsontable({
  22. search: false
  23. });
  24. expect(hot.search).not.toBeDefined();
  25. });
  26. it('plugin should be disabled by default', function () {
  27. var hot = handsontable();
  28. expect(hot.search).not.toBeDefined();
  29. });
  30. it('should disable plugin using updateSettings', function () {
  31. var hot = handsontable({
  32. search: true
  33. });
  34. expect(hot.search).toBeDefined();
  35. updateSettings({
  36. search: false
  37. });
  38. expect(hot.search).not.toBeDefined();
  39. });
  40. it('should enable plugin using updateSettings', function () {
  41. var hot = handsontable({
  42. search: false
  43. });
  44. expect(hot.search).not.toBeDefined();
  45. updateSettings({
  46. search: true
  47. });
  48. expect(hot.search).toBeDefined();
  49. });
  50. });
  51. describe('query method', function () {
  52. afterEach(function () {
  53. Handsontable.plugins.Search.global.setDefaultQueryMethod(Handsontable.plugins.Search.DEFAULT_QUERY_METHOD);
  54. });
  55. it('should use the default query method if no queryMethod is passed to query function', function () {
  56. spyOn(Handsontable.plugins.Search, 'DEFAULT_QUERY_METHOD');
  57. var defaultQueryMethod = Handsontable.plugins.Search.DEFAULT_QUERY_METHOD;
  58. Handsontable.plugins.Search.global.setDefaultQueryMethod(defaultQueryMethod);
  59. var hot = handsontable({
  60. data: Handsontable.helper.createSpreadsheetData(5, 5),
  61. search: true
  62. });
  63. var searchResult = hot.search.query('A');
  64. expect(defaultQueryMethod.calls.count()).toEqual(25);
  65. });
  66. it('should use the custom default query method if no queryMethod is passed to query function', function () {
  67. var customDefaultQueryMethod = jasmine.createSpy('customDefaultQueryMethod');
  68. var hot = handsontable({
  69. data: Handsontable.helper.createSpreadsheetData(5, 5),
  70. search: true
  71. });
  72. Handsontable.plugins.Search.global.setDefaultQueryMethod(customDefaultQueryMethod);
  73. var searchResult = hot.search.query('A');
  74. expect(customDefaultQueryMethod.calls.count()).toEqual(25);
  75. });
  76. it('should use the query method from the constructor if no queryMethod is passed to query function', function () {
  77. var customQueryMethod = jasmine.createSpy('customDefaultQueryMethod');
  78. var hot = handsontable({
  79. data: Handsontable.helper.createSpreadsheetData(5, 5),
  80. search: {
  81. queryMethod: customQueryMethod
  82. }
  83. });
  84. var searchResult = hot.search.query('A');
  85. expect(customQueryMethod.calls.count()).toEqual(25);
  86. });
  87. it('should use method passed to query function', function () {
  88. var customQueryMethod = jasmine.createSpy('customQueryMethod');
  89. var hot = handsontable({
  90. data: Handsontable.helper.createSpreadsheetData(5, 5),
  91. search: true
  92. });
  93. var searchResult = hot.search.query('A', null, customQueryMethod);
  94. expect(customQueryMethod.calls.count()).toEqual(25);
  95. });
  96. });
  97. describe('default query method', function () {
  98. it('should use query method to find phrase', function () {
  99. var hot = handsontable({
  100. data: Handsontable.helper.createSpreadsheetData(5, 5),
  101. search: true
  102. });
  103. var searchResult = hot.search.query('A');
  104. expect(searchResult.length).toEqual(5);
  105. for (var i = 0; i < searchResult.length; i++) {
  106. expect(searchResult[i].row).toEqual(i);
  107. expect(searchResult[i].col).toEqual(0);
  108. expect(searchResult[i].data).toEqual(hot.getDataAtCell(i, 0));
  109. }
  110. });
  111. it('default query method should be case insensitive', function () {
  112. var hot = handsontable({
  113. data: Handsontable.helper.createSpreadsheetData(5, 5),
  114. search: true
  115. });
  116. var searchResult = hot.search.query('a');
  117. expect(searchResult.length).toEqual(5);
  118. searchResult = hot.search.query('A');
  119. expect(searchResult.length).toEqual(5);
  120. });
  121. it('default query method should work with numeric values', function () {
  122. var hot = handsontable({
  123. data: [[1, 2], [22, 4]],
  124. search: true
  125. });
  126. var searchResult = hot.search.query('2');
  127. expect(searchResult.length).toEqual(2);
  128. });
  129. it('default query method should interpret query as string, not regex', function () {
  130. var hot = handsontable({
  131. data: Handsontable.helper.createSpreadsheetData(5, 5),
  132. search: true
  133. });
  134. var searchResult = hot.search.query('A*');
  135. expect(searchResult.length).toEqual(0);
  136. });
  137. it('default query method should always return false if query string is empty', function () {
  138. var hot = handsontable({
  139. data: Handsontable.helper.createSpreadsheetData(5, 5),
  140. search: true
  141. });
  142. var searchResult = hot.search.query('A');
  143. expect(searchResult.length).toEqual(5);
  144. searchResult = hot.search.query('');
  145. expect(searchResult.length).toEqual(0);
  146. });
  147. it('default query method should always return false if no query string has been specified', function () {
  148. var hot = handsontable({
  149. data: Handsontable.helper.createSpreadsheetData(5, 5),
  150. search: true
  151. });
  152. var searchResult = hot.search.query('A');
  153. expect(searchResult.length).toEqual(5);
  154. searchResult = hot.search.query();
  155. expect(searchResult.length).toEqual(0);
  156. });
  157. it('default query method should always return false if no query string is not a string', function () {
  158. var hot = handsontable({
  159. data: Handsontable.helper.createSpreadsheetData(5, 5),
  160. search: true
  161. });
  162. var searchResult = hot.search.query('A');
  163. expect(searchResult.length).toEqual(5);
  164. searchResult = hot.search.query([1, 2, 3]);
  165. expect(searchResult.length).toEqual(0);
  166. });
  167. });
  168. describe('search callback', function () {
  169. afterEach(function () {
  170. Handsontable.plugins.Search.global.setDefaultCallback(Handsontable.plugins.Search.DEFAULT_CALLBACK);
  171. });
  172. it('should invoke default callback for each cell', function () {
  173. spyOn(Handsontable.plugins.Search, 'DEFAULT_CALLBACK');
  174. var defaultCallback = Handsontable.plugins.Search.DEFAULT_CALLBACK;
  175. Handsontable.plugins.Search.global.setDefaultCallback(defaultCallback);
  176. var hot = handsontable({
  177. data: Handsontable.helper.createSpreadsheetData(5, 5),
  178. search: true
  179. });
  180. var searchResult = hot.search.query('A');
  181. expect(defaultCallback.calls.count()).toEqual(25);
  182. });
  183. it('should change the default callback', function () {
  184. spyOn(Handsontable.plugins.Search, 'DEFAULT_CALLBACK');
  185. var hot = handsontable({
  186. data: Handsontable.helper.createSpreadsheetData(5, 5),
  187. search: true
  188. });
  189. var defaultCallback = jasmine.createSpy('defaultCallback');
  190. Handsontable.plugins.Search.global.setDefaultCallback(defaultCallback);
  191. var searchResult = hot.search.query('A');
  192. expect(Handsontable.plugins.Search.DEFAULT_CALLBACK).not.toHaveBeenCalled();
  193. expect(defaultCallback.calls.count()).toEqual(25);
  194. });
  195. it('should invoke callback passed in constructor', function () {
  196. var searchCallback = jasmine.createSpy('searchCallback');
  197. var hot = handsontable({
  198. data: Handsontable.helper.createSpreadsheetData(5, 5),
  199. search: {
  200. callback: searchCallback
  201. }
  202. });
  203. var searchResult = hot.search.query('A');
  204. expect(searchCallback.calls.count()).toEqual(25);
  205. });
  206. it('should invoke custom callback for each cell which has been tested', function () {
  207. var hot = handsontable({
  208. data: Handsontable.helper.createSpreadsheetData(5, 5),
  209. search: true
  210. });
  211. var searchCallback = jasmine.createSpy('searchCallback');
  212. var searchResult = hot.search.query('A', searchCallback);
  213. expect(searchCallback.calls.count()).toEqual(25);
  214. for (var rowIndex = 0, rowCount = countRows(); rowIndex < rowCount; rowIndex++) {
  215. for (var colIndex = 0, colCount = countCols(); colIndex < colCount; colIndex++) {
  216. var callArgs = searchCallback.calls.argsFor(rowIndex * 5 + colIndex);
  217. expect(callArgs[0]).toEqual(hot);
  218. expect(callArgs[1]).toEqual(rowIndex);
  219. expect(callArgs[2]).toEqual(colIndex);
  220. expect(callArgs[3]).toEqual(hot.getDataAtCell(rowIndex, colIndex));
  221. if (colIndex == 0) {
  222. expect(callArgs[4]).toBe(true);
  223. } else {
  224. expect(callArgs[4]).toBe(false);
  225. }
  226. }
  227. }
  228. });
  229. });
  230. describe('default search callback', function () {
  231. it('should add isSearchResult = true, to cell properties of all matched cells', function () {
  232. var hot = handsontable({
  233. data: Handsontable.helper.createSpreadsheetData(5, 5),
  234. search: true
  235. });
  236. var searchResult = hot.search.query('2');
  237. for (var rowIndex = 0, rowCount = countRows(); rowIndex < rowCount; rowIndex++) {
  238. for (var colIndex = 0, colCount = countCols(); colIndex < colCount; colIndex++) {
  239. var cellProperties = getCellMeta(rowIndex, colIndex);
  240. if (rowIndex == 1) {
  241. expect(cellProperties.isSearchResult).toBeTruthy();
  242. } else {
  243. expect(cellProperties.isSearchResult).toBeFalsy();
  244. }
  245. }
  246. }
  247. });
  248. });
  249. describe('search result decorator', function () {
  250. it('should add default search result class to cells which mach the query', function () {
  251. var hot = handsontable({
  252. data: Handsontable.helper.createSpreadsheetData(5, 5),
  253. search: true
  254. });
  255. var searchResult = hot.search.query('2');
  256. render();
  257. for (var rowIndex = 0, rowCount = countRows(); rowIndex < rowCount; rowIndex++) {
  258. for (var colIndex = 0, colCount = countCols(); colIndex < colCount; colIndex++) {
  259. var cell = getCell(rowIndex, colIndex);
  260. if (rowIndex == 1) {
  261. expect($(cell).hasClass(Handsontable.plugins.Search.DEFAULT_SEARCH_RESULT_CLASS)).toBe(true);
  262. } else {
  263. expect($(cell).hasClass(Handsontable.plugins.Search.DEFAULT_SEARCH_RESULT_CLASS)).toBe(false);
  264. }
  265. }
  266. }
  267. });
  268. it('should add custom search result class to cells which mach the query', function () {
  269. var hot = handsontable({
  270. data: Handsontable.helper.createSpreadsheetData(5, 5),
  271. search: {
  272. searchResultClass: 'customSearchResultClass'
  273. }
  274. });
  275. var searchResult = hot.search.query('2');
  276. render();
  277. for (var rowIndex = 0, rowCount = countRows(); rowIndex < rowCount; rowIndex++) {
  278. for (var colIndex = 0, colCount = countCols(); colIndex < colCount; colIndex++) {
  279. var cell = getCell(rowIndex, colIndex);
  280. if (rowIndex == 1) {
  281. expect($(cell).hasClass('customSearchResultClass')).toBe(true);
  282. } else {
  283. expect($(cell).hasClass('customSearchResultClass')).toBe(false);
  284. }
  285. }
  286. }
  287. });
  288. });
  289. describe('HOT properties compatibility', function () {
  290. it('should work properly when the last row is empty', function () {
  291. // connected with https://github.com/handsontable/handsontable/issues/1606
  292. var hot = handsontable({
  293. data: Handsontable.helper.createSpreadsheetData(5, 5),
  294. colHeaders: true,
  295. search: true,
  296. minSpareRows: 1
  297. }),
  298. errorThrown = false;
  299. try {
  300. hot.search.query('A');
  301. } catch (err) {
  302. errorThrown = true;
  303. }
  304. expect(errorThrown).toBe(false);
  305. });
  306. });
  307. });