ajax_test.js 3.8 KB

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