api.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function () {
  2. 'use strict';
  3. var service = getMisagoService('api');
  4. var container = {
  5. setup: {
  6. api: '/test-api/'
  7. }
  8. };
  9. QUnit.module("API");
  10. QUnit.test("service factory", function(assert) {
  11. var api = service({});
  12. assert.ok(api, "service factory has returned service instance.");
  13. });
  14. QUnit.test("model", function(assert) {
  15. var api = service(container);
  16. assert.equal(api.model('user', 'admin').url, '/test-api/users/admin/',
  17. "model constructed valid url with string pk.");
  18. assert.equal(api.model('user', 123).url, '/test-api/users/123/',
  19. "model constructed valid url with integer pk.");
  20. assert.equal(
  21. api.model('user', {token: 'abc&df', uid: 123}).url,
  22. '/test-api/users/?token=abc%26df&uid=123',
  23. "model constructed valid url with querystring.");
  24. assert.equal(api.model('user', 123).related('follows', 1).url,
  25. '/test-api/users/123/follows/1/',
  26. "model constructed valid related url.");
  27. assert.equal(api.model('user', 123).endpoint('avatar').url,
  28. '/test-api/users/123/avatar/',
  29. "model constructed valid endpoint url.");
  30. assert.ok(
  31. !api.model('user', 123).endpoint('avatar').related,
  32. "model can't have relation to endpoint.");
  33. assert.ok(
  34. !api.model('user', 123).related('avatar').related,
  35. "model can't have relation to relation.");
  36. });
  37. QUnit.test("endpoint", function(assert) {
  38. var api = service(container);
  39. assert.equal(api.endpoint('auth').url, '/test-api/auth/',
  40. "endpoint constructed valid url.");
  41. assert.equal(
  42. api.endpoint('auth', 'string-pk').url,
  43. '/test-api/auth/string-pk/',
  44. "endpoint constructed valid url with string pk.");
  45. assert.equal(
  46. api.endpoint('auth', 124).url,
  47. '/test-api/auth/124/',
  48. "endpoint constructed valid url with integer pk.");
  49. assert.equal(
  50. api.endpoint('auth', {token: 'abc&df', uid: 123}).url,
  51. '/test-api/auth/?token=abc%26df&uid=123',
  52. "endpoint constructed valid url with querystring.");
  53. assert.ok(
  54. !api.endpoint('auth', 124).related,
  55. "endpoint can't have nested relation.");
  56. assert.equal(
  57. api.endpoint('auth', 124).endpoint('change-password').url,
  58. '/test-api/auth/124/change-password/',
  59. "nested endpoint constructed valid url with integer pk.");
  60. assert.ok(
  61. !api.endpoint('auth', 124).endpoint('change-password').endpoint,
  62. "nested endpoint can't be nested further.");
  63. });
  64. }());