Skip to content

Commit

Permalink
Navigator Screen: warn if path doesn't follow a URL-like scheme (Word…
Browse files Browse the repository at this point in the history
…Press#65231)

* Navigator Screen: warn if path doesn't follow the expected scheme

* Add test

* CHANGELOG

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
  • Loading branch information
4 people authored Sep 11, 2024
1 parent 20ca80f commit 5daa582
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Enhancements

- `Navigator`: warn if a screen's `path` doesn't follow a URL-like scheme ([#65231](https://github.com/WordPress/gutenberg/pull/65231)).
- `Modal`: Decrease close button size and remove horizontal offset ([#65131](https://github.com/WordPress/gutenberg/pull/65131)).

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { useMergeRefs } from '@wordpress/compose';
import { isRTL as isRTLFn } from '@wordpress/i18n';
import { escapeAttribute } from '@wordpress/escape-html';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand All @@ -33,6 +34,12 @@ function UnconnectedNavigatorScreen(
props: WordPressComponentProps< NavigatorScreenProps, 'div', false >,
forwardedRef: ForwardedRef< any >
) {
if ( ! /^\//.test( props.path ) ) {
warning(
'wp.components.NavigatorScreen: the `path` should follow a URL-like scheme; it should start with and be separated by the `/` character.'
);
}

const screenId = useId();
const { children, className, path, ...otherProps } = useContextSystem(
props,
Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/navigator/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ describe( 'Navigator', () => {
).toHaveAttribute( 'id', INVALID_HTML_ATTRIBUTE.escaped );
} );

it( 'should warn if the `path` prop does not follow the required format', () => {
render( <NavigatorScreen path="not-valid">Test</NavigatorScreen> );

expect( console ).toHaveWarnedWith(
'wp.components.NavigatorScreen: the `path` should follow a URL-like scheme; it should start with and be separated by the `/` character.'
);
} );

it( 'should match correctly paths with named arguments', async () => {
const user = userEvent.setup();

Expand Down

0 comments on commit 5daa582

Please sign in to comment.