Skip to content

Commit

Permalink
Check for exotic component fixes #82
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Oct 22, 2019
1 parent 1111666 commit 6dc0fb0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.8.1 (tbd)

* Improved default template
* Fixed misidentified React class components (#82)
* Added `piral-axios` library

## 0.8.0 (October 21, 2019)

Expand Down
80 changes: 80 additions & 0 deletions src/packages/piral-core/src/utils/components.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as React from 'react';
import { convertComponent } from './components';
import { ComponentConverters, HtmlComponent } from '../types';

describe('Component Converters Module', () => {
it('React FC is correctly marked', () => {
const Component = () => <div />;
const converters: ComponentConverters<any> = {
html() {
return undefined;
},
};
const result = convertComponent(converters, Component, 'test');
expect(result).toHaveProperty('displayName', 'test');
expect(result).not.toHaveProperty('converted');
});

it('React class component is correctly marked', () => {
const Component = class extends React.Component {
render() {
return <div />;
}
};
const converters: ComponentConverters<any> = {
html() {
return undefined;
},
};
const result = convertComponent(converters, Component, 'test');
expect(result).toHaveProperty('displayName', 'test');
expect(result).not.toHaveProperty('converted');
});

it('React exotic component is correctly marked', () => {
const Component = React.lazy(() =>
Promise.resolve().then(() => ({
default: () => <div />,
})),
);
const converters: ComponentConverters<any> = {
html() {
return undefined;
},
};
const result = convertComponent(converters, Component, 'test');
expect(result).toHaveProperty('displayName', 'test');
expect(result).not.toHaveProperty('converted');
});

it('HTML component is correctly marked', () => {
const Component: HtmlComponent<any> = {
type: 'html',
render() {
return undefined;
},
};
const converters: ComponentConverters<any> = {
html(): any {
return {
converted: true,
};
},
};
const result = convertComponent(converters, Component, 'test');
expect(result).not.toHaveProperty('displayName');
expect(result).toHaveProperty('converted', true);
});

it('Unknown component throws', () => {
const Component = {
type: 'foo',
};
const converters: ComponentConverters<any> = {
html() {
return undefined;
},
};
expect(() => convertComponent(converters, Component as any, 'test')).toThrow();
});
});
8 changes: 6 additions & 2 deletions src/packages/piral-core/src/utils/components.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ComponentType } from 'react';
import { ComponentType, ExoticComponent } from 'react';
import { AnyComponent, ComponentConverters } from '../types';

function isNotExotic(component: any): component is object {
return !(component as ExoticComponent).$$typeof;
}

export function markReact<T>(arg: ComponentType<T>, displayName: string) {
if (arg && !arg.displayName) {
arg.displayName = displayName;
Expand All @@ -12,7 +16,7 @@ export function convertComponent<T>(
component: AnyComponent<T>,
displayName: string,
) {
if (typeof component === 'object') {
if (typeof component === 'object' && isNotExotic(component)) {
const converter = converters[component.type];

if (typeof converter !== 'function') {
Expand Down

0 comments on commit 6dc0fb0

Please sign in to comment.