Core_destroy.spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. describe('Core_destroy', () => {
  2. const id = 'testContainer';
  3. beforeEach(() => {
  4. spec().$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(() => {
  7. if (spec().$container) {
  8. destroy();
  9. spec().$container.remove();
  10. }
  11. });
  12. it('should remove table from the root element', () => {
  13. handsontable();
  14. destroy();
  15. expect(spec().$container.html()).toEqual('');
  16. });
  17. it('should remove events from the root element, document element and window', () => {
  18. const x = handsontable();
  19. expect(x.eventListeners.length > 0).toBeTruthy();
  20. destroy();
  21. expect(x.eventListeners).toBeNull();
  22. $(document.documentElement).off('.copypaste'); // remove copypaste.js listeners, which are not removed by destroy (because copypaste is a singleton for whole page)
  23. });
  24. it('should NOT remove events from document element and window for other Handsontable instances on the page', () => {
  25. // test based on Core_selectionSpec.js (should deselect currently selected cell)
  26. handsontable();
  27. const $tmp = $('<div id="tmp"></div>').appendTo(document.body);
  28. $tmp.handsontable();
  29. $tmp.handsontable('destroy');
  30. $tmp.remove();
  31. selectCell(0, 0);
  32. $('html').simulate('mousedown');
  33. expect(getSelected()).toBeUndefined();
  34. });
  35. it('should throw an exception when metod on destroyed instance is called', () => {
  36. const hot = handsontable();
  37. destroy();
  38. expect(() => {
  39. hot.getDataAtCell(0, 0);
  40. }).toThrowError('The "getDataAtCell" method cannot be called because this Handsontable instance has been destroyed');
  41. expect(() => {
  42. hot.listen();
  43. }).toThrowError('The "listen" method cannot be called because this Handsontable instance has been destroyed');
  44. });
  45. it('should set isDestroyed flag to `true` when instance is destroyed', () => {
  46. const hot = handsontable();
  47. expect(hot.isDestroyed).toBe(false);
  48. destroy();
  49. expect(hot.isDestroyed).toBe(true);
  50. });
  51. });