Skip to content

Commit

Permalink
Test componentToHints, exclude test files from be published (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis authored Dec 10, 2024
1 parent fb14616 commit 51d14d3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.test.*

6 changes: 4 additions & 2 deletions src/Playroom/Playroom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { Box } from './Box/Box';

import { assignInlineVars } from '@vanilla-extract/dynamic';

const staticTypes = __PLAYROOM_GLOBAL__STATIC_TYPES__;

const resizableConfig = (position: EditorPosition = 'bottom') => ({
top: position === 'bottom',
right: false,
Expand Down Expand Up @@ -59,7 +61,7 @@ const getTitle = (title: string | undefined) => {
};

export interface PlayroomProps {
components: Record<string, ComponentType>;
components: Record<string, ComponentType<any>>;
themes: string[];
widths: number[];
snippets: Snippets;
Expand Down Expand Up @@ -122,7 +124,7 @@ export default ({ components, themes, widths, snippets }: PlayroomProps) => {
dispatch({ type: 'updateCode', payload: { code: newCode } })
}
previewCode={previewEditorCode}
hints={componentsToHints(components)}
hints={componentsToHints(components, staticTypes)}
/>
<StatusMessage />
</div>
Expand Down
5 changes: 4 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ interface Window {
}

declare const __PLAYROOM_GLOBAL__CONFIG__: InternalPlayroomConfig;
declare const __PLAYROOM_GLOBAL__STATIC_TYPES__: any;
declare const __PLAYROOM_GLOBAL__STATIC_TYPES__: Record<
string,
Record<string, string[]>
>;
70 changes: 70 additions & 0 deletions src/utils/componentsToHints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import componentsToHints from './componentsToHints';
// @ts-expect-error
import * as PropTypeComponents from '../../cypress/projects/themed/components';
import * as TypeScriptComponents from '../../cypress/projects/typescript/components';

describe('componentsToHints', () => {
it('should support javascript components with proptypes', () => {
const result = componentsToHints({
Bar: PropTypeComponents.Bar,
Foo: PropTypeComponents.Foo,
});

expect(result).toMatchInlineSnapshot(`
{
"Bar": {
"attrs": {
"color": [
"red",
"blue",
],
},
},
"Foo": {
"attrs": {
"color": [
"red",
"blue",
],
},
},
}
`);
});

it('should support typescript components when provided with type data', () => {
const result = componentsToHints(
{
Bar: TypeScriptComponents.Bar,
Foo: TypeScriptComponents.Foo,
},
{
Bar: { color: ['red', 'blue', 'black'] },
Foo: { color: ['red', 'blue', 'black'] },
}
);

expect(result).toMatchInlineSnapshot(`
{
"Bar": {
"attrs": {
"color": [
"red",
"blue",
"black",
],
},
},
"Foo": {
"attrs": {
"color": [
"red",
"blue",
"black",
],
},
},
}
`);
});
});
7 changes: 4 additions & 3 deletions src/utils/componentsToHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import omit from 'lodash/omit';
import parsePropTypes from 'parse-prop-types';
import type { PlayroomProps } from '../Playroom/Playroom';

const staticTypes = __PLAYROOM_GLOBAL__STATIC_TYPES__;

export default (components: PlayroomProps['components']) => {
export default (
components: PlayroomProps['components'],
staticTypes: typeof __PLAYROOM_GLOBAL__STATIC_TYPES__ = {}
) => {
const componentNames = Object.keys(components).sort();

return Object.assign(
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 51d14d3

Please sign in to comment.