manage_alert_notifications_view_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. var view;
  20. describe('App.ManageAlertNotificationsView', function () {
  21. beforeEach(function () {
  22. view = App.ManageAlertNotificationsView.create({
  23. controller: Em.Object.create()
  24. });
  25. });
  26. describe('#buttonObserver', function () {
  27. Em.A([
  28. {
  29. isOperator: false,
  30. selectedAlertNotification: {id: 1},
  31. m: 'some alert notification is selected and user is an admin',
  32. p: {
  33. isAddButtonDisabled: true,
  34. isEditButtonDisabled: true,
  35. isRemoveButtonDisabled: true,
  36. isDuplicateButtonDisabled: true
  37. },
  38. e: {
  39. isAddButtonDisabled: false,
  40. isEditButtonDisabled: false,
  41. isRemoveButtonDisabled: false,
  42. isDuplicateButtonDisabled: false
  43. }
  44. },
  45. {
  46. isOperator: true,
  47. selectedAlertNotification: {id: 1},
  48. m: 'some alert notification is selected and user is a non-admin operator',
  49. p: {
  50. isAddButtonDisabled: true,
  51. isEditButtonDisabled: true,
  52. isRemoveButtonDisabled: true,
  53. isDuplicateButtonDisabled: true
  54. },
  55. e: {
  56. isAddButtonDisabled: true,
  57. isEditButtonDisabled: true,
  58. isRemoveButtonDisabled: true,
  59. isDuplicateButtonDisabled: true
  60. }
  61. },
  62. {
  63. isOperator: false,
  64. selectedAlertNotification: null,
  65. m: 'some alert notification is not selected and user is an admin',
  66. p: {
  67. isAddButtonDisabled: true,
  68. isEditButtonDisabled: false,
  69. isRemoveButtonDisabled: false,
  70. isDuplicateButtonDisabled: false
  71. },
  72. e: {
  73. isAddButtonDisabled: true,
  74. isEditButtonDisabled: true,
  75. isRemoveButtonDisabled: true,
  76. isDuplicateButtonDisabled: true
  77. }
  78. },
  79. {
  80. isOperator: true,
  81. selectedAlertNotification: null,
  82. m: 'some alert notification is not selected and user is a non-admin operator',
  83. p: {
  84. isAddButtonDisabled: true,
  85. isEditButtonDisabled: false,
  86. isRemoveButtonDisabled: false,
  87. isDuplicateButtonDisabled: false
  88. },
  89. e: {
  90. isAddButtonDisabled: true,
  91. isEditButtonDisabled: true,
  92. isRemoveButtonDisabled: true,
  93. isDuplicateButtonDisabled: true
  94. }
  95. }
  96. ]).forEach(function (test) {
  97. describe(test.m, function () {
  98. beforeEach(function () {
  99. view.setProperties(test.p);
  100. view.set('controller.selectedAlertNotification', test.selectedAlertNotification);
  101. App.isOperator = test.isOperator;
  102. view.buttonObserver();
  103. });
  104. Em.keys(test.e).forEach(function (k) {
  105. it(k, function () {
  106. expect(view.get(k)).to.equal(test.e[k]);
  107. });
  108. });
  109. });
  110. });
  111. });
  112. describe('#showEmailDetails', function () {
  113. Em.A([
  114. {
  115. selectedAlertNotification: {type: 'SNMP'},
  116. e: false
  117. },
  118. {
  119. selectedAlertNotification: {type: 'EMAIL'},
  120. e: true
  121. }
  122. ]).forEach(function (test, i) {
  123. it('test ' + (i + 1), function () {
  124. view.set('controller.selectedAlertNotification', test.selectedAlertNotification);
  125. expect(view.get('showEmailDetails')).to.equal(test.e);
  126. });
  127. });
  128. });
  129. describe('#showSNMPDetails', function () {
  130. Em.A([
  131. {
  132. selectedAlertNotification: {type: 'SNMP'},
  133. e: true
  134. },
  135. {
  136. selectedAlertNotification: {type: 'EMAIL'},
  137. e: false
  138. }
  139. ]).forEach(function (test, i) {
  140. it('test ' + (i + 1), function () {
  141. view.set('controller.selectedAlertNotification', test.selectedAlertNotification);
  142. expect(view.get('showSNMPDetails')).to.equal(test.e);
  143. });
  144. });
  145. });
  146. describe("#selectedAlertNotificationGroups", function () {
  147. it("should contain group names", function () {
  148. view.set('controller', Em.Object.create({
  149. selectedAlertNotification: Em.Object.create({
  150. groups: [
  151. Em.Object.create({
  152. displayName: 'g1'
  153. }),
  154. Em.Object.create({
  155. displayName: 'g2'
  156. })
  157. ]
  158. })
  159. }));
  160. expect(view.get('selectedAlertNotificationGroups')).to.equal('g1, g2');
  161. });
  162. });
  163. describe("#email", function () {
  164. it("should return ambari.dispatch.recipients", function () {
  165. view.set('controller', Em.Object.create({
  166. selectedAlertNotification: Em.Object.create({
  167. properties: {
  168. 'ambari.dispatch.recipients': 1
  169. }
  170. })
  171. }));
  172. expect(view.get('email')).to.equal(1);
  173. });
  174. });
  175. describe("#severities", function () {
  176. it("should return list of states", function () {
  177. view.set('controller', Em.Object.create({
  178. selectedAlertNotification: Em.Object.create({
  179. alertStates: ['st1', 'st2']
  180. })
  181. }));
  182. expect(view.get('severities')).to.equal('st1, st2');
  183. });
  184. });
  185. describe("#onAlertNotificationSelect()", function () {
  186. beforeEach(function () {
  187. view.removeObserver('selectedAlertNotification', view, 'onAlertNotificationSelect');
  188. view.set('controller', Em.Object.create({selectedAlertNotification: null}));
  189. });
  190. it("selectedAlertNotification is null", function () {
  191. view.set('selectedAlertNotification', null);
  192. view.onAlertNotificationSelect();
  193. expect(view.get('selectedAlertNotification')).to.be.null;
  194. expect(view.get('controller.selectedAlertNotification')).to.be.null;
  195. });
  196. it("selectedAlertNotification is empty array", function () {
  197. view.set('selectedAlertNotification', []);
  198. view.onAlertNotificationSelect();
  199. expect(view.get('selectedAlertNotification')).to.be.empty;
  200. expect(view.get('controller.selectedAlertNotification')).to.be.null;
  201. });
  202. it("selectedAlertNotification is array with single element", function () {
  203. view.set('selectedAlertNotification', [1]);
  204. view.onAlertNotificationSelect();
  205. expect(view.get('selectedAlertNotification')).to.eql([1]);
  206. expect(view.get('controller.selectedAlertNotification')).to.equal(1);
  207. });
  208. it("selectedAlertNotification is array with two elements", function () {
  209. view.set('selectedAlertNotification', [1, 2]);
  210. view.onAlertNotificationSelect();
  211. expect(view.get('selectedAlertNotification')).to.equal(2);
  212. expect(view.get('controller.selectedAlertNotification')).to.equal(2);
  213. });
  214. });
  215. describe("#willInsertElement()", function () {
  216. beforeEach(function () {
  217. view.set('controller', Em.Object.create({loadAlertNotifications: Em.K}));
  218. sinon.spy(view.get('controller'), 'loadAlertNotifications');
  219. });
  220. afterEach(function () {
  221. view.get('controller').loadAlertNotifications.restore();
  222. });
  223. it("loadAlertNotifications should be called", function () {
  224. view.willInsertElement();
  225. expect(view.get('controller').loadAlertNotifications.calledOnce).to.be.true;
  226. });
  227. });
  228. describe("#didInsertElement()", function () {
  229. beforeEach(function () {
  230. sinon.stub(view, 'onLoad');
  231. });
  232. afterEach(function () {
  233. view.onLoad.restore();
  234. });
  235. it("loadAlertNotifications should be called", function () {
  236. view.didInsertElement();
  237. expect(view.onLoad.calledOnce).to.be.true;
  238. });
  239. });
  240. describe("#onLoad()", function () {
  241. beforeEach(function () {
  242. view.removeObserver('controller.isLoaded', view, 'onLoad');
  243. view.set('controller', Em.Object.create());
  244. sinon.stub(view, 'buttonObserver');
  245. sinon.stub(Em.run, 'later', function (context, callback) {
  246. callback();
  247. });
  248. sinon.stub(App, 'tooltip');
  249. this.clock = sinon.useFakeTimers();
  250. });
  251. afterEach(function () {
  252. view.buttonObserver.restore();
  253. Em.run.later.restore();
  254. App.tooltip.restore();
  255. this.clock.restore();
  256. });
  257. it("controller.isLoaded is false", function () {
  258. view.set('controller.isLoaded', false);
  259. view.onLoad();
  260. expect(Em.run.later.called).to.be.false;
  261. });
  262. describe("controller.isLoaded is true, alertNotifications is null", function () {
  263. beforeEach(function () {
  264. view.set('controller.isLoaded', true);
  265. view.set('controller.alertNotifications', null);
  266. });
  267. it("Em.run.later should be called", function () {
  268. view.onLoad();
  269. expect(Em.run.later.calledOnce).to.be.true;
  270. });
  271. it("App.tooltip should be called twice", function () {
  272. view.onLoad();
  273. this.clock.tick(50);
  274. expect(App.tooltip.calledTwice).to.be.true;
  275. });
  276. it("selectedAlertNotification should be null", function () {
  277. view.onLoad();
  278. expect(view.get('selectedAlertNotification')).to.be.null;
  279. });
  280. it("isAddButtonDisabled should be true", function () {
  281. view.set('isAddButtonDisabled', true);
  282. App.isOperator = true;
  283. view.onLoad();
  284. expect(view.get('isAddButtonDisabled')).to.be.true;
  285. });
  286. it("isAddButtonDisabled should be false", function () {
  287. view.set('isAddButtonDisabled', true);
  288. App.isOperator = false;
  289. view.onLoad();
  290. expect(view.get('isAddButtonDisabled')).to.be.false;
  291. });
  292. });
  293. describe("controller.isLoaded is true, alertNotifications is array", function () {
  294. beforeEach(function () {
  295. view.set('controller.isLoaded', true);
  296. view.set('controller.alertNotifications', [{}]);
  297. });
  298. it("Em.run.later should be called", function () {
  299. view.onLoad();
  300. expect(Em.run.later.calledOnce).to.be.true;
  301. });
  302. it("App.tooltip should be called twice", function () {
  303. view.onLoad();
  304. this.clock.tick(50);
  305. expect(App.tooltip.calledTwice).to.be.true;
  306. });
  307. it("selectedAlertNotification should be object", function () {
  308. view.onLoad();
  309. expect(view.get('selectedAlertNotification')).to.eql({});
  310. });
  311. it("buttonObserver should be called", function () {
  312. view.onLoad();
  313. expect(view.buttonObserver.calledOnce).to.be.true;
  314. });
  315. });
  316. });
  317. });