-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ID-1174 Passport - delay preload until window ready
- Loading branch information
1 parent
b7ae1d2
commit 00b319f
Showing
18 changed files
with
162 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { lazyDocumentReady, lazyLoad } from './lazyLoad'; | ||
|
||
describe('lazyLoad', () => { | ||
it('should return call initFunction and returns the value', async () => { | ||
const initFunction = jest.fn().mockReturnValue('test'); | ||
const promiseToAwait = jest.fn().mockResolvedValue(undefined); | ||
const result = await lazyLoad(promiseToAwait, initFunction); | ||
expect(result).toEqual('test'); | ||
expect(initFunction).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('lazyDocumentReady', () => { | ||
let mockInitialiseFunction: jest.Mock<any, any>; | ||
let originalDocument: Document | undefined; | ||
|
||
beforeEach(() => { | ||
mockInitialiseFunction = jest.fn(); | ||
originalDocument = window.document; | ||
const mockDocument = { | ||
...window.document, | ||
readyState: 'complete', | ||
}; | ||
(window as any).document = mockDocument; | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the original document.readyState value after each test | ||
(window as any).document = originalDocument; | ||
}); | ||
|
||
it('should call the initialiseFunction when the document is already ready', async () => { | ||
jest.spyOn(window.document, 'readyState', 'get').mockReturnValue('complete'); | ||
|
||
await lazyDocumentReady(mockInitialiseFunction); | ||
|
||
expect(mockInitialiseFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call the initialiseFunction when the document becomes ready', async () => { | ||
jest.spyOn(window.document, 'readyState', 'get').mockReturnValue('loading'); | ||
const mockAddEventListener = jest.spyOn(window.document, 'addEventListener'); | ||
|
||
const lazyDocumentPromise = lazyDocumentReady(mockInitialiseFunction); | ||
expect(mockInitialiseFunction).toHaveBeenCalledTimes(0); | ||
|
||
jest.spyOn(window.document, 'readyState', 'get').mockReturnValue('complete'); | ||
const mockEvent = new Event('readystatechange'); | ||
window.document.dispatchEvent(mockEvent); | ||
|
||
await lazyDocumentPromise; | ||
|
||
expect(mockAddEventListener).toHaveBeenCalledWith('readystatechange', expect.any(Function)); | ||
expect(mockInitialiseFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const lazyLoad = <T, Y = void>(promiseToAwait: () => Promise<Y>, initialiseFunction: () => T): Promise<T> => ( | ||
promiseToAwait().then(initialiseFunction) | ||
); | ||
|
||
export const lazyDocumentReady = <T>(initialiseFunction: () => T): Promise<T> => { | ||
const documentReadyPromise = () => new Promise<void>((resolve) => { | ||
if (window.document.readyState === 'complete') { | ||
resolve(); | ||
} else { | ||
const onReadyStateChange = () => { | ||
if (window.document.readyState === 'complete') { | ||
resolve(); | ||
window.document.removeEventListener('readystatechange', onReadyStateChange); | ||
} | ||
}; | ||
window.document.addEventListener('readystatechange', onReadyStateChange); | ||
} | ||
}); | ||
|
||
return lazyLoad(documentReadyPromise, initialiseFunction); | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters