Skip to content

Commit 90971d0

Browse files
committed
jest fixes
1 parent ea2b866 commit 90971d0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ function setupPageNavigationListeners(): void {
5858
}
5959
}
6060

61+
let isPageLifecycleInitialized = false;
6162
function onPageReady(callback: () => void) {
63+
if (typeof window === 'undefined') return;
64+
65+
if (isPageLifecycleInitialized) {
66+
return;
67+
}
68+
isPageLifecycleInitialized = true;
69+
6270
if (document.readyState === 'complete') {
6371
callback();
6472
} else {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,23 @@ describe('pageLifecycle', () => {
7878
// Since no navigation library is mocked, callbacks should run immediately
7979
expect(callback).toHaveBeenCalledTimes(1);
8080
// Should not add DOMContentLoaded listener since readyState is not 'loading'
81-
expect(addEventListenerSpy).not.toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
81+
expect(addEventListenerSpy).not.toHaveBeenCalledWith('readystatechange', expect.any(Function));
8282
});
8383

84-
it('should initialize page event listeners immediately when document.readyState is "interactive"', () => {
85-
setReadyState('interactive');
84+
it('should wait for readystatechange when document.readyState is "interactive"', () => {
85+
setReadyState('loading');
8686
const callback = jest.fn();
8787
const { onPageLoaded } = importPageLifecycle();
8888

8989
onPageLoaded(callback);
9090

91-
// Since no navigation library is mocked, callbacks should run immediately
92-
expect(callback).toHaveBeenCalledTimes(1);
93-
// Should not add DOMContentLoaded listener since readyState is not 'loading'
94-
expect(addEventListenerSpy).not.toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
91+
// Should not call callback immediately since readyState is 'loading'
92+
expect(callback).not.toHaveBeenCalled();
93+
// Verify that a DOMContentLoaded listener was added when readyState is 'loading'
94+
expect(addEventListenerSpy).toHaveBeenCalledWith('readystatechange', expect.any(Function));
9595
});
9696

97-
it('should wait for DOMContentLoaded when document.readyState is "loading"', () => {
97+
it('should wait for readystatechange when document.readyState is "loading"', () => {
9898
setReadyState('loading');
9999
const callback = jest.fn();
100100
const { onPageLoaded } = importPageLifecycle();
@@ -104,7 +104,7 @@ describe('pageLifecycle', () => {
104104
// Should not call callback immediately since readyState is 'loading'
105105
expect(callback).not.toHaveBeenCalled();
106106
// Verify that a DOMContentLoaded listener was added when readyState is 'loading'
107-
expect(addEventListenerSpy).toHaveBeenCalledWith('DOMContentLoaded', expect.any(Function));
107+
expect(addEventListenerSpy).toHaveBeenCalledWith('readystatechange', expect.any(Function));
108108
});
109109

110110
describe('with Turbo navigation library', () => {

0 commit comments

Comments
 (0)