ContactManagerTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const modulePath = '../../../../app/src/Features/Contacts/ContactManager'
  4. const SandboxedModule = require('sandboxed-module')
  5. describe('ContactManager', function () {
  6. beforeEach(function () {
  7. this.user_id = 'user-id-123'
  8. this.contact_id = 'contact-id-123'
  9. this.contact_ids = ['mock', 'contact_ids']
  10. this.FetchUtils = {
  11. fetchJson: sinon.stub(),
  12. }
  13. this.ContactManager = SandboxedModule.require(modulePath, {
  14. requires: {
  15. '@overleaf/fetch-utils': this.FetchUtils,
  16. '@overleaf/settings': (this.settings = {
  17. apis: {
  18. contacts: {
  19. url: 'http://contacts.overleaf.com',
  20. },
  21. },
  22. }),
  23. },
  24. })
  25. })
  26. describe('getContacts', function () {
  27. describe('with a successful response code', function () {
  28. beforeEach(async function () {
  29. this.FetchUtils.fetchJson.resolves({ contact_ids: this.contact_ids })
  30. this.result = await this.ContactManager.promises.getContactIds(
  31. this.user_id,
  32. { limit: 42 }
  33. )
  34. })
  35. it('should get the contacts from the contacts api', function () {
  36. this.FetchUtils.fetchJson.should.have.been.calledWithMatch(
  37. sinon.match(
  38. url =>
  39. url.toString() ===
  40. `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts?limit=42`
  41. )
  42. )
  43. })
  44. it('should return the contacts', function () {
  45. this.result.should.equal(this.contact_ids)
  46. })
  47. })
  48. describe('when an error occurs', function () {
  49. beforeEach(async function () {
  50. this.response = {
  51. ok: false,
  52. statusCode: 500,
  53. json: sinon.stub().resolves({ contact_ids: this.contact_ids }),
  54. }
  55. this.FetchUtils.fetchJson.rejects(new Error('request error'))
  56. })
  57. it('should reject the promise', async function () {
  58. await expect(
  59. this.ContactManager.promises.getContactIds(this.user_id, {
  60. limit: 42,
  61. })
  62. ).to.be.rejected
  63. })
  64. })
  65. })
  66. describe('addContact', function () {
  67. describe('with a successful response code', function () {
  68. beforeEach(async function () {
  69. this.FetchUtils.fetchJson.resolves({ contact_ids: this.contact_ids })
  70. this.result = await this.ContactManager.promises.addContact(
  71. this.user_id,
  72. this.contact_id
  73. )
  74. })
  75. it('should add the contacts for the user in the contacts api', function () {
  76. this.FetchUtils.fetchJson.should.have.been.calledWithMatch(
  77. sinon.match(
  78. url =>
  79. url.toString() ===
  80. `${this.settings.apis.contacts.url}/user/${this.user_id}/contacts`
  81. ),
  82. sinon.match({
  83. method: 'POST',
  84. json: { contact_id: this.contact_id },
  85. })
  86. )
  87. })
  88. it('should call the callback', function () {
  89. this.result.should.equal(this.contact_ids)
  90. })
  91. })
  92. })
  93. })