user-details-modal.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. const u = converse.env.utils;
  10. return describe("The User Details Modal", function () {
  11. it("can be used to remove a contact",
  12. mock.initConverse(
  13. ['rosterGroupsFetched', 'chatBoxesFetched', 'emojisInitialized'], {},
  14. async function (done, _converse) {
  15. await test_utils.waitForRoster(_converse, 'current', 1);
  16. _converse.api.trigger('rosterContactsFetched');
  17. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  18. await test_utils.openChatBoxFor(_converse, contact_jid);
  19. await u.waitUntil(() => _converse.chatboxes.length > 1);
  20. const view = _converse.chatboxviews.get(contact_jid);
  21. let show_modal_button = view.el.querySelector('.show-user-details-modal');
  22. expect(u.isVisible(show_modal_button)).toBeTruthy();
  23. show_modal_button.click();
  24. const modal = view.user_details_modal;
  25. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  26. spyOn(window, 'confirm').and.returnValue(true);
  27. spyOn(view.model.contact, 'removeFromRoster').and.callFake(function (callback) {
  28. callback();
  29. });
  30. let remove_contact_button = modal.el.querySelector('button.remove-contact');
  31. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  32. remove_contact_button.click();
  33. await u.waitUntil(() => modal.el.getAttribute('aria-hidden'), 1000);
  34. show_modal_button = view.el.querySelector('.show-user-details-modal');
  35. show_modal_button.click();
  36. remove_contact_button = modal.el.querySelector('button.remove-contact');
  37. expect(remove_contact_button === null).toBeTruthy();
  38. done();
  39. }));
  40. it("shows an alert when an error happened while removing the contact",
  41. mock.initConverse(
  42. ['rosterGroupsFetched', 'emojisInitialized'], {},
  43. async function (done, _converse) {
  44. await test_utils.waitForRoster(_converse, 'current', 1);
  45. _converse.api.trigger('rosterContactsFetched');
  46. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  47. await test_utils.openChatBoxFor(_converse, contact_jid)
  48. const view = _converse.chatboxviews.get(contact_jid);
  49. let show_modal_button = view.el.querySelector('.show-user-details-modal');
  50. expect(u.isVisible(show_modal_button)).toBeTruthy();
  51. show_modal_button.click();
  52. const modal = view.user_details_modal;
  53. await u.waitUntil(() => u.isVisible(modal.el), 2000);
  54. spyOn(window, 'confirm').and.returnValue(true);
  55. spyOn(view.model.contact, 'removeFromRoster').and.callFake((callback, errback) => errback());
  56. let remove_contact_button = modal.el.querySelector('button.remove-contact');
  57. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  58. remove_contact_button.click();
  59. await u.waitUntil(() => u.isVisible(document.querySelector('.alert-danger')), 2000);
  60. const header = document.querySelector('.alert-danger .modal-title');
  61. expect(header.textContent).toBe("Error");
  62. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  63. .toBe("Sorry, there was an error while trying to remove Mercutio as a contact.");
  64. document.querySelector('.alert-danger button.close').click();
  65. show_modal_button = view.el.querySelector('.show-user-details-modal');
  66. show_modal_button.click();
  67. await u.waitUntil(() => u.isVisible(modal.el), 2000)
  68. show_modal_button = view.el.querySelector('.show-user-details-modal');
  69. show_modal_button.click();
  70. await u.waitUntil(() => u.isVisible(modal.el), 2000)
  71. remove_contact_button = modal.el.querySelector('button.remove-contact');
  72. expect(u.isVisible(remove_contact_button)).toBeTruthy();
  73. done();
  74. }));
  75. });
  76. }));