Skip to content

Commit

Permalink
Add jest-console typings and convert DateTimePicker tests to TypeScri…
Browse files Browse the repository at this point in the history
…pt (#40957)

* Convert DateTimePicker tests to TypeScript

* Use correct return type

* Use typings from DefinitelyTyped

* Update CHANGELOG.md files

* Add attribution
  • Loading branch information
noisysocks authored May 11, 2022
1 parent 15b211a commit 9120449
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Internal

- `DateTimePicker`: Convert to TypeScript ([#40775](https://github.com/WordPress/gutenberg/pull/40775)).
- `DateTimePicker`: Convert unit tests to TypeScript ([#40957](https://github.com/WordPress/gutenberg/pull/40957)).

## 19.10.0 (2022-05-04)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,21 @@ describe( 'TimePicker', () => {
/>
);

expect( screen.getByLabelText( 'Month' ).value ).toBe( '07' );
expect( screen.getByLabelText( 'Day' ).value ).toBe( '13' );
expect( screen.getByLabelText( 'Year' ).value ).toBe( '2020' );
expect( screen.getByLabelText( 'Hours' ).value ).toBe( '06' );
expect( screen.getByLabelText( 'Minutes' ).value ).toBe( '00' );
expect(
( screen.getByLabelText( 'Month' ) as HTMLInputElement ).value
).toBe( '07' );
expect(
( screen.getByLabelText( 'Day' ) as HTMLInputElement ).value
).toBe( '13' );
expect(
( screen.getByLabelText( 'Year' ) as HTMLInputElement ).value
).toBe( '2020' );
expect(
( screen.getByLabelText( 'Hours' ) as HTMLInputElement ).value
).toBe( '06' );
expect(
( screen.getByLabelText( 'Minutes' ) as HTMLInputElement ).value
).toBe( '00' );
/**
* This is not ideal, but best of we can do for now until we refactor
* AM/PM into accessible elements, like radio buttons.
Expand All @@ -267,14 +277,12 @@ describe( 'TimePicker', () => {
</form>
);

const form = screen.getByRole( 'form' );
const form = screen.getByRole( 'form' ) as HTMLFormElement;

let monthInputIndex = [].indexOf.call(
form.elements,
let monthInputIndex = Array.from( form.elements ).indexOf(
screen.getByLabelText( 'Month' )
);
let dayInputIndex = [].indexOf.call(
form.elements,
let dayInputIndex = Array.from( form.elements ).indexOf(
screen.getByLabelText( 'Day' )
);

Expand All @@ -290,12 +298,10 @@ describe( 'TimePicker', () => {
</form>
);

monthInputIndex = [].indexOf.call(
form.elements,
monthInputIndex = Array.from( form.elements ).indexOf(
screen.getByLabelText( 'Month' )
);
dayInputIndex = [].indexOf.call(
form.elements,
dayInputIndex = Array.from( form.elements ).indexOf(
screen.getByLabelText( 'Day' )
);

Expand All @@ -313,11 +319,20 @@ describe( 'TimePicker', () => {
/>
);

const monthInput = screen.getByLabelText( 'Month' ).value;
const dayInput = screen.getByLabelText( 'Day' ).value;
const yearInput = screen.getByLabelText( 'Year' ).value;
const hoursInput = screen.getByLabelText( 'Hours' ).value;
const minutesInput = screen.getByLabelText( 'Minutes' ).value;
const monthInput = ( screen.getByLabelText(
'Month'
) as HTMLInputElement ).value;
const dayInput = ( screen.getByLabelText( 'Day' ) as HTMLInputElement )
.value;
const yearInput = ( screen.getByLabelText(
'Year'
) as HTMLInputElement ).value;
const hoursInput = ( screen.getByLabelText(
'Hours'
) as HTMLInputElement ).value;
const minutesInput = ( screen.getByLabelText(
'Minutes'
) as HTMLInputElement ).value;

expect( Number.isNaN( parseInt( monthInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( dayInput, 10 ) ) ).toBe( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe( 'getMomentDate', () => {
const momentDate = getMomentDate( currentDate );

expect( moment.isMoment( momentDate ) ).toBe( true );
expect( momentDate.isSame( moment( currentDate ) ) ).toBe( true );
expect( momentDate?.isSame( moment( currentDate ) ) ).toBe( true );
} );

it( 'should return null when given a null argument', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/date-time/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { ValidDateTimeInput } from './types';
* date or null to signify no selection.
* @return {?moment.Moment} Moment object for selected date or null.
*/
export const getMomentDate = ( date: ValidDateTimeInput | undefined ) => {
export const getMomentDate = ( date?: ValidDateTimeInput ) => {
if ( null === date ) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"gutenberg-test-env",
"jest",
"@testing-library/jest-dom",
"snapshot-diff"
"snapshot-diff",
"@wordpress/jest-console"
],
// Some errors in Reakit types with TypeScript 4.3
// Remove the following line when they've been addressed.
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Added TypeScript definitions for package consumers ([#40957](https://github.com/WordPress/gutenberg/pull/40957)).

## 5.0.0 (2022-01-27)

### Breaking Changes
Expand Down
1 change: 1 addition & 0 deletions packages/jest-console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"main": "build/index.js",
"module": "build-module/index.js",
"types": "types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"jest-matcher-utils": "^27.4.2",
Expand Down
46 changes: 46 additions & 0 deletions packages/jest-console/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Definitions originally written by Damien Sorel <https://github.com/mistic100> under MIT license.
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f0b72c12f6b561e4342dc8a1cf87432d2ad40ae7/types/wordpress__jest-console/index.d.ts

declare namespace jest {
interface Matchers< R, T > {
/**
* Ensure that `console.error` function was called.
*/
toHaveErrored(): R;

/**
* Ensure that `console.error` function was called with specific arguments.
*/
toHaveErroredWith( ...args: any[] ): R;

/**
* Ensure that `console.info` function was called.
*/
toHaveInformed(): R;

/**
* Ensure that `console.info` function was called with specific arguments.
*/
toHaveInformedWith( ...args: any[] ): R;

/**
* Ensure that `console.log` function was called.
*/
toHaveLogged(): R;

/**
* Ensure that `console.log` function was called with specific arguments.
*/
toHaveLoggedWith( ...args: any[] ): R;

/**
* Ensure that `console.warn` function was called.
*/
toHaveWarned(): R;

/**
* Ensure that `console.warn` function was called with specific arguments.
*/
toHaveWarnedWith( ...args: any[] ): R;
}
}

0 comments on commit 9120449

Please sign in to comment.