Skip to content

Commit

Permalink
fix(cy): serialize resize observer calls
Browse files Browse the repository at this point in the history
Prevent crashes in Chrome due to the resize observer warnings.

See cypress-io/cypress#27415 (comment)

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Dec 23, 2024
1 parent f840ae0 commit 14ad7ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cypress/support/ResizeObserverPolyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// store real observer
const RealResizeObserver = ResizeObserver;

Check failure on line 2 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon

let queueFlushTimeout;

Check failure on line 4 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
let queue = [];

Check failure on line 5 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon

/**
* ResizeObserver wrapper with "enforced batches"
*/
export default class ResizeObserverPolyfill {

Check failure on line 10 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Block must be padded by blank lines
constructor(callback) {
this.callback = callback;

Check failure on line 12 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
this.observer = new RealResizeObserver(this.check.bind(this));

Check failure on line 13 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
}

observe(element) {
this.observer.observe(element);

Check failure on line 17 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
}

unobserve(element) {
this.observer.unobserve(element);

Check failure on line 21 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
}

disconnect() {
this.observer.disconnect();

Check failure on line 25 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
}

check(entries) {
// remove previous invocations of "self"
queue = queue.filter((x) => x.cb !== this.callback);

Check failure on line 30 in cypress/support/ResizeObserverPolyfill.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
// put a new one
queue.push({ cb: this.callback, args: entries });
// trigger update
if (!queueFlushTimeout) {
queueFlushTimeout = requestAnimationFrame(() => {
queueFlushTimeout = undefined;
const q = queue;
queue = [];
q.forEach(({ cb, args }) => cb(args));
}, 0);
}
}
}
2 changes: 2 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import './commands.js'
import './sessions.js'
import chaiExtension from './chai.js'
import ResizeObserverPolyfill from './ResizeObserverPolyfill.js'

beforeEach(() => {
cy.intercept('GET', '**/preview-service-worker.js', { fixture: 'preview-service-worker.txt' })
Expand All @@ -17,6 +18,7 @@ Cypress.on('window:before:load', (win) => {
// disable service workers
// eslint-disable-next-line
delete win.navigator.ServiceWorker
win.ResizeObserver = ResizeObserverPolyfill
})

before(() => {
Expand Down

0 comments on commit 14ad7ab

Please sign in to comment.