ajax-service-test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import Ember from 'ember';
  2. import DS from 'ember-data';
  3. import { module, test } from 'qunit';
  4. import startApp from '../helpers/start-app';
  5. var application, container, service;
  6. module('Acceptance: Ajax Service', {
  7. beforeEach: function() {
  8. application = startApp();
  9. container = application.__container__;
  10. service = container.lookup('service:ajax');
  11. },
  12. afterEach: function() {
  13. Ember.run(application, 'destroy');
  14. Ember.$.mockjax.clear();
  15. }
  16. });
  17. test('unpluralizeUrlProcedure fixes urls', function(assert) {
  18. assert.expect(3);
  19. var url = service.unpluralizeUrlProcedure('/some-words/', 'some-word');
  20. assert.equal(url, '/some-word/');
  21. url = service.unpluralizeUrlProcedure('/some-words/', 'someWord');
  22. assert.equal(url, '/some-word/');
  23. url = service.unpluralizeUrlProcedure('/model/some-words/', 'some-word');
  24. assert.equal(url, '/model/some-word/');
  25. });
  26. test('buildProcedureURL builds valid url', function(assert) {
  27. assert.expect(4);
  28. var adapter = container.lookup('adapter:application');
  29. var url = service.buildProcedureURL(adapter, 'close');
  30. assert.equal(url, '/api/close/');
  31. url = service.buildProcedureURL(adapter, 'close-thread');
  32. assert.equal(url, '/api/close-thread/');
  33. url = service.buildProcedureURL(adapter, 'closeThread');
  34. assert.equal(url, '/api/close-thread/');
  35. url = service.buildProcedureURL(adapter, 'thread/1/close');
  36. assert.equal(url, '/api/thread/1/close/');
  37. });
  38. test('successful get', function(assert) {
  39. assert.expect(2);
  40. var done = assert.async();
  41. Ember.$.mockjax({
  42. url: '/api/some-value/',
  43. status: 200,
  44. responseText: {
  45. 'detail': 'it works'
  46. }
  47. });
  48. service.get('some-value').then(function(data) {
  49. assert.equal(data.detail, 'it works');
  50. }, function() {
  51. assert.ok(false, 'get call should pass');
  52. }).finally(function() {
  53. assert.ok(true, 'finally() was called');
  54. done();
  55. });
  56. });
  57. test('failed post', function(assert) {
  58. assert.expect(2);
  59. var done = assert.async();
  60. Ember.$.mockjax({
  61. url: '/api/some-value/',
  62. status: 400,
  63. responseText: {
  64. 'detail': 'it fails'
  65. }
  66. });
  67. service.get('some-value').then(function(data) {
  68. assert.ok(false, 'get call should fail');
  69. }, function(jqXHR) {
  70. var rejection = jqXHR.responseJSON;
  71. assert.equal(rejection.detail, 'it fails');
  72. }).finally(function() {
  73. assert.ok(true, 'finally() was called');
  74. done();
  75. });
  76. });
  77. test('successful model prop get', function(assert) {
  78. assert.expect(2);
  79. var done = assert.async();
  80. Ember.$.mockjax({
  81. url: '/api/legal-pages/privacy-policy/',
  82. status: 200,
  83. responseText: {
  84. 'id': 'privacy-policy',
  85. 'title': '',
  86. 'link': '',
  87. 'body': ''
  88. }
  89. });
  90. Ember.$.mockjax({
  91. url: '/api/legal-pages/privacy-policy/some-rpc/',
  92. status: 200,
  93. responseText: {
  94. 'detail': 'it works'
  95. }
  96. });
  97. Ember.run(function() {
  98. var store = container.lookup('store:main');
  99. store.find('legal-page', 'privacy-policy').then(function(record) {
  100. service.get(record, 'some-rpc').then(function(data) {
  101. assert.equal(data.detail, 'it works');
  102. }, function() {
  103. assert.ok(false, 'model get call should pass');
  104. }).finally(function() {
  105. assert.ok(true, 'finally() was called');
  106. done();
  107. });
  108. });
  109. });
  110. });
  111. test('failed model prop get', function(assert) {
  112. assert.expect(2);
  113. var done = assert.async();
  114. Ember.$.mockjax({
  115. url: '/api/legal-pages/privacy-policy/',
  116. status: 200,
  117. responseText: {
  118. 'id': 'privacy-policy',
  119. 'title': '',
  120. 'link': '',
  121. 'body': ''
  122. }
  123. });
  124. Ember.$.mockjax({
  125. url: '/api/legal-pages/privacy-policy/some-rpc/',
  126. status: 400,
  127. responseText: {
  128. 'detail': 'it failed'
  129. }
  130. });
  131. Ember.run(function() {
  132. var store = container.lookup('store:main');
  133. store.find('legal-page', 'privacy-policy').then(function(record) {
  134. service.get(record, 'some-rpc').then(function() {
  135. assert.ok(false, 'get call should fail');
  136. }, function(jqXHR) {
  137. var rejection = jqXHR.responseJSON;
  138. assert.equal(rejection.detail, 'it failed');
  139. }).finally(function() {
  140. assert.ok(true, 'finally() was called');
  141. done();
  142. });
  143. });
  144. });
  145. });
  146. test('successful post', function(assert) {
  147. assert.expect(2);
  148. var done = assert.async();
  149. Ember.$.mockjax({
  150. url: '/api/some-rpc/',
  151. status: 200,
  152. responseText: {
  153. 'detail': 'it works'
  154. }
  155. });
  156. service.post('some-rpc').then(function(data) {
  157. assert.equal(data.detail, 'it works');
  158. }, function() {
  159. assert.ok(false, 'rpc call should pass');
  160. }).finally(function() {
  161. assert.ok(true, 'finally() was called');
  162. done();
  163. });
  164. });
  165. test('failed post', function(assert) {
  166. assert.expect(2);
  167. var done = assert.async();
  168. Ember.$.mockjax({
  169. url: '/api/some-rpc/',
  170. status: 400,
  171. responseText: {
  172. 'detail': 'it fails'
  173. }
  174. });
  175. service.post('some-rpc').then(function() {
  176. assert.ok(false, 'rpc call should fail');
  177. }, function(jqXHR) {
  178. var rejection = jqXHR.responseJSON;
  179. assert.equal(rejection.detail, 'it fails');
  180. }).finally(function() {
  181. assert.ok(true, 'finally() was called');
  182. done();
  183. });
  184. });
  185. test('successful model rpc post', function(assert) {
  186. assert.expect(2);
  187. var done = assert.async();
  188. Ember.$.mockjax({
  189. url: '/api/legal-pages/privacy-policy/',
  190. status: 200,
  191. responseText: {
  192. 'id': 'privacy-policy',
  193. 'title': '',
  194. 'link': '',
  195. 'body': ''
  196. }
  197. });
  198. Ember.$.mockjax({
  199. url: '/api/legal-pages/privacy-policy/some-rpc/',
  200. status: 200,
  201. responseText: {
  202. 'detail': 'it works'
  203. }
  204. });
  205. Ember.run(function() {
  206. var store = container.lookup('store:main');
  207. store.find('legal-page', 'privacy-policy').then(function(record) {
  208. service.post(record, 'some-rpc').then(function(data) {
  209. assert.equal(data.detail, 'it works');
  210. }, function() {
  211. assert.ok(false, 'rpc call should pass');
  212. }).finally(function() {
  213. assert.ok(true, 'finally() was called');
  214. done();
  215. });
  216. });
  217. });
  218. });
  219. test('failed model rpc post', function(assert) {
  220. assert.expect(2);
  221. var done = assert.async();
  222. Ember.$.mockjax({
  223. url: '/api/legal-pages/privacy-policy/',
  224. status: 200,
  225. responseText: {
  226. 'id': 'privacy-policy',
  227. 'title': '',
  228. 'link': '',
  229. 'body': ''
  230. }
  231. });
  232. Ember.$.mockjax({
  233. url: '/api/legal-pages/privacy-policy/some-rpc/',
  234. status: 400,
  235. responseText: {
  236. 'detail': 'it failed'
  237. }
  238. });
  239. Ember.run(function() {
  240. var store = container.lookup('store:main');
  241. store.find('legal-page', 'privacy-policy').then(function(record) {
  242. service.post(record, 'some-rpc').then(function() {
  243. assert.ok(false, 'rpc call should fail');
  244. }, function(jqXHR) {
  245. var rejection = jqXHR.responseJSON;
  246. assert.equal(rejection.detail, 'it failed');
  247. }).finally(function() {
  248. assert.ok(true, 'finally() was called');
  249. done();
  250. });
  251. });
  252. });
  253. });