configs_collection_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/configs_collection');
  20. describe('App.configsCollection', function () {
  21. var configsCollection;
  22. beforeEach(function () {
  23. configsCollection = Em.Object.create(App.configsCollection);
  24. sinon.spy(Em, 'assert');
  25. });
  26. afterEach(function () {
  27. Em.assert.restore();
  28. });
  29. describe('#add', function () {
  30. var throwCases = [
  31. {
  32. obj: undefined,
  33. collection: [],
  34. isError: true,
  35. title: 'null passed'
  36. },
  37. {
  38. obj: {},
  39. collection: [],
  40. isError: true,
  41. title: 'no id passed'
  42. },
  43. {
  44. obj: undefined,
  45. collection: [],
  46. isError: true,
  47. title: 'no item passed'
  48. }
  49. ];
  50. var cases = [
  51. {
  52. collection: [],
  53. isError: false,
  54. title: 'initial state'
  55. },
  56. {
  57. obj: {
  58. id: 1,
  59. name: 'n10'
  60. },
  61. collection: [
  62. {
  63. id: 1,
  64. name: 'n10'
  65. }
  66. ],
  67. mapItem: {
  68. id: 1,
  69. name: 'n10'
  70. },
  71. isError: false,
  72. title: 'new item'
  73. },
  74. {
  75. obj: {
  76. id: 1,
  77. name: 'n11'
  78. },
  79. collection: [
  80. {
  81. id: 1,
  82. name: 'n10'
  83. }
  84. ],
  85. mapItem: {
  86. id: 1,
  87. name: 'n11'
  88. },
  89. isError: false,
  90. title: 'duplicate id'
  91. },
  92. {
  93. obj: {
  94. id: '1',
  95. name: 'n12'
  96. },
  97. collection: [
  98. {
  99. id: 1,
  100. name: 'n10'
  101. }
  102. ],
  103. mapItem: {
  104. id: '1',
  105. name: 'n12'
  106. },
  107. isError: false,
  108. title: 'duplicate id, key name conversion'
  109. }
  110. ];
  111. throwCases.forEach(function (item) {
  112. it(item.title, function () {
  113. expect(function () {configsCollection.add(item.obj);}).to.throw(Error);
  114. });
  115. });
  116. cases.forEach(function (item) {
  117. describe(item.title, function () {
  118. beforeEach(function () {
  119. if (item.hasOwnProperty('obj')) {
  120. configsCollection.add(item.obj);
  121. }
  122. });
  123. it('configs array', function () {
  124. expect(configsCollection.getAll()).to.eql(item.collection);
  125. });
  126. if (item.obj && item.obj.id) {
  127. it('configs map', function () {
  128. expect(configsCollection.getConfig(item.obj.id)).to.eql(item.mapItem);
  129. });
  130. }
  131. });
  132. });
  133. });
  134. describe('#getConfig', function () {
  135. var throwCases = [
  136. {
  137. result: undefined,
  138. isError: true,
  139. title: 'no id passed'
  140. },
  141. {
  142. id: null,
  143. result: undefined,
  144. isError: true,
  145. title: 'invalid id passed'
  146. }
  147. ];
  148. var cases = [
  149. {
  150. id: 1,
  151. result: {
  152. id: 1
  153. },
  154. isError: false,
  155. title: 'existing item'
  156. },
  157. {
  158. id: 1,
  159. result: {
  160. id: 1
  161. },
  162. isError: false,
  163. title: 'existing item, key name conversion'
  164. },
  165. {
  166. id: 2,
  167. result: undefined,
  168. isError: false,
  169. title: 'item doesn\'t exist'
  170. }
  171. ];
  172. throwCases.forEach(function (item) {
  173. it(item.title, function () {
  174. configsCollection.add({
  175. id: 1
  176. });
  177. expect(function () {configsCollection.getConfig(item.id);}).to.throw(Error);
  178. });
  179. });
  180. cases.forEach(function (item) {
  181. describe(item.title, function () {
  182. var result;
  183. beforeEach(function () {
  184. configsCollection.add({
  185. id: 1
  186. });
  187. result = configsCollection.getConfig(item.id);
  188. });
  189. it('returned value', function () {
  190. expect(result).to.eql(item.result);
  191. });
  192. });
  193. });
  194. });
  195. describe('#getConfigByName', function () {
  196. var configIds = ['n0_f0', 'n1_f1'];
  197. var throwCases = [
  198. {
  199. fileName: 'f0',
  200. result: undefined,
  201. isError: true,
  202. title: 'no name passed'
  203. },
  204. {
  205. name: 'n0',
  206. result: undefined,
  207. isError: true,
  208. title: 'no filename passed'
  209. }
  210. ];
  211. var cases = [
  212. {
  213. name: 'n0',
  214. fileName: 'f0',
  215. result: {
  216. id: 'n0_f0'
  217. },
  218. isError: false,
  219. title: 'existing item'
  220. },
  221. {
  222. name: 'n0',
  223. fileName: 'f1',
  224. result: undefined,
  225. isError: false,
  226. title: 'not existing item'
  227. }
  228. ];
  229. beforeEach(function () {
  230. sinon.stub(App.config, 'configId', function (name, fileName) {
  231. return name + '_' + fileName;
  232. });
  233. });
  234. afterEach(function () {
  235. configsCollection.clearAll();
  236. App.config.configId.restore();
  237. });
  238. throwCases.forEach(function (item) {
  239. it(item.title, function () {
  240. expect(function () {configsCollection.getConfigByName(item.name, item.fileName);}).to.throw(Error);
  241. });
  242. });
  243. cases.forEach(function (item) {
  244. describe(item.title, function () {
  245. var result;
  246. beforeEach(function () {
  247. configIds.forEach(function (id) {
  248. configsCollection.add({
  249. id: id
  250. });
  251. });
  252. result = configsCollection.getConfigByName(item.name, item.fileName);
  253. });
  254. it('returned value', function () {
  255. expect(result).to.eql(item.result);
  256. });
  257. });
  258. });
  259. });
  260. describe('#getAll', function () {
  261. var configs = [
  262. {
  263. id: 'c0'
  264. },
  265. {
  266. id: 'c1'
  267. }
  268. ];
  269. beforeEach(function () {
  270. configsCollection.clearAll();
  271. });
  272. it('should return all configs', function () {
  273. configs.forEach(function (item) {
  274. configsCollection.add(item);
  275. });
  276. expect(configsCollection.getAll()).to.eql(configs);
  277. });
  278. });
  279. describe('#clearAll', function () {
  280. beforeEach(function () {
  281. configsCollection.add({
  282. id: 'c0'
  283. });
  284. configsCollection.clearAll();
  285. });
  286. it('should clear configs array', function () {
  287. expect(configsCollection.getAll()).to.have.length(0);
  288. });
  289. it('should clear configs map', function () {
  290. expect(configsCollection.getConfig('c0')).to.be.undefined;
  291. });
  292. });
  293. });