Skip to content

Commit aaae2d0

Browse files
committed
remove event listener multiple call test
1 parent 497c5d1 commit aaae2d0

File tree

2 files changed

+4
-26
lines changed

2 files changed

+4
-26
lines changed

packages/react-on-rails/src/pageLifecycle.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ let isPageLifecycleInitialized = false;
6262
function onPageReady(callback: () => void) {
6363
if (typeof window === 'undefined') return;
6464

65-
if (isPageLifecycleInitialized) {
66-
return;
67-
}
68-
isPageLifecycleInitialized = true;
69-
7065
if (document.readyState === 'complete') {
66+
if (isPageLifecycleInitialized) {
67+
return;
68+
}
69+
isPageLifecycleInitialized = true;
7170
callback();
7271
} else {
7372
document.addEventListener('readystatechange', function onReadyStateChange() {

packages/react-on-rails/tests/pageLifecycle.test.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,4 @@ describe('pageLifecycle', () => {
232232
global.window = originalWindow;
233233
});
234234
});
235-
236-
describe('preventing duplicate initialization', () => {
237-
it('should not initialize listeners multiple times', () => {
238-
setReadyState('loading');
239-
const { onPageLoaded } = importPageLifecycle();
240-
const callback1 = jest.fn();
241-
const callback2 = jest.fn();
242-
243-
// First call should initialize and call addEventListener
244-
onPageLoaded(callback1);
245-
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
246-
247-
// Second call should not add more listeners (isPageLifecycleInitialized is true)
248-
onPageLoaded(callback2);
249-
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
250-
251-
// Both callbacks should be called
252-
expect(callback1).not.toHaveBeenCalled();
253-
expect(callback2).not.toHaveBeenCalled();
254-
});
255-
});
256235
});

0 commit comments

Comments
 (0)