add_view_test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 testHelpers = require('test/helpers');
  20. var view;
  21. describe('App.AddHostView', function () {
  22. beforeEach(function () {
  23. view = App.AddHostView.create({
  24. controller: Em.Object.create({
  25. getDBProperty: Em.K,
  26. setDBProperty: Em.K,
  27. content: Em.Object.create()
  28. })
  29. });
  30. });
  31. describe("#willInsertElement()", function() {
  32. beforeEach(function() {
  33. sinon.stub(view, 'loadHosts');
  34. this.mock = sinon.stub(view.get('controller'), 'getDBProperty');
  35. });
  36. afterEach(function() {
  37. view.loadHosts.restore();
  38. this.mock.restore();
  39. });
  40. it("hosts saved in DB", function() {
  41. this.mock.returns(['host1']);
  42. view.willInsertElement();
  43. expect(view.get('isLoaded')).to.be.true;
  44. expect(view.loadHosts.calledOnce).to.be.false;
  45. });
  46. it("hosts not saved in DB", function() {
  47. this.mock.returns(null);
  48. view.willInsertElement();
  49. expect(view.get('isLoaded')).to.be.false;
  50. expect(view.loadHosts.calledOnce).to.be.true;
  51. });
  52. });
  53. describe("#loadHosts()", function() {
  54. it("App.ajax.send should be called", function() {
  55. view.loadHosts();
  56. var args = testHelpers.filterAjaxRequests('name', 'hosts.confirmed');
  57. expect(args[0][0]).to.eql({
  58. name: 'hosts.confirmed',
  59. sender: view,
  60. data: {},
  61. success: 'loadHostsSuccessCallback',
  62. error: 'loadHostsErrorCallback'
  63. });
  64. });
  65. });
  66. describe("#loadHostsSuccessCallback()", function() {
  67. beforeEach(function() {
  68. sinon.stub(view.get('controller'), 'setDBProperty');
  69. });
  70. afterEach(function() {
  71. view.get('controller').setDBProperty.restore();
  72. });
  73. it("should save hosts to DB", function() {
  74. var response = {items: [
  75. {
  76. Hosts: {
  77. host_name: 'host1',
  78. cpu_count: 1,
  79. total_mem: 1024,
  80. disk_info: {}
  81. },
  82. host_components: [
  83. {
  84. component_name: 'C1'
  85. }
  86. ]
  87. }
  88. ]};
  89. view.loadHostsSuccessCallback(response);
  90. expect(view.get('isLoaded')).to.be.true;
  91. expect(view.get('controller').setDBProperty.calledWith('hosts', {
  92. host1: {
  93. name: 'host1',
  94. cpu: 1,
  95. memory: 1024,
  96. disk_info: {},
  97. bootStatus: "REGISTERED",
  98. isInstalled: true,
  99. hostComponents: [
  100. {
  101. component_name: 'C1'
  102. }
  103. ]
  104. }
  105. })).to.be.true;
  106. expect(view.get('controller.content.hosts')).to.eql({
  107. host1: {
  108. name: 'host1',
  109. cpu: 1,
  110. memory: 1024,
  111. disk_info: {},
  112. bootStatus: "REGISTERED",
  113. isInstalled: true,
  114. hostComponents: [
  115. {
  116. component_name: 'C1'
  117. }
  118. ]
  119. }
  120. });
  121. });
  122. });
  123. describe("#loadHostsErrorCallback()", function() {
  124. it("isLoaded should be set to true", function() {
  125. view.loadHostsErrorCallback();
  126. expect(view.get('isLoaded')).to.be.true;
  127. });
  128. });
  129. });