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

refactor!: Remove 1.x.x and 2.x.x deprecated properties #1988

Merged
merged 21 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 17 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
5 changes: 4 additions & 1 deletion src/components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const multiselectable = (): React.ReactElement => (
<Accordion items={testItems} multiselectable={true} />
)

const customTestItems = [
const customTestItems: AccordionItemProps[] = [
{
title: (
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
Expand All @@ -130,6 +130,7 @@ const customTestItems = [
expanded: false,
id: '123',
className: 'myCustomAccordionItem',
headingLevel: 'h4',
},
{
title: 'Second Amendment',
Expand All @@ -148,6 +149,7 @@ const customTestItems = [
),
expanded: false,
id: 'abc',
headingLevel: 'h4',
},
{
title: 'Third Amendment',
Expand All @@ -160,6 +162,7 @@ const customTestItems = [
),
expanded: false,
id: 'def',
headingLevel: 'h4',
},
]

Expand Down
124 changes: 50 additions & 74 deletions src/components/Accordion/Accordion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react'
import { render, fireEvent } from '@testing-library/react'

import { Accordion, AccordionItemProps } from './Accordion'
import { deprecationWarning } from '../../deprecation'
jest.mock('../../deprecation')

const firstAmendment = (
<p>
Expand Down Expand Up @@ -102,9 +100,14 @@ describe('Accordion component', () => {
})

it('renders a header and content for each item', () => {
const { getByTestId } = render(<Accordion items={testItems} />)
const { getByTestId, getAllByRole } = render(
<Accordion items={testItems} />
)
const accordionEl = getByTestId('accordion')
const headings = getAllByRole('heading', { level: 4 })

expect(accordionEl.childElementCount).toBe(testItems.length * 2)
expect(headings.length).toEqual(testItems.length)
})

it('no items are open by default', () => {
Expand Down Expand Up @@ -257,6 +260,7 @@ describe('Accordion component', () => {
content: thirdAmendment,
expanded: false,
id: 'def',
headingLevel: 'h4',
},
{
title: 'Fourth Amendment',
Expand Down Expand Up @@ -327,77 +331,49 @@ describe('Accordion component', () => {
)
})
})
describe('custom headingLevel for AccordionItems', () => {
it('passes on the headingLevel', () => {
const customTestItems: AccordionItemProps[] = [
{
title: 'First Amendment',
content: firstAmendment,
expanded: false,
headingLevel: 'h2',
id: '123',
className: 'myCustomAccordionItem',
},
{
title: 'Second Amendment',
content: secondAmendment,
expanded: false,
headingLevel: 'h2',

id: 'abc',
},
{
title: 'Third Amendment',
content: thirdAmendment,
expanded: false,
headingLevel: 'h2',
id: 'def',
},
]

const { getAllByRole } = render(<Accordion items={customTestItems} />)

const headings = getAllByRole('heading', { level: 2 })
expect(headings.length).toEqual(customTestItems.length)
})

it('uses headingLevel h4 by default', () => {
const { getAllByRole } = render(<Accordion items={testItems} />)

const headings = getAllByRole('heading', { level: 4 })
expect(headings.length).toEqual(testItems.length)
})
})

describe('default headingLevel for AccordionItems', () => {
it('uses the default headingLevel and warns deprecation', () => {
const customTestItems: AccordionItemProps[] = [
{
title: 'First Amendment',
content: firstAmendment,
expanded: false,
id: '123',
className: 'myCustomAccordionItem',
},
{
title: 'Second Amendment',
content: secondAmendment,
expanded: false,
id: 'abc',
},
{
title: 'Third Amendment',
content: thirdAmendment,
expanded: false,
id: 'def',
},
]

const { getAllByRole } = render(<Accordion items={customTestItems} />)

const headings = getAllByRole('heading', { level: 4 })
expect(headings.length).toEqual(customTestItems.length)
expect(deprecationWarning).toHaveBeenCalledTimes(customTestItems.length)
})
describe('custom headingLevel for AccordionItems', () => {
const scenarios: [HeadingLevel, number][] = [
['h1', 1],
['h2', 2],
['h3', 3],
['h4', 4],
['h5', 5],
['h6', 6],
]
it.each(scenarios)(
'can render with headingLevel %s',
(headingLevel, expectedLevel) => {
const customTestItems: AccordionItemProps[] = [
{
title: 'First Amendment',
content: firstAmendment,
expanded: false,
headingLevel,
id: '123',
className: 'myCustomAccordionItem',
},
{
title: 'Second Amendment',
content: secondAmendment,
expanded: false,
headingLevel,

id: 'abc',
},
{
title: 'Third Amendment',
content: thirdAmendment,
expanded: false,
headingLevel,
id: 'def',
},
]

const { getAllByRole } = render(<Accordion items={customTestItems} />)
const headings = getAllByRole('heading', { level: expectedLevel })
expect(headings.length).toEqual(customTestItems.length)
}
)
})
})
10 changes: 2 additions & 8 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React, { useState } from 'react'
import classnames from 'classnames'
import { deprecationWarning } from '../../deprecation'

export interface AccordionItemProps {
title: React.ReactNode | string
content: React.ReactNode
expanded: boolean
id: string
className?: string
headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
headingLevel: HeadingLevel
handleToggle?: (event: React.MouseEvent<HTMLButtonElement>) => void
}

Expand All @@ -35,12 +34,7 @@ export const AccordionItem = ({
className
)

if (!headingLevel) {
deprecationWarning(
'Default headingLevel h4 has been deprecated. Please specify a heading level.'
)
}
const Heading = headingLevel || 'h4'
const Heading = headingLevel

return (
<>
Expand Down
25 changes: 13 additions & 12 deletions src/components/Alert/Alert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,50 @@ export const info = (): React.ReactElement => (

export const slim = (): React.ReactElement => (
<>
<Alert type="success" slim>
<Alert type="success" headingLevel="h4" slim>
{testText}
</Alert>
<Alert type="warning" slim>
<Alert type="warning" headingLevel="h4" slim>
{testText}
</Alert>
<Alert type="error" slim>
<Alert type="error" headingLevel="h4" slim>
{testText}
</Alert>
<Alert type="info" slim>
<Alert type="info" headingLevel="h4" slim>
{testText}
</Alert>
</>
)

export const noIcon = (): React.ReactElement => (
<>
<Alert type="success" noIcon>
<Alert type="success" headingLevel="h4" noIcon>
{testText}
</Alert>
<Alert type="warning" noIcon>
<Alert type="warning" headingLevel="h4" noIcon>
{testText}
</Alert>
<Alert type="error" noIcon>
<Alert type="error" headingLevel="h4" noIcon>
{testText}
</Alert>
<Alert type="info" noIcon>
<Alert type="info" headingLevel="h4" noIcon>
{testText}
</Alert>
</>
)

export const slimNoIcon = (): React.ReactElement => (
<>
<Alert type="success" slim noIcon>
<Alert type="success" headingLevel="h4" slim noIcon>
{testText}
</Alert>
<Alert type="warning" slim noIcon>
<Alert type="warning" headingLevel="h4" slim noIcon>
{testText}
</Alert>
<Alert type="error" slim noIcon>
<Alert type="error" headingLevel="h4" slim noIcon>
{testText}
</Alert>
<Alert type="info" slim noIcon>
<Alert type="info" headingLevel="h4" slim noIcon>
{testText}
</Alert>
</>
Expand All @@ -113,6 +113,7 @@ export const withCTA = (): React.ReactElement => (
<Alert
type="warning"
heading="Warning status"
headingLevel="h4"
cta={
<Button type="button" outline>
Click here
Expand Down
47 changes: 30 additions & 17 deletions src/components/Alert/Alert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ import React from 'react'
import { render } from '@testing-library/react'

import { Alert } from './Alert'
import { deprecationWarning } from '../../deprecation'
jest.mock('../../deprecation')

describe('Alert component', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('renders without errors', () => {
const { queryByTestId } = render(<Alert type="success" />)
const { queryByTestId } = render(<Alert type="success" headingLevel="h4" />)
expect(queryByTestId('alert')).toBeInTheDocument()
})

it('renders children in <p> tag by default', () => {
const { queryByTestId } = render(
<Alert type="success" className="myClass">
<Alert type="success" headingLevel="h4" className="myClass">
Test children
</Alert>
)
Expand All @@ -27,7 +25,7 @@ describe('Alert component', () => {

it('renders validation style alert', () => {
const { queryByTestId } = render(
<Alert type="success" validation className="myClass">
<Alert type="success" validation headingLevel="h4" className="myClass">
Test children
</Alert>
)
Expand All @@ -38,28 +36,43 @@ describe('Alert component', () => {

it('accepts className prop', () => {
const { queryByTestId } = render(
<Alert type="success" className="myClass" />
<Alert type="success" headingLevel="h4" className="myClass" />
)
expect(queryByTestId('alert')).toHaveClass('myClass')
})

it('accepts a headingLevel', () => {
const { getByRole } = render(
<Alert type="success" headingLevel="h2" heading="Working Alert" />
describe('with custom heading levels', () => {
const scenarios: [HeadingLevel, number][] = [
['h1', 1],
['h2', 2],
['h3', 3],
['h4', 4],
['h5', 5],
['h6', 6],
]
it.each(scenarios)(
'can render with headingLevel %s',
(headingLevel, expectedLevel) => {
const { getByRole } = render(
<Alert
type="success"
headingLevel={headingLevel}
heading="Working Alert"
/>
)
expect(
getByRole('heading', { level: expectedLevel })
).toBeInTheDocument()
}
)
expect(getByRole('heading', { level: 2 })).toBeInTheDocument()
})

it('uses heading level 4 by default and warns deprecation', () => {
const { getByRole } = render(<Alert type="info" heading="Working Alert" />)
expect(getByRole('heading', { level: 4 })).toBeInTheDocument()
expect(deprecationWarning).toHaveBeenCalledTimes(1)
})

describe('with a CTA', () => {
it('renders the CTA', () => {
const testCTA = <button type="button">Click Here</button>
const { queryByText } = render(<Alert type="success" cta={testCTA} />)
const { queryByText } = render(
<Alert type="success" headingLevel="h4" cta={testCTA} />
)
expect(queryByText('Click Here')).toBeInTheDocument()
})
})
Expand Down
Loading