user-details-modal.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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', 'chatBoxesFetched'], {},
  18. function (done, _converse) {
  19. test_utils.createContacts(_converse, 'current');
  20. _converse.emit('rosterContactsFetched');
  21. let view, show_modal_button, modal;
  22. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  23. test_utils.openChatBoxFor(_converse, contact_jid);
  24. return test_utils.waitUntil(() => _converse.chatboxes.length).then(() => {
  25. view = _converse.chatboxviews.get(contact_jid);
  26. show_modal_button = view.el.querySelector('.show-user-details-modal');
  27. expect(u.isVisible(show_modal_button)).toBeTruthy();
  28. show_modal_button.click();
  29. modal = view.user_details_modal;
  30. return 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. let view, modal;
  55. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  56. test_utils.openChatBoxFor(_converse, contact_jid)
  57. .then(() => {
  58. view = _converse.chatboxviews.get(contact_jid);
  59. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  60. expect(u.isVisible(show_modal_button)).toBeTruthy();
  61. show_modal_button.click();
  62. modal = view.user_details_modal;
  63. return test_utils.waitUntil(() => u.isVisible(modal.el), 2000);
  64. }).then(function () {
  65. spyOn(window, 'confirm').and.returnValue(true);
  66. spyOn(view.model.contact, 'removeFromRoster').and.callFake(function (callback, errback) {
  67. errback();
  68. });
  69. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  70. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  71. remove_contact_button.click();
  72. return test_utils.waitUntil(() => u.isVisible(document.querySelector('.alert-danger')), 2000);
  73. }).then(function () {
  74. const header = document.querySelector('.alert-danger .modal-title');
  75. expect(header.textContent).toBe("Error");
  76. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  77. .toBe("Sorry, there was an error while trying to remove Max Frankfurter as a contact.");
  78. document.querySelector('.alert-danger button.close').click();
  79. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  80. show_modal_button.click();
  81. return test_utils.waitUntil(() => u.isVisible(modal.el), 2000)
  82. }).then(function () {
  83. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  84. show_modal_button.click();
  85. const modal = view.user_details_modal;
  86. return test_utils.waitUntil(() => u.isVisible(modal.el), 2000)
  87. }).then(function () {
  88. const remove_contact_button = modal.el.querySelector('button.remove-contact');
  89. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  90. done();
  91. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  92. }));
  93. });
  94. }));