Skip to content

Commit e1c4a52

Browse files
author
Luke Bowerman
authored
Styled Components 5 prep Test fix-ups (#1544)
Refactored DialogContent to make remove class component, removed problematic test and replaced with Storybook image-snapshot Remove `FieldText.stories` storybook - it wasn't useful Replaced instances of `toHaveStyle` with `toHaveStyleRule` (former doesn't work well with SC5) Fix recursive interface
1 parent e606408 commit e1c4a52

File tree

21 files changed

+209
-202
lines changed

21 files changed

+209
-202
lines changed

packages/components-theme-editor/src/ThemeEditor.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626

2727
import { IconButton, Dialog } from '@looker/components'
2828
import React, { FC } from 'react'
29-
import {
30-
ThemeEditorContent,
31-
ThemeEditorContentProps,
32-
} from './ThemeEditorContent'
29+
import { ThemeEditorContent } from './ThemeEditorContent'
3330
import { ThemeEditableProps } from './types'
3431

35-
export interface ThemeEditorProps extends ThemeEditorContentProps {
32+
export interface ThemeEditorProps {
3633
hasCustomTheme: boolean
3734
updateTheme: (customTheme?: ThemeEditableProps) => void
3835
}
502 Bytes
Loading
3.13 KB
Loading

packages/components/src/Button/IconButton.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ describe('IconButton', () => {
107107
)
108108

109109
const image = getByTestId('icon-layout')
110-
expect(image).toHaveStyle('width: 44')
111-
expect(image).toHaveStyle('height: 44')
110+
expect(image).toHaveStyleRule('width: 44')
111+
expect(image).toHaveStyleRule('height: 44')
112112
})
113113

114114
xtest('renders focus ring on tab input but not on click', () => {

packages/components/src/Chip/Chip.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('Chip renders disabled', () => {
3838

3939
test('Chip accepts a prefix and renders it with correct style', () => {
4040
const { getByText } = renderWithTheme(<Chip prefix="role">admin</Chip>)
41-
expect(getByText(/\brole\b/)).toHaveStyle('font-weight: 400')
41+
expect(getByText(/\brole\b/)).toHaveStyleRule('font-weight: 400')
4242
})
4343

4444
test('Chip onDelete renders correctly', () => {

packages/components/src/Dialog/Confirm/Confirm.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test('<Confirm/> with defaults', async () => {
6565

6666
expect(getByText(requiredProps.title)).toBeVisible()
6767
expect(getByText(requiredProps.message)).toBeVisible()
68-
expect(button).toHaveStyle(`background: ${theme.colors.key}`)
68+
expect(button).toHaveStyleRule(`background: ${theme.colors.key}`)
6969

7070
fireEvent.click(button)
7171
expect(requiredProps.onConfirm).toHaveBeenCalledTimes(1)
@@ -86,7 +86,7 @@ test('<Confirm/> with custom props', () => {
8686
fireEvent.click(opener)
8787

8888
const button = getByText(optionalProps.confirmLabel || '')
89-
expect(button).toHaveStyle(`background: ${theme.colors.critical}`)
89+
expect(button).toHaveStyleRule(`background: ${theme.colors.critical}`)
9090

9191
fireEvent.click(getByText(optionalProps.cancelLabel || ''))
9292
fireEvent.click(button)

storybook/src/FieldText.stories.tsx renamed to packages/components/src/Dialog/Layout/DialogContent.story.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,29 @@
2525
*/
2626

2727
import React from 'react'
28-
import { FieldText, FieldTextProps } from '@looker/components'
2928
import { Story } from '@storybook/react/types-6-0'
29+
import { Box, Paragraph } from '../..'
30+
import { DialogContent, DialogContentProps } from './DialogContent'
3031

31-
const Template: Story<FieldTextProps> = (args) => <FieldText {...args} />
32+
const Template: Story<DialogContentProps> = (args) => (
33+
<DialogContent {...args} />
34+
)
3235

33-
export const Primary = Template.bind({})
34-
Primary.args = {
35-
children: 'Filed Text',
36+
export const Basic = Template.bind({})
37+
Basic.args = {
38+
children: <Box height="2rem" bg="rebeccapurple" />,
3639
}
3740

41+
export const Overflow = () => (
42+
<Box height="10rem" display="flex" bg="white" p="large">
43+
<DialogContent>
44+
<Paragraph>Scroll down here...</Paragraph>
45+
<Box height="6rem" bg="rebeccapurple" />
46+
</DialogContent>
47+
</Box>
48+
)
49+
3850
export default {
39-
component: FieldText,
40-
title: 'FieldText',
51+
component: DialogContent,
52+
title: 'DialogContent',
4153
}

packages/components/src/Dialog/Layout/DialogContent.test.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
import 'jest-styled-components'
2828
import React from 'react'
29-
import { theme } from '@looker/design-tokens'
30-
import { assertSnapshot, renderWithTheme } from '@looker/components-test-utils'
29+
import { assertSnapshot } from '@looker/components-test-utils'
3130
import { DialogContent } from './DialogContent'
3231

3332
describe('DialogContent', () => {
@@ -42,21 +41,4 @@ describe('DialogContent', () => {
4241
</DialogContent>
4342
)
4443
})
45-
46-
test('borderBottom', () => {
47-
const { getByText } = renderWithTheme(
48-
<>
49-
<DialogContent>No border</DialogContent>
50-
<DialogContent borderBottom>Has border</DialogContent>
51-
</>
52-
)
53-
54-
const noBorder = getByText('No border')
55-
expect(noBorder.parentNode).toHaveStyle('border-bottom: none')
56-
57-
const hasBorder = getByText('Has border')
58-
expect(hasBorder.parentNode).toHaveStyle(
59-
`border-bottom: 1px solid ${theme.colors.ui2};`
60-
)
61-
})
6244
})

packages/components/src/Dialog/Layout/DialogContent.tsx

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
LayoutProps,
3333
layout,
3434
} from '@looker/design-tokens'
35-
import React, { Component, createRef, RefObject } from 'react'
35+
import React, { FC, useRef, useState, useEffect } from 'react'
3636
import ReactResizeDetector from 'react-resize-detector'
3737
import styled from 'styled-components'
3838
import omit from 'lodash/omit'
@@ -46,79 +46,49 @@ export interface DialogContentProps
4646
* touch the container edges.
4747
*/
4848
innerProps?: SpaceProps
49-
50-
borderBottom?: boolean
51-
}
52-
53-
interface ContentState {
54-
overflow: boolean
5549
}
5650

57-
interface InternalContentProps extends DialogContentProps {
51+
interface DialogContentLayoutProps extends DialogContentProps {
5852
renderedHeight: string
5953
}
6054

61-
class Internal extends Component<InternalContentProps, ContentState> {
62-
private ref: RefObject<HTMLDivElement>
63-
64-
constructor(props: InternalContentProps) {
65-
super(props)
66-
this.state = { overflow: false }
67-
this.ref = createRef()
68-
}
69-
70-
public hasOverflow(e: HTMLDivElement) {
71-
return e.offsetHeight < e.scrollHeight
72-
}
73-
74-
public componentDidUpdate(prevProps: InternalContentProps) {
75-
if (prevProps.renderedHeight !== this.props.renderedHeight) {
76-
this.ref.current &&
77-
this.setState({ overflow: this.hasOverflow(this.ref.current) })
78-
}
79-
}
80-
81-
public render() {
82-
const {
83-
borderBottom,
84-
children,
85-
className,
86-
innerProps,
87-
...props
88-
} = this.props
89-
90-
if (innerProps && innerProps.p && !innerProps.px) {
91-
innerProps.px = innerProps.p
55+
const DialogContentLayout: FC<DialogContentLayoutProps> = ({
56+
children,
57+
className,
58+
innerProps,
59+
renderedHeight,
60+
...props
61+
}) => {
62+
const internalRef = useRef<HTMLDivElement>(null)
63+
const [overflow, setOverflow] = useState(false)
64+
65+
useEffect(() => {
66+
const container = internalRef.current
67+
68+
if (container) {
69+
setOverflow(container.offsetHeight < container.scrollHeight)
9270
}
71+
}, [renderedHeight])
9372

94-
return (
95-
<Outer
96-
borderBottom={borderBottom}
97-
className={`${className} ${this.state.overflow && 'overflow'}`}
98-
ref={this.ref}
99-
{...omit(props, ['renderedHeight'])}
100-
>
101-
<Inner {...innerProps}>{children}</Inner>
102-
</Outer>
103-
)
73+
if (innerProps && innerProps.p && !innerProps.px) {
74+
innerProps.px = innerProps.p
10475
}
105-
}
10676

107-
export const DialogContent = (props: DialogContentProps) => {
10877
return (
109-
<ReactResizeDetector handleHeight>
110-
{(height: string) => <Internal renderedHeight={height} {...props} />}
111-
</ReactResizeDetector>
78+
<div
79+
className={`${className} ${overflow ? 'overflow' : ''}`}
80+
ref={internalRef}
81+
{...omit(props, ['renderedHeight'])}
82+
>
83+
<Inner {...innerProps}>{children}</Inner>
84+
</div>
11285
)
11386
}
11487

115-
const Outer = styled.div<{ borderBottom?: boolean }>`
88+
const DialogContentStyled = styled(DialogContentLayout)`
11689
${reset}
11790
${layout}
11891
119-
${({ borderBottom, theme: { colors } }) =>
120-
borderBottom && `border-bottom: 1px solid ${colors.ui2};`}
121-
12292
flex: 1 1 auto;
12393
overflow: auto;
12494
@@ -129,6 +99,16 @@ const Outer = styled.div<{ borderBottom?: boolean }>`
12999
}
130100
`
131101

102+
export const DialogContent = (props: DialogContentProps) => {
103+
return (
104+
<ReactResizeDetector handleHeight>
105+
{(height: string) => (
106+
<DialogContentStyled renderedHeight={height} {...props} />
107+
)}
108+
</ReactResizeDetector>
109+
)
110+
}
111+
132112
const Inner = styled.div<SpaceProps>`
133113
${reset}
134114
${space}

packages/components/src/Dialog/Layout/__snapshots__/DialogContent.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports[`DialogContent Snapshot - No overflow 1`] = `
2424
}
2525
2626
<div
27-
className="c0 undefined false"
27+
className="c0 "
2828
>
2929
<div
3030
className="c1"
@@ -66,7 +66,7 @@ exports[`DialogContent Snapshot 1`] = `
6666
}
6767
6868
<div
69-
className="c0 undefined false"
69+
className="c0 "
7070
>
7171
<div
7272
className="c1"

0 commit comments

Comments
 (0)