renderOnDomReady.spec.jsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {renderOnDomReady} from 'sentry/bootstrap/renderOnDomReady';
  2. describe('renderOnDomReady', function () {
  3. it('immediately runs callback if `document.readyState` is not loading', function () {
  4. const cb = jest.fn();
  5. renderOnDomReady(cb);
  6. expect(cb).toHaveBeenCalledTimes(1);
  7. });
  8. it('runs callbacks after `DOMContentLoaded` is fired', function () {
  9. const cb = jest.fn();
  10. const cb2 = jest.fn();
  11. Object.defineProperty(document, 'readyState', {
  12. configurable: true,
  13. get() {
  14. return 'loading';
  15. },
  16. });
  17. renderOnDomReady(cb);
  18. renderOnDomReady(cb2);
  19. expect(cb).toHaveBeenCalledTimes(0);
  20. // Dispatch `DOMContentLoaded` event
  21. const event = document.createEvent('Event');
  22. event.initEvent('DOMContentLoaded', true, true);
  23. document.dispatchEvent(event);
  24. expect(cb).toHaveBeenCalledTimes(1);
  25. expect(cb2).toHaveBeenCalledTimes(1);
  26. // Reset `readyState`
  27. Object.defineProperty(document, 'readyState', {
  28. get() {
  29. return 'complete';
  30. },
  31. });
  32. });
  33. });