ajax_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/ajax/ajax');
  20. describe('App.ajax', function() {
  21. beforeEach(function() {
  22. App.set('apiPrefix', '/api/v1');
  23. App.set('clusterName', 'tdk');
  24. });
  25. describe('#send', function() {
  26. beforeEach(function() {
  27. sinon.spy($, 'ajax');
  28. });
  29. afterEach(function() {
  30. $.ajax.restore();
  31. });
  32. it('Without sender', function() {
  33. expect(App.ajax.send({})).to.equal(null);
  34. expect($.ajax.called).to.equal(false);
  35. });
  36. it('Invalid config.name', function() {
  37. expect(App.ajax.send({name:'fake_name', sender: this})).to.equal(null);
  38. expect($.ajax.called).to.equal(false);
  39. });
  40. it('With proper data', function() {
  41. App.ajax.send({name: 'router.logoff', sender: this});
  42. expect($.ajax.calledOnce).to.equal(true);
  43. });
  44. });
  45. describe('#formatUrl', function() {
  46. var tests = [
  47. {
  48. url: null,
  49. data: {},
  50. e: null,
  51. m: 'url is null'
  52. },
  53. {
  54. url: 'site/{param}',
  55. data: null,
  56. e: 'site/',
  57. m: 'url with one param, but data is null'
  58. },
  59. {
  60. url: 'clean_url',
  61. data: {},
  62. e: 'clean_url',
  63. m: 'url without placeholders'
  64. },
  65. {
  66. url: 'site/{param}',
  67. data: {},
  68. e: 'site/',
  69. m: 'url with param, but there is no such param in the data'
  70. },
  71. {
  72. url: 'site/{param}/{param}',
  73. data: {param: 123},
  74. e: 'site/123/123',
  75. m: 'url with param which appears two times'
  76. }
  77. ];
  78. tests.forEach(function(test) {
  79. it(test.m, function() {
  80. var r = App.ajax.fakeFormatUrl(test.url, test.data);
  81. expect(r).to.equal(test.e);
  82. });
  83. });
  84. });
  85. describe('Check "real" property for each url object', function() {
  86. var names = App.ajax.fakeGetUrlNames();
  87. names.forEach(function(name) {
  88. it(name, function() {
  89. var url = App.ajax.fakeGetUrl(name);
  90. expect(url.real).to.be.a('string');
  91. });
  92. });
  93. });
  94. describe('#formatRequest', function() {
  95. it('App.testMode = true', function() {
  96. sinon.stub(App, 'get', function(k) {
  97. if ('testMode' === k) return true;
  98. return Em.get(App, k);
  99. });
  100. var r = App.ajax.fakeFormatRequest({real:'/', mock: '/some_url'}, {});
  101. expect(r.type).to.equal('GET');
  102. expect(r.url).to.equal('/some_url');
  103. expect(r.dataType).to.equal('json');
  104. App.get.restore();
  105. });
  106. var tests = [
  107. {
  108. urlObj: {
  109. real: '/real_url',
  110. format: function() {
  111. return {
  112. type: 'PUT'
  113. }
  114. }
  115. },
  116. data: {},
  117. m: '',
  118. e: {type: 'PUT', url: '/api/v1/real_url'}
  119. }
  120. ];
  121. tests.forEach(function(test) {
  122. it(test.m, function() {
  123. sinon.stub(App, 'get', function(k) {
  124. if ('testMode' === k) return false;
  125. return Em.get(App, k);
  126. });
  127. var r = App.ajax.fakeFormatRequest(test.urlObj, test.data);
  128. expect(r.type).to.equal(test.e.type);
  129. expect(r.url).to.equal(test.e.url);
  130. App.get.restore();
  131. });
  132. });
  133. });
  134. });