appsSpec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2015 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('OC.Settings.Apps tests', function() {
  22. var Apps;
  23. beforeEach(function() {
  24. var $el = $('<div id="apps-list"></div>' +
  25. '<div id="apps-list-empty" class="hidden"></div>' +
  26. '<div id="app-template">' +
  27. // dummy template for testing
  28. '<div id="app-{{id}}" data-id="{{id}}" class="section">{{name}}</div>' +
  29. '</div>'
  30. );
  31. $('#testArea').append($el);
  32. Apps = OC.Settings.Apps;
  33. });
  34. afterEach(function() {
  35. Apps.State.apps = null;
  36. Apps.State.currentCategory = null;
  37. });
  38. describe('Filtering apps', function() {
  39. var oldApps;
  40. function loadApps(appList) {
  41. Apps.State.apps = appList;
  42. _.each(appList, function(appSpec) {
  43. Apps.renderApp(appSpec);
  44. });
  45. }
  46. function getResultsFromDom() {
  47. var results = [];
  48. $('#apps-list .section:not(.hidden)').each(function() {
  49. results.push($(this).attr('data-id'));
  50. });
  51. return results;
  52. }
  53. beforeEach(function() {
  54. loadApps([
  55. {id: 'appone', name: 'App One', description: 'The first app', author: 'author1', level: 200},
  56. {id: 'apptwo', name: 'App Two', description: 'The second app', author: 'author2', level: 100},
  57. {id: 'appthree', name: 'App Three', description: 'Third app', author: 'author3', level: 0},
  58. {id: 'somestuff', name: 'Some Stuff', description: 'whatever', author: 'author4', level: 0}
  59. ]);
  60. });
  61. it('returns no results when query does not match anything', function() {
  62. expect(getResultsFromDom().length).toEqual(4);
  63. expect($('#apps-list:not(.hidden)').length).toEqual(1);
  64. expect($('#apps-list-empty:not(.hidden)').length).toEqual(0);
  65. Apps.filter('absurdity');
  66. expect(getResultsFromDom().length).toEqual(0);
  67. expect($('#apps-list:not(.hidden)').length).toEqual(0);
  68. expect($('#apps-list-empty:not(.hidden)').length).toEqual(1);
  69. Apps.filter('');
  70. expect(getResultsFromDom().length).toEqual(4);
  71. expect($('#apps-list:not(.hidden)').length).toEqual(1);
  72. expect($('#apps-list-empty:not(.hidden)').length).toEqual(0);
  73. expect(getResultsFromDom().length).toEqual(4);
  74. });
  75. it('returns relevant results when query matches name', function() {
  76. expect($('#apps-list:not(.hidden)').length).toEqual(1);
  77. expect($('#apps-list-empty:not(.hidden)').length).toEqual(0);
  78. var results;
  79. Apps.filter('app');
  80. results = getResultsFromDom();
  81. expect(results.length).toEqual(3);
  82. expect(results[0]).toEqual('appone');
  83. expect(results[1]).toEqual('apptwo');
  84. expect(results[2]).toEqual('appthree');
  85. expect($('#apps-list:not(.hidden)').length).toEqual(1);
  86. expect($('#apps-list-empty:not(.hidden)').length).toEqual(0);
  87. });
  88. it('returns relevant result when query matches name', function() {
  89. var results;
  90. Apps.filter('TWO');
  91. results = getResultsFromDom();
  92. expect(results.length).toEqual(1);
  93. expect(results[0]).toEqual('apptwo');
  94. });
  95. it('returns relevant result when query matches description', function() {
  96. var results;
  97. Apps.filter('ever');
  98. results = getResultsFromDom();
  99. expect(results.length).toEqual(1);
  100. expect(results[0]).toEqual('somestuff');
  101. });
  102. it('returns relevant results when query matches author name', function() {
  103. var results;
  104. Apps.filter('author');
  105. results = getResultsFromDom();
  106. expect(results.length).toEqual(4);
  107. expect(results[0]).toEqual('appone');
  108. expect(results[1]).toEqual('apptwo');
  109. expect(results[2]).toEqual('appthree');
  110. expect(results[3]).toEqual('somestuff');
  111. });
  112. it('returns relevant result when query matches author name', function() {
  113. var results;
  114. Apps.filter('thor3');
  115. results = getResultsFromDom();
  116. expect(results.length).toEqual(1);
  117. expect(results[0]).toEqual('appthree');
  118. });
  119. it('returns relevant result when query matches level name', function() {
  120. var results;
  121. Apps.filter('Offic');
  122. results = getResultsFromDom();
  123. expect(results.length).toEqual(1);
  124. expect(results[0]).toEqual('appone');
  125. });
  126. it('returns relevant result when query matches level name', function() {
  127. var results;
  128. Apps.filter('Appro');
  129. results = getResultsFromDom();
  130. expect(results.length).toEqual(1);
  131. expect(results[0]).toEqual('apptwo');
  132. });
  133. it('returns relevant result when query matches level name', function() {
  134. var results;
  135. Apps.filter('Exper');
  136. results = getResultsFromDom();
  137. expect(results.length).toEqual(2);
  138. expect(results[0]).toEqual('appthree');
  139. expect(results[1]).toEqual('somestuff');
  140. });
  141. });
  142. describe('loading categories', function() {
  143. var suite = this;
  144. beforeEach( function(){
  145. suite.server = sinon.fakeServer.create();
  146. });
  147. afterEach( function(){
  148. suite.server.restore();
  149. });
  150. function getResultsFromDom() {
  151. var results = [];
  152. $('#apps-list .section:not(.hidden)').each(function() {
  153. results.push($(this).attr('data-id'));
  154. });
  155. return results;
  156. }
  157. it('sorts all applications using the level', function() {
  158. Apps.loadCategory('TestId');
  159. suite.server.requests[0].respond(
  160. 200,
  161. {
  162. 'Content-Type': 'application/json'
  163. },
  164. JSON.stringify({
  165. apps: [
  166. {
  167. id: 'foo',
  168. name: 'Foo app',
  169. description: 'Hello',
  170. level: 0,
  171. author: 'foo'
  172. },
  173. {
  174. id: 'alpha',
  175. name: 'Alpha app',
  176. description: 'Hello',
  177. level: 300,
  178. author: ['alpha', 'beta']
  179. },
  180. {
  181. id: 'nolevel',
  182. name: 'No level',
  183. description: 'Hello',
  184. author: 'bar'
  185. },
  186. {
  187. id: 'zork',
  188. name: 'Some famous adventure game',
  189. description: 'Hello',
  190. level: 200,
  191. author: 'baz'
  192. },
  193. {
  194. id: 'delta',
  195. name: 'Mathematical symbol',
  196. description: 'Hello',
  197. level: 200,
  198. author: 'foobar'
  199. }
  200. ]
  201. })
  202. );
  203. var results = getResultsFromDom();
  204. expect(results.length).toEqual(5);
  205. expect(results).toEqual(['alpha', 'delta', 'zork', 'foo', 'nolevel']);
  206. expect(OC.Settings.Apps.State.apps).toEqual({
  207. 'foo': {
  208. id: 'foo',
  209. name: 'Foo app',
  210. description: 'Hello',
  211. level: 0,
  212. author: 'foo'
  213. },
  214. 'alpha': {
  215. id: 'alpha',
  216. name: 'Alpha app',
  217. description: 'Hello',
  218. level: 300,
  219. author: ['alpha', 'beta']
  220. },
  221. 'nolevel': {
  222. id: 'nolevel',
  223. name: 'No level',
  224. description: 'Hello',
  225. author: 'bar'
  226. },
  227. 'zork': {
  228. id: 'zork',
  229. name: 'Some famous adventure game',
  230. description: 'Hello',
  231. level: 200,
  232. author: 'baz',
  233. },
  234. 'delta': {
  235. id: 'delta',
  236. name: 'Mathematical symbol',
  237. description: 'Hello',
  238. level: 200,
  239. author: 'foobar'
  240. }
  241. });
  242. });
  243. });
  244. });