user-details-modal.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "utils",
  5. "converse-core",
  6. "mock",
  7. "test-utils"
  8. ], factory);
  9. } (this, function (jasmine, utils, converse, mock, test_utils) {
  10. "use strict";
  11. var _ = converse.env._;
  12. var $iq = converse.env.$iq;
  13. var $msg = converse.env.$msg;
  14. var Strophe = converse.env.Strophe;
  15. var u = converse.env.utils;
  16. return describe("The User Details Modal", function () {
  17. it("can be used to remove a contact",
  18. mock.initConverseWithPromises(
  19. null, ['rosterGroupsFetched'], {},
  20. function (done, _converse) {
  21. test_utils.createContacts(_converse, 'current');
  22. _converse.emit('rosterContactsFetched');
  23. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  24. test_utils.openChatBoxFor(_converse, contact_jid);
  25. const view = _converse.chatboxviews.get(contact_jid);
  26. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  27. expect(u.isVisible(show_modal_button)).toBeTruthy();
  28. show_modal_button.click();
  29. const modal = view.user_details_modal;
  30. test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
  31. .then(function () {
  32. spyOn(window, 'confirm').and.returnValue(true);
  33. spyOn(view.model.contact, 'removeFromRoster').and.callFake(function (callback) {
  34. callback();
  35. });
  36. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  37. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  38. remove_contact_button.click();
  39. return test_utils.waitUntil(() => modal.el.getAttribute('aria-hidden'), 1000);
  40. }).then(function () {
  41. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  42. show_modal_button.click();
  43. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  44. expect(_.isNull(remove_contact_button)).toBeTruthy();
  45. done();
  46. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  47. }));
  48. it("shows an alert when an error happened while removing the contact",
  49. mock.initConverseWithPromises(
  50. null, ['rosterGroupsFetched'], {},
  51. function (done, _converse) {
  52. test_utils.createContacts(_converse, 'current');
  53. _converse.emit('rosterContactsFetched');
  54. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  55. test_utils.openChatBoxFor(_converse, contact_jid);
  56. const view = _converse.chatboxviews.get(contact_jid);
  57. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  58. expect(u.isVisible(show_modal_button)).toBeTruthy();
  59. show_modal_button.click();
  60. const modal = view.user_details_modal;
  61. test_utils.waitUntil(() => u.isVisible(modal.el), 2000)
  62. .then(function () {
  63. spyOn(window, 'confirm').and.returnValue(true);
  64. spyOn(view.model.contact, 'removeFromRoster').and.callFake(function (callback, errback) {
  65. errback();
  66. });
  67. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  68. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  69. remove_contact_button.click();
  70. return test_utils.waitUntil(() => u.isVisible(document.querySelector('.alert-danger')), 2000);
  71. }).then(function () {
  72. expect(document.querySelector('.alert-danger .modal-title').textContent).toBe("Error");
  73. expect(document.querySelector('.modal:not(#user-profile-modal) .modal-body p').textContent.trim())
  74. .toBe("Sorry, there was an error while trying to remove Max Frankfurter as a contact.");
  75. document.querySelector('.alert-danger button.close').click();
  76. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  77. show_modal_button.click();
  78. return test_utils.waitUntil(() => u.isVisible(modal.el), 2000)
  79. }).then(function () {
  80. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  81. show_modal_button.click();
  82. const modal = view.user_details_modal;
  83. return test_utils.waitUntil(() => u.isVisible(modal.el), 2000)
  84. }).then(function () {
  85. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  86. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  87. done();
  88. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  89. }));
  90. });
  91. }));