Skip to content

Commit

Permalink
Fix warnings (premieroctet#25)
Browse files Browse the repository at this point in the history
* Fix warnings

* Improve stability

* WIP tests
  • Loading branch information
tlenclos authored Feb 10, 2020
1 parent c3282b0 commit 8ed144c
Show file tree
Hide file tree
Showing 25 changed files with 371 additions and 159 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"@rehooks/local-storage": "^2.1.1",
"@rematch/core": "^1.3.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^25.1.2",
"codesandbox": "^2.1.11",
"coloreact": "^0.3.1",
"emotion-theming": "^10.0.27",
Expand Down
2 changes: 0 additions & 2 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const Header = () => {
Builder mode
</FormLabel>
<Switch
defaultIsChecked={showLayout}
isChecked={showLayout}
color="teal"
size="sm"
Expand All @@ -98,7 +97,6 @@ const Header = () => {
Code panel
</FormLabel>
<Switch
defaultIsChecked={showCode}
isChecked={showCode}
id="code"
color="teal"
Expand Down
109 changes: 109 additions & 0 deletions src/components/editor/ComponentPreview.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react'
import { render } from '@testing-library/react'
import { init } from '@rematch/core'
import { Provider } from 'react-redux'
import { DndProvider } from 'react-dnd'
import Backend from 'react-dnd-html5-backend'
import { ThemeProvider, theme } from '@chakra-ui/core'

import ComponentPreview from './ComponentPreview'
import { storeConfig } from '../../core/store'

function renderWithRedux(
components: React.ReactNode,
{
// @ts-ignore
initialState,
// @ts-ignore
store = init(storeConfig),
} = {},
) {
return {
...render(
<ThemeProvider theme={theme}>
<DndProvider backend={Backend}>
<Provider store={store}>{components}</Provider>
</DndProvider>
</ThemeProvider>,
),
// adding `store` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
store,
}
}

const componentsToTest = [
'Badge',
'Button',
'Icon',
'IconButton',
'Image',
'Text',
'Progress',
'Link',
'Spinner',
'CloseButton',
'Checkbox',
'Divider',
'Code',
'Textarea',
'CircularProgress',
'Heading',
'Tag',
'Switch',
'FormLabel',
'FormHelperText',
'FormErrorMessage',
'TabPanel',
// 'Tab',
'Input',
'Radio',
'ListItem',
'ListIcon',
// 'AlertIcon',
// 'AccordionIcon',
'Box',
'SimpleGrid',
'Flex',
// 'AccordionPanel',
// 'AccordionItem',
'FormControl',
// 'Tabs',
// 'TabList',
// 'TabPanels',
'InputLeftElement',
'InputRightElement',
'List',
'Avatar',
'AvatarBadge',
'AvatarGroup',
'Alert',
'AlertTitle',
'AlertDescription',
'Stack',
'Accordion',
// 'AccordionHeader',
'RadioGroup',
'Select',
'InputGroup',
'InputLeftAddon',
'InputRightAddon',
]

test.each(componentsToTest)('Component Preview for %s', componentName => {
// const spy = jest.spyOn(global.console, 'error')
// @ts-ignore
const store = init(storeConfig)
store.dispatch.components.addComponent({
parentName: 'root',
type: componentName,
rootParentType: componentName,
testId: 'test',
})

// console.log(componentName, store.getState().components.present.components);
// @ts-ignore
renderWithRedux(<ComponentPreview componentName="test" />, { store })
// expect(spy).not.toHaveBeenCalled();
})
26 changes: 19 additions & 7 deletions src/components/editor/ComponentPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import InputGroupPreview from './previews/InputGroupPreview'
import InputLeftAddonPreview from './previews/InputLeftAddonPreview'
import InputRightAddonPreview from './previews/InputRightAddonPreview'
import { getComponentBy } from '../../core/selectors/components'
import WithBoxRefSimplePreviewContainer from './WithRefSimplePreviewContainer'

const ComponentPreview: React.FC<{
componentName: string
}> = ({ componentName }) => {
const component = useSelector(getComponentBy(componentName))
if (!component) {
console.error(`ComponentPreview unavailable for component ${componentName}`)
}

const type = (component && component.type) || null

switch (type) {
Expand All @@ -37,19 +42,14 @@ const ComponentPreview: React.FC<{
case 'IconButton':
case 'Image':
case 'Text':
case 'Progress':
case 'Link':
case 'Spinner':
case 'CloseButton':
case 'Checkbox':
case 'Divider':
case 'Code':
case 'Textarea':
case 'CircularProgress':
case 'Heading':
case 'Tag':
case 'Switch':
case 'AlertIcon':
case 'FormLabel':
case 'FormHelperText':
case 'FormErrorMessage':
Expand All @@ -58,17 +58,29 @@ const ComponentPreview: React.FC<{
case 'Input':
case 'Radio':
case 'ListItem':
case 'ListIcon':
return (
<SimplePreviewContainer component={component} type={Chakra[type]} />
)
// Wrapped functional components
case 'AlertIcon':
case 'Progress':
case 'CloseButton':
case 'AccordionIcon':
case 'Code':
case 'ListIcon':
case 'Tag':
return (
<WithBoxRefSimplePreviewContainer
component={component}
type={Chakra[type]}
/>
)
// Components with childrens
case 'Box':
case 'SimpleGrid':
case 'Flex':
case 'AccordionPanel':
case 'AccordionItem':
case 'AccordionIcon':
case 'FormControl':
case 'Tabs':
case 'TabList':
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/WithChildrenPreviewContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const WithChildrenPreviewContainer: React.FC<{
type,
{ ...props, pos: 'relative', ref: drop(ref) },
component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
)),
)
}
Expand Down
19 changes: 19 additions & 0 deletions src/components/editor/WithRefSimplePreviewContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { FunctionComponent, ComponentClass } from 'react'
import { useInteractive } from '../../hooks/useInteractive'
import { Box } from '@chakra-ui/core'

const WithRefSimplePreviewContainer: React.FC<{
component: IComponent
type: string | FunctionComponent<any> | ComponentClass<any, any>
}> = ({ component, type }) => {
const { props, ref } = useInteractive(component)
let boxProps: any = {}

return (
<Box {...boxProps} ref={ref}>
{React.createElement(type, { ...props })}
</Box>
)
}

export default WithRefSimplePreviewContainer
4 changes: 2 additions & 2 deletions src/components/editor/previews/AccordionPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AccordionPreview: React.FC<IPreviewProps> = ({ component }) => {
<Box ref={drop(ref)} {...boxProps}>
<Accordion {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</Accordion>
</Box>
Expand All @@ -43,7 +43,7 @@ export const AccordionHeaderPreview = ({ component }: IPreviewProps) => {
return (
<AccordionHeader ref={drop(ref)} {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</AccordionHeader>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/previews/AlertPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const AlertPreview: React.FC<IPreviewProps> = ({ component }) => {
<Box ref={drop(ref)} {...boxProps}>
<Alert {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</Alert>
</Box>
Expand Down
3 changes: 2 additions & 1 deletion src/components/editor/previews/AvatarPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const AvatarPreview: React.FC<IPreviewProps & {
<Box ref={drop(ref)} {...boxProps}>
<Avatar ml={index === 0 ? 0 : spacing} {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</Avatar>
</Box>
Expand All @@ -56,6 +56,7 @@ export const AvatarGroupPreview = ({ component }: IPreviewProps) => {
<AvatarGroup {...props}>
{component.children.map((key: string, i: number) => (
<AvatarPreview
key={key}
index={i + 1}
spacing={props.spacing}
component={components[key]}
Expand Down
24 changes: 12 additions & 12 deletions src/components/editor/previews/BoxPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React from "react";
import { Box } from "@chakra-ui/core";
import ComponentPreview from "../ComponentPreview";
import { useDropComponent } from "../../../hooks/useDropComponent";
import { useInteractive } from "../../../hooks/useInteractive";
import React from 'react'
import { Box } from '@chakra-ui/core'
import ComponentPreview from '../ComponentPreview'
import { useDropComponent } from '../../../hooks/useDropComponent'
import { useInteractive } from '../../../hooks/useInteractive'

const BoxPreview: React.FC<{ component: IComponent }> = ({ component }) => {
const { drop, isOver } = useDropComponent(component.id);
const { props, ref } = useInteractive(component, true);
const { drop, isOver } = useDropComponent(component.id)
const { props, ref } = useInteractive(component, true)

if (isOver) {
props.bg = "teal.50";
props.bg = 'teal.50'
}

return (
<Box pos="relative" ref={drop(ref)} {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</Box>
);
};
)
}

export default BoxPreview;
export default BoxPreview
4 changes: 2 additions & 2 deletions src/components/editor/previews/InputGroupPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const InputGroupPreview: React.FC<{ component: IComponent }> = ({

return (
<Box {...boxProps} ref={drop(ref)}>
<InputGroup ref={drop(ref)} {...props}>
<InputGroup {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</InputGroup>
</Box>
Expand Down
24 changes: 12 additions & 12 deletions src/components/editor/previews/RadioPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import React from "react";
import { RadioGroup, Box } from "@chakra-ui/core";
import { useInteractive } from "../../../hooks/useInteractive";
import { useDropComponent } from "../../../hooks/useDropComponent";
import ComponentPreview from "../ComponentPreview";
import React from 'react'
import { RadioGroup, Box } from '@chakra-ui/core'
import { useInteractive } from '../../../hooks/useInteractive'
import { useDropComponent } from '../../../hooks/useDropComponent'
import ComponentPreview from '../ComponentPreview'

export const RadioGroupPreview: React.FC<{ component: IComponent }> = ({
component
component,
}) => {
const { drop, isOver } = useDropComponent(component.id);
const { props, ref } = useInteractive(component, true);
const { drop, isOver } = useDropComponent(component.id)
const { props, ref } = useInteractive(component, true)

const propsBox: any = {};
const propsBox: any = {}

if (isOver) {
props.bg = "teal.50";
props.bg = 'teal.50'
}

return (
<Box {...propsBox} ref={drop(ref)}>
<RadioGroup {...props}>
{component.children.map((key: string) => (
<ComponentPreview componentName={key} />
<ComponentPreview key={key} componentName={key} />
))}
</RadioGroup>
</Box>
);
)
}
23 changes: 13 additions & 10 deletions src/components/inspector/children/ChildrenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ const ChildrenList: React.FC<Props> = ({
}) => {
return (
<Box mx={2} h="100%">
{childrenList.map((child, index) => (
<ChildElement
key={child.id}
type={child.type}
index={index}
moveItem={moveItem}
id={child.id}
onSelect={onSelect}
/>
))}
{childrenList.map(
(child, index) =>
child && (
<ChildElement
key={child.id}
type={child.type}
index={index}
moveItem={moveItem}
id={child.id}
onSelect={onSelect}
/>
),
)}
</Box>
)
}
Expand Down
Loading

0 comments on commit 8ed144c

Please sign in to comment.