diff --git a/src/components/utils/__tests__/isOfType.test.tsx b/src/components/utils/__tests__/isOfType.test.tsx new file mode 100644 index 0000000000..7946902e3e --- /dev/null +++ b/src/components/utils/__tests__/isOfType.test.tsx @@ -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( + + + , + ); + expect(screen.getByText('correct')).toBeVisible(); + }); + test('should match displayName', () => { + const Component = () => null; + Component.displayName = 'comp1'; + const Component2 = () => null; + Component2.displayName = 'comp1'; + + render( + + + , + ); + 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( + + + , + ); + 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( + + + , + ); + expect(screen.getByText('wrong')).toBeVisible(); + }); + test('should match type of builtin component', () => { + render( + + + , + ); + expect(screen.getByText('correct')).toBeVisible(); + }); + test('should not match if type of builtin component is different', () => { + render( + +