table_view_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('utils/db');
  20. require('views/common/filter_view');
  21. require('views/common/sort_view');
  22. require('mixins');
  23. require('mixins/common/userPref');
  24. require('views/common/table_view');
  25. describe('App.TableView', function () {
  26. var view;
  27. beforeEach(function() {
  28. App.db.cleanUp();
  29. });
  30. afterEach(function() {
  31. App.db.cleanUp();
  32. });
  33. describe('#init', function() {
  34. it('should set filterConditions on instance', function() {
  35. var tableView = App.TableView.create();
  36. expect(tableView.get('filterConditions') === App.TableView.prototype.filterConditions).to.be.false;
  37. });
  38. });
  39. describe('#updatePaging', function() {
  40. beforeEach(function() {
  41. view = App.TableView.create(App.UserPref, {
  42. controller: Em.Object.create({}),
  43. displayLength: 10,
  44. startIndex: 1,
  45. content: d3.range(1, 100),
  46. filteredContent: d3.range(1, 100),
  47. filtersUsedCalc: function() {},
  48. filter: function() {}
  49. });
  50. view.clearFilters();
  51. view.updateFilter();
  52. });
  53. it('should set "startIndex" to 0 if "filteredContent" is empty', function() {
  54. view.set('filteredContent', []);
  55. expect(view.get('startIndex')).to.equal(0);
  56. });
  57. it('should set "startIndex" to 1 if "filteredContent" is not empty', function() {
  58. view.set('filteredContent', d3.range(1, 10));
  59. expect(view.get('startIndex')).to.equal(1);
  60. });
  61. });
  62. describe('#endIndex', function() {
  63. beforeEach(function() {
  64. view = App.TableView.create(App.UserPref, {
  65. controller: Em.Object.create({}),
  66. displayLength: 10,
  67. startIndex: 1,
  68. content: d3.range(1, 100),
  69. filteredContent: d3.range(1, 100),
  70. filtersUsedCalc: function() {},
  71. filter: function() {}
  72. });
  73. view.clearFilters();
  74. view.updateFilter();
  75. });
  76. it('should be recalculated if "startIndex" was changed', function() {
  77. view.set('startIndex', 2);
  78. expect(view.get('endIndex')).to.equal(11);
  79. });
  80. it('should be recalculated if "displayLength" was changed', function() {
  81. view.set('displayLength', 5);
  82. expect(view.get('endIndex')).to.equal(5);
  83. });
  84. it('should be recalculated (but not changed) if "filteredContent" was changed (and "filterContent.length" is more than "startIndex + displayLength")', function() {
  85. var endIndexBefore = view.get('endIndex');
  86. view.set('filteredContent', d3.range(2,100));
  87. expect(view.get('endIndex')).to.equal(endIndexBefore);
  88. });
  89. it('should be recalculated (and changed) if "filteredContent" was changed (and "filterContent.length" is less than "startIndex + displayLength")', function() {
  90. var endIndexBefore = view.get('endIndex');
  91. var indx = 4;
  92. view.set('filteredContent', d3.range(1,indx));
  93. expect(view.get('endIndex')).to.not.equal(endIndexBefore);
  94. expect(view.get('endIndex')).to.equal(indx - 1);
  95. });
  96. });
  97. describe('#pageContent', function() {
  98. beforeEach(function() {
  99. view = App.TableView.create(App.UserPref, {
  100. controller: Em.Object.create({}),
  101. displayLength: 10,
  102. startIndex: 1,
  103. content: d3.range(1, 100),
  104. filteredContent: d3.range(1, 100),
  105. endIndex: 10,
  106. filtersUsedCalc: function() {},
  107. filter: function() {}
  108. });
  109. view.clearFilters();
  110. view.updateFilter();
  111. });
  112. it('should be recalculated if "startIndex" was changed', function() {
  113. view.set('startIndex', 2);
  114. expect(view.get('pageContent').length).to.equal(9);
  115. });
  116. it('should be recalculated if "endIndex" was changed', function() {
  117. view.set('endIndex', 5);
  118. expect(view.get('pageContent').length).to.equal(5);
  119. });
  120. it('should be recalculated if "filteredContent" was changed', function() {
  121. var pageContentBefore = view.get('pageContent');
  122. view.set('filteredContent', d3.range(2,100));
  123. expect(view.get('pageContent').length).to.equal(pageContentBefore.length);
  124. expect(view.get('pageContent')).to.not.eql(pageContentBefore);
  125. });
  126. });
  127. describe('#clearFilters', function() {
  128. it('should set "filterConditions" to empty array', function() {
  129. view.clearFilters();
  130. expect(view.get('filterConditions')).to.eql([]);
  131. });
  132. });
  133. describe('#filtersUsedCalc', function() {
  134. beforeEach(function() {
  135. view = App.TableView.create(App.UserPref, {
  136. controller: Em.Object.create({}),
  137. displayLength: 10,
  138. startIndex: 1,
  139. content: d3.range(1, 100),
  140. filteredContent: d3.range(1, 100),
  141. endIndex: 10,
  142. filter: function() {}
  143. });
  144. });
  145. it('should set "filtersUsed" to false if "filterConditions" is empty array', function() {
  146. view.set('filterConditions', []);
  147. view.filtersUsedCalc();
  148. expect(view.get('filtersUsed')).to.equal(false);
  149. });
  150. it('should set "filtersUsed" to false if each value in "filterConditions" is empty', function() {
  151. view.set('filterConditions', [{value:''}, {value:''}]);
  152. view.filtersUsedCalc();
  153. expect(view.get('filtersUsed')).to.equal(false);
  154. });
  155. it('should set "filtersUsed" to true if one or more values in "filterConditions" are not empty', function() {
  156. view.set('filterConditions', [{value:''}, {value:'lol'}]);
  157. view.filtersUsedCalc();
  158. expect(view.get('filtersUsed')).to.equal(true);
  159. });
  160. });
  161. describe('#nextPage', function() {
  162. beforeEach(function() {
  163. view = App.TableView.create(App.UserPref, {
  164. controller: Em.Object.create({}),
  165. displayLength: 10,
  166. startIndex: 1,
  167. content: d3.range(1, 100),
  168. filteredContent: d3.range(1, 100),
  169. endIndex: 10,
  170. filter: function() {}
  171. });
  172. });
  173. it('should set "startIndex" if "filteredContent.length is greater than "startIndex" + "displayLength"', function() {
  174. var oldStartIndex = view.get('startIndex');
  175. var displayLength = 50;
  176. view.set('displayLength', displayLength);
  177. view.nextPage();
  178. expect(view.get('startIndex')).to.equal(oldStartIndex + displayLength);
  179. });
  180. it('should not set "startIndex" if "filteredContent.length is equal to "startIndex" + "displayLength"', function() {
  181. var oldStartIndex = view.get('startIndex');
  182. var displayLength = 99;
  183. view.set('displayLength', displayLength);
  184. view.nextPage();
  185. expect(view.get('startIndex')).to.equal(oldStartIndex);
  186. });
  187. it('should not set "startIndex" if "filteredContent.length is less than "startIndex" + "displayLength"', function() {
  188. var oldStartIndex = view.get('startIndex');
  189. var displayLength = 100;
  190. view.set('displayLength', displayLength);
  191. view.nextPage();
  192. expect(view.get('startIndex')).to.equal(oldStartIndex);
  193. });
  194. });
  195. describe('#previousPage', function() {
  196. beforeEach(function() {
  197. view = App.TableView.create(App.UserPref, {
  198. controller: Em.Object.create({}),
  199. displayLength: 10,
  200. startIndex: 50,
  201. content: d3.range(1, 100),
  202. filteredContent: d3.range(1, 100),
  203. endIndex: 60,
  204. filter: function() {}
  205. });
  206. });
  207. it('should set "startIndex" to 1', function() {
  208. var displayLength = 50;
  209. view.set('displayLength', displayLength);
  210. view.previousPage();
  211. expect(view.get('startIndex')).to.equal(1);
  212. });
  213. it('should not set "startIndex" to 40', function() {
  214. view.set('startIndex', 50);
  215. var displayLength = 10;
  216. view.set('displayLength', displayLength);
  217. view.previousPage();
  218. expect(view.get('startIndex')).to.equal(40);
  219. });
  220. });
  221. describe("#showFilteredContent", function() {
  222. beforeEach(function() {
  223. view = App.TableView.create({});
  224. });
  225. it('hide clear filters link', function () {
  226. view.set('filterConditions', []);
  227. expect(view.get('showFilteredContent')).to.be.false;
  228. });
  229. it('shows clear filters link', function () {
  230. view.set('filterConditions', [{value: "1"}]);
  231. expect(view.get('showFilteredContent')).to.be.true;
  232. });
  233. it('shows clear filters link for array filter', function () {
  234. view.set('filterConditions', [{value: ["1", "2"]}]);
  235. expect(view.get('showFilteredContent')).to.be.true;
  236. });
  237. });
  238. describe('#filter', function () {
  239. var cases = [
  240. {
  241. filterConditions: [
  242. {
  243. iColumn: 1,
  244. type: 'string',
  245. value: 'v0'
  246. }
  247. ],
  248. content: [
  249. Em.Object.create({
  250. c0: 'v0'
  251. }),
  252. Em.Object.create({
  253. c1: 'v1'
  254. })
  255. ],
  256. filteredContent: [],
  257. title: 'no matches'
  258. },
  259. {
  260. filterConditions: [
  261. {
  262. iColumn: 0,
  263. type: 'string',
  264. value: 'v1'
  265. }
  266. ],
  267. content: [
  268. Em.Object.create({
  269. c0: 'v1'
  270. }),
  271. Em.Object.create({
  272. c0: 'v11'
  273. }),
  274. Em.Object.create({
  275. c1: 'v01'
  276. })
  277. ],
  278. filteredContent: [
  279. Em.Object.create({
  280. c0: 'v1'
  281. }),
  282. Em.Object.create({
  283. c0: 'v11'
  284. })
  285. ],
  286. title: 'matches present'
  287. },
  288. {
  289. filterConditions: [],
  290. content: [
  291. Em.Object.create({
  292. c0: 'v0'
  293. }),
  294. Em.Object.create({
  295. c1: 'v1'
  296. })
  297. ],
  298. filteredContent: [
  299. Em.Object.create({
  300. c0: 'v0'
  301. }),
  302. Em.Object.create({
  303. c1: 'v1'
  304. })
  305. ],
  306. title: 'no filter conditions'
  307. },
  308. {
  309. filterConditions: [],
  310. filteredContent: [],
  311. title: 'no filter conditions, no content'
  312. }
  313. ];
  314. beforeEach(function () {
  315. view = App.TableView.create({
  316. colPropAssoc: ['c0', 'c1']
  317. });
  318. });
  319. cases.forEach(function (item) {
  320. it(item.title, function () {
  321. view.setProperties({
  322. filterConditions: item.filterConditions,
  323. content: item.content
  324. });
  325. view.filter();
  326. expect(view.get('filteredContent')).to.eql(item.filteredContent);
  327. });
  328. });
  329. });
  330. });