Skip to content

Commit

Permalink
Convert dom-ready package to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Dec 4, 2024
1 parent e62f8b4 commit 81421aa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
6 changes: 1 addition & 5 deletions packages/dom-ready/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ domReady( function () {

_Parameters_

- _callback_ `Callback`: A function to execute after the DOM is ready.

_Returns_

- `void`:
- _callback_ `VoidFunction`: A function to execute after the DOM is ready.

<!-- END TOKEN(Autogenerated API docs) -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
/**
* @typedef {() => void} Callback
*
* TODO: Remove this typedef and inline `() => void` type.
*
* This typedef is used so that a descriptive type is provided in our
* automatically generated documentation.
*
* An in-line type `() => void` would be preferable, but the generated
* documentation is `null` in that case.
*
* @see https://github.com/WordPress/gutenberg/issues/18045
*/

/**
* Specify a function to execute when the DOM is fully loaded.
*
* @param {Callback} callback A function to execute after the DOM is ready.
* @param callback A function to execute after the DOM is ready.
*
* @example
* ```js
Expand All @@ -25,10 +11,8 @@
* //do something after DOM loads.
* } );
* ```
*
* @return {void}
*/
export default function domReady( callback ) {
export default function domReady( callback: VoidFunction ) {
if ( typeof document === 'undefined' ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe( 'domReady', () => {
describe( 'when document readystate is complete', () => {
it( 'should call the callback.', () => {
const callback = jest.fn( () => {} );
// @ts-expect-error document.readyState is read-only
document.readyState = 'complete';
domReady( callback );
expect( callback ).toHaveBeenCalled();
Expand All @@ -23,6 +24,7 @@ describe( 'domReady', () => {
describe( 'when document readystate is interactive', () => {
it( 'should call the callback.', () => {
const callback = jest.fn( () => {} );
// @ts-expect-error document.readyState is read-only
document.readyState = 'interactive';
domReady( callback );
expect( callback ).toHaveBeenCalled();
Expand All @@ -32,6 +34,7 @@ describe( 'domReady', () => {
describe( 'when document readystate is still loading', () => {
it( 'should add the callback as an event listener to the DOMContentLoaded event.', () => {
const addEventListener = jest.fn( () => {} );
// @ts-expect-error document.readyState is read-only
document.readyState = 'loading';
Object.defineProperty( document, 'addEventListener', {
value: addEventListener,
Expand Down

0 comments on commit 81421aa

Please sign in to comment.