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

Test componentToHints, exclude test files from be published #381

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.test.*

Comment on lines +1 to +2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this with npm pack:

Before: 128 files being packaged.
After: 124 files being packaged.

There are exactly 4 test files inside src, so this seems correct.

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>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ComponentType defaults to a component with no props, specifying any here permits components with or without props, which is obviously more accurate than before.

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, unknown>
>;
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({
Foo: PropTypeComponents.Foo,
Bar: PropTypeComponents.Bar,
});

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(
{
Foo: TypeScriptComponents.Foo,
Bar: TypeScriptComponents.Bar,
},
{
Foo: { color: ['red', 'blue', 'black'] },
Bar: { 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__;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this into Playroom.tsx makes this function much easier to test as there's no need to modify globals in the tests.


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.
Loading