Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): correctly check type of element #1560

Merged
merged 1 commit into from
May 2, 2024
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
76 changes: 76 additions & 0 deletions src/components/utils/__tests__/isOfType.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';

import {render, screen} from '../../../../test-utils/utils';
import {isOfType} from '../isOfType';

function Test(props: React.PropsWithChildren<{matcher: (c: unknown) => boolean}>) {
const child = React.Children.only(props.children);

return props.matcher(child) ? 'correct' : 'wrong';
}

describe('isOfType', () => {
test('should match type of component', () => {
const Component = () => null;

render(
<Test matcher={isOfType(Component)}>
<Component />
</Test>,
);
expect(screen.getByText('correct')).toBeVisible();
});
test('should match displayName', () => {
const Component = () => null;
Component.displayName = 'comp1';
const Component2 = () => null;
Component2.displayName = 'comp1';

render(
<Test matcher={isOfType(Component)}>
<Component2 />
</Test>,
);
expect(screen.getByText('correct')).toBeVisible();
});
test('should not match if type and displayName do not match', () => {
const Component = () => null;
Component.displayName = 'comp1';
const Component2 = () => null;
Component2.displayName = 'comp2';

render(
<Test matcher={isOfType(Component)}>
<Component2 />
</Test>,
);
expect(screen.getByText('wrong')).toBeVisible();
});
test('should not match if type do not match and displayName is absent', () => {
const Component = () => null;
const Component2 = () => null;

render(
<Test matcher={isOfType(Component)}>
<Component2 />
</Test>,
);
expect(screen.getByText('wrong')).toBeVisible();
});
test('should match type of builtin component', () => {
render(
<Test matcher={isOfType('svg')}>
<svg />
</Test>,
);
expect(screen.getByText('correct')).toBeVisible();
});
test('should not match if type of builtin component is different', () => {
render(
<Test matcher={isOfType('svg')}>
<button />
</Test>,
);
expect(screen.getByText('wrong')).toBeVisible();
});
});
19 changes: 12 additions & 7 deletions src/components/utils/isOfType.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import React from 'react';

export function isOfType<P = {}>(Component: React.ComponentType<P>) {
export function isOfType<P = {}>(Component: React.ComponentType<P> | string) {
return function isMatching(
component: React.ReactNode,
): component is React.ReactElement<P, typeof React.Component> {
component: unknown,
): component is React.ReactElement<P, typeof Component> {
if (!React.isValidElement(component)) {
return false;
}

const {type} = component;
if (type === Component) {
return true;
}

if (typeof Component === 'string' || typeof type === 'string') {
return false;
}

return (
type === React.Component ||
(type as React.ComponentType).displayName === Component.displayName
);
const displayName = (type as React.ComponentType).displayName;
return Boolean(displayName && displayName === Component.displayName);
};
}
Loading