views_controller_test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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('controllers/main/views_controller');
  20. var testHelpers = require('test/helpers');
  21. var mainViewsController;
  22. describe('MainViewsController', function () {
  23. beforeEach(function () {
  24. mainViewsController = App.MainViewsController.create();
  25. });
  26. describe('#loadAmbariViews()', function () {
  27. beforeEach(function () {
  28. this.stub = sinon.stub(App.router, 'get');
  29. });
  30. afterEach(function () {
  31. App.router.get.restore();
  32. });
  33. it('should load views if the user is logged in', function () {
  34. this.stub.withArgs('loggedIn').returns(true);
  35. mainViewsController.loadAmbariViews();
  36. var args = testHelpers.findAjaxRequest('name', 'views.info');
  37. expect(args).to.exists;
  38. });
  39. it('should not load views if the user is not logged in', function () {
  40. this.stub.withArgs('loggedIn').returns(false);
  41. mainViewsController.loadAmbariViews();
  42. var args = testHelpers.findAjaxRequest('name', 'views.info');
  43. expect(args).to.not.exists;
  44. })
  45. });
  46. describe('#loadAmbariViewsSuccess()', function () {
  47. it('data has items', function () {
  48. mainViewsController.loadAmbariViewsSuccess({items: [{}]});
  49. var args = testHelpers.findAjaxRequest('name', 'views.instances');
  50. expect(args).to.exists;
  51. });
  52. it('data is empty', function () {
  53. mainViewsController.loadAmbariViewsSuccess({items: []});
  54. var args = testHelpers.findAjaxRequest('name', 'views.instances');
  55. expect(args).to.not.exists;
  56. expect(mainViewsController.get('ambariViews')).to.be.empty;
  57. expect(mainViewsController.get('isDataLoaded')).to.be.true;
  58. });
  59. });
  60. describe('#loadAmbariViewsError()', function () {
  61. it('ambariViews should be empty', function () {
  62. mainViewsController.loadAmbariViewsError();
  63. expect(mainViewsController.get('ambariViews')).to.be.empty;
  64. expect(mainViewsController.get('isDataLoaded')).to.be.true;
  65. });
  66. });
  67. describe("#loadViewInstancesSuccess()", function () {
  68. var data = {
  69. items: [
  70. {
  71. versions: [
  72. {
  73. instances: [
  74. {
  75. ViewInstanceInfo: {
  76. icon_path: 'icon_path1',
  77. label: 'label1',
  78. visible: true,
  79. version: '1.0',
  80. description: 'desc1',
  81. viewName: 'view_name1',
  82. instanceName: 'instance_name1',
  83. context_path: 'path1'
  84. }
  85. }
  86. ]
  87. }
  88. ]
  89. }
  90. ]
  91. };
  92. it("should parse view instance data", function () {
  93. mainViewsController.loadViewInstancesSuccess(data);
  94. expect(JSON.parse(JSON.stringify(mainViewsController.get('ambariViews')))).to.be.eql([{
  95. "iconPath": "icon_path1",
  96. "label": "label1",
  97. "visible": true,
  98. "version": "1.0",
  99. "description": "desc1",
  100. "href": "path1/"
  101. }]);
  102. expect(mainViewsController.get('isDataLoaded')).to.be.true;
  103. });
  104. });
  105. describe('#loadViewInstancesError()', function () {
  106. it('ambariViews should be empty', function () {
  107. mainViewsController.loadViewInstancesError();
  108. expect(mainViewsController.get('ambariViews')).to.be.empty;
  109. expect(mainViewsController.get('isDataLoaded')).to.be.true;
  110. });
  111. });
  112. describe("#setView", function () {
  113. beforeEach(function () {
  114. sinon.stub(App.router, 'route');
  115. });
  116. afterEach(function () {
  117. App.router.route.restore();
  118. });
  119. it("no context", function () {
  120. mainViewsController.setView({});
  121. expect(App.router.route.called).to.be.false;
  122. });
  123. it("context exist", function () {
  124. mainViewsController.setView({
  125. context: {
  126. viewName: 'view1',
  127. version: '1',
  128. instanceName: 'instance1'
  129. }
  130. });
  131. expect(App.router.route.calledWith('main/views/view1/1/instance1')).to.be.true;
  132. });
  133. });
  134. });