Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- `Divider`, `Flex`, `Spacer`: Improve documentation for the `SpaceInput` prop ([#42376](https://github.com/WordPress/gutenberg/pull/42376)).
- `Elevation`: Convert to TypeScript ([#42302](https://github.com/WordPress/gutenberg/pull/42302)).
- `ScrollLock`: Convert to TypeScript ([#42303](https://github.com/WordPress/gutenberg/pull/42303)).
- `Shortcut`: Convert to TypeScript ([#42272](https://github.com/WordPress/gutenberg/pull/42272)).
- `TreeSelect`: Refactor away from `_.compact()` ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).
- `MediaEdit`: Refactor away from `_.compact()` for mobile ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).
- `BoxControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/** @typedef {string | { display: string, ariaLabel: string }} Shortcut */
/**
* @typedef Props
* @property {Shortcut} shortcut Shortcut configuration
* @property {string} [className] Classname
* Internal dependencies
*/
import type { ShortcutProps } from './types';

function Shortcut( props: ShortcutProps ) {
const { shortcut, className } = props;

/**
* @param {Props} props Props
* @return {JSX.Element | null} Element
*/
function Shortcut( { shortcut, className } ) {
if ( ! shortcut ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Shortcut renders passed className 1`] = `
<span
class="my-class"
>
shortcut text
</span>
`;

exports[`Shortcut renders the shortcut display text when a string is passed as the shortcut 1`] = `
<span>
shortcut text
</span>
`;
37 changes: 0 additions & 37 deletions packages/components/src/shortcut/test/index.js

This file was deleted.

41 changes: 41 additions & 0 deletions packages/components/src/shortcut/test/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import Shortcut from '..';

describe( 'Shortcut', () => {
it( 'does not render anything if no shortcut prop is provided', () => {
const { container } = render( <Shortcut /> );
expect( container.firstChild ).toBe( null );
} );

it( 'renders the shortcut display text when a string is passed as the shortcut', () => {
render( <Shortcut shortcut="shortcut text" /> );
expect( screen.getByText( 'shortcut text' ) ).toMatchSnapshot();
} );

it( 'renders the shortcut display text and aria-label when an object is passed as the shortcut with the correct properties', () => {
render(
<Shortcut
shortcut={ {
display: 'shortcut text',
ariaLabel: 'shortcut label',
} }
/>
);

expect( screen.getByLabelText( 'shortcut label' ) ).toHaveTextContent(
'shortcut text'
);
} );

it( 'renders passed className', () => {
render( <Shortcut shortcut="shortcut text" className="my-class" /> );
expect( screen.getByText( 'shortcut text' ) ).toMatchSnapshot();
} );
} );
10 changes: 10 additions & 0 deletions packages/components/src/shortcut/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type ShortcutProps = {
/**
* Classname to apply to the shortcut.
*/
className?: string;
/**
* Shortcut configuration
*/
shortcut?: string | { display: string; ariaLabel: string };
};