user-details-modal.js 4.9 KB

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