-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(components): replace the componentOnReady method by an helper
- Loading branch information
1 parent
e8460d8
commit 65eade9
Showing
5 changed files
with
86 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@swisspost/design-system-components': patch | ||
--- | ||
|
||
Fixed the post-tabs component throwing an error when imported individually. |
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,25 @@ | ||
import { HostElement } from '@stencil/core/internal'; | ||
|
||
/** | ||
* Invoke the `componentOnReady` method if it is available, simulate it otherwise | ||
* @see https://stenciljs.com/docs/api#componentonready | ||
*/ | ||
export const componentOnReady = <T extends HostElement>(el: T): Promise<T> => { | ||
if (typeof el.componentOnReady === 'function') { | ||
return el.componentOnReady(); | ||
} else { | ||
return new Promise(resolve => | ||
customOnReady(() => { | ||
resolve(el); | ||
}), | ||
); | ||
} | ||
}; | ||
|
||
const customOnReady = (callback: FrameRequestCallback) => { | ||
if (typeof requestAnimationFrame === 'function') { | ||
return requestAnimationFrame(callback); | ||
} | ||
|
||
return setTimeout(callback); | ||
}; |
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
51 changes: 51 additions & 0 deletions
51
packages/components/src/utils/tests/component-on-ready.spec.ts
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,51 @@ | ||
import { componentOnReady } from '../component-on-ready'; | ||
import { HostElement } from '@stencil/core/internal'; | ||
|
||
describe('componentOnReady', () => { | ||
const mockRequestAnimationFrame = jest.fn(); | ||
const mockSetTimeout = jest.fn(); | ||
|
||
beforeAll(() => { | ||
global.requestAnimationFrame = mockRequestAnimationFrame; | ||
global.setTimeout = mockSetTimeout; | ||
}); | ||
|
||
afterEach(() => { | ||
mockRequestAnimationFrame.mockClear(); | ||
mockSetTimeout.mockClear(); | ||
}); | ||
|
||
it('should return the result of componentOnReady if it exists', async () => { | ||
const el = { | ||
componentOnReady: jest.fn().mockResolvedValue('resolvedValue'), | ||
} as HostElement; | ||
|
||
const result = await componentOnReady(el); | ||
expect(el.componentOnReady).toHaveBeenCalledTimes(1); | ||
expect(result).toBe('resolvedValue'); | ||
}); | ||
|
||
it('should use customOnReady if componentOnReady does not exist and requestAnimationFrame is available', async () => { | ||
mockRequestAnimationFrame.mockImplementation(callback => callback()); | ||
|
||
const el = {} as HostElement; | ||
const promise = componentOnReady(el); | ||
|
||
const result = await promise; | ||
expect(mockRequestAnimationFrame).toHaveBeenCalledTimes(1); | ||
expect(result).toBe(el); | ||
}); | ||
|
||
it('should use customOnReady if componentOnReady does not exist and requestAnimationFrame is not available', async () => { | ||
delete global.requestAnimationFrame; | ||
|
||
mockSetTimeout.mockImplementation(callback => callback()); | ||
|
||
const el = {} as HostElement; | ||
const promise = componentOnReady(el); | ||
|
||
const result = await promise; | ||
expect(mockSetTimeout).toHaveBeenCalledTimes(1); | ||
expect(result).toBe(el); | ||
}); | ||
}); |