Skip to content

Commit

Permalink
update some components tests and add react-redux to package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Feb 28, 2024
1 parent 6fcf0de commit 5acb14d
Show file tree
Hide file tree
Showing 30 changed files with 193 additions and 242 deletions.
3 changes: 3 additions & 0 deletions components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"redux": "4.0.5",
"styled-components": "5.3.6"
},
"devDependencies": {
"react-redux": "8.1.2"
},
"browser": {
"jest-when": false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import { describe, beforeEach, afterEach } from 'vitest'
import { describe, beforeEach, afterEach, vi, expect } from 'vitest'
import { fireEvent, screen } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { ALIGN_CENTER, JUSTIFY_CENTER } from '../../../styles'
import { renderWithProviders } from '../../../testing/utils'
import { COLORS } from '../../../helix-design-system'
Expand All @@ -16,7 +17,7 @@ describe('CheckboxField', () => {

beforeEach(() => {
props = {
onChange: jest.fn(),
onChange: vi.fn(),
value: false,
name: 'mockCheckboxField',
label: 'checkMockCheckboxField',
Expand All @@ -26,7 +27,7 @@ describe('CheckboxField', () => {
})

afterEach(() => {
jest.resetAllMocks()
vi.resetAllMocks()
})

it('renders label with correct style', () => {
Expand Down
20 changes: 7 additions & 13 deletions components/src/atoms/buttons/__tests__/AlertPrimaryButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'jest-styled-components'
import * as React from 'react'
import { describe, it, beforeEach, expect } from 'vitest'
import { screen } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { renderWithProviders } from '../../../testing/utils'
import { COLORS } from '../../../helix-design-system'
import { BORDERS, TYPOGRAPHY, SPACING } from '../../../ui-style-constants'
Expand All @@ -20,8 +22,8 @@ describe('AlertPrimaryButton', () => {
})

it('renders alert primary button with text', () => {
const { getByText } = render(props)
const button = getByText('alert primary button')
render(props)
const button = screen.getByText('alert primary button')
expect(button).toHaveStyle(`background-color: ${COLORS.red50}`)
expect(button).toHaveStyle(
`padding: ${SPACING.spacing8} ${SPACING.spacing16} ${SPACING.spacing8} ${SPACING.spacing16}`
Expand All @@ -38,16 +40,8 @@ describe('AlertPrimaryButton', () => {

it('renders alert primary button with text and disabled', () => {
props.disabled = true
const { getByText } = render(props)
const button = getByText('alert primary button')
render(props)
const button = screen.getByText('alert primary button')
expect(button).toBeDisabled()
})

it('applies the correct states to the button - hover', () => {
const { getByText } = render(props)
const button = getByText('alert primary button')
expect(button).toHaveStyleRule('box-shadow', '0 0 0', {
modifier: ':hover',
})
})
})
59 changes: 23 additions & 36 deletions components/src/atoms/buttons/__tests__/PrimaryButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'jest-styled-components'
import * as React from 'react'
import { describe, it, beforeEach, expect } from 'vitest'
import { fireEvent, screen } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { renderWithProviders } from '../../../testing/utils'
import { COLORS } from '../../../helix-design-system'
import { BORDERS, TYPOGRAPHY, SPACING } from '../../../ui-style-constants'
Expand All @@ -19,9 +21,9 @@ describe('PrimaryButton', () => {
})

it('renders primary button with text', () => {
const { getByText } = render(props)
const button = getByText('primary button')
expect(button).toHaveStyle(`background-color: ${COLORS.blue50}`)
render(props)
const button = screen.getByText('primary button')
expect(button).toHaveStyle(`background-color: ${COLORS.blue60}`)
expect(button).toHaveStyle(
`padding: ${SPACING.spacing8} ${SPACING.spacing16} ${SPACING.spacing8} ${SPACING.spacing16}`
)
Expand All @@ -38,53 +40,38 @@ describe('PrimaryButton', () => {

it('renders primary button with text and disabled', () => {
props.disabled = true
const { getByText } = render(props)
const button = getByText('primary button')
render(props)
const button = screen.getByText('primary button')
expect(button).toBeDisabled()
expect(button).toHaveStyle(`background-color: ${COLORS.grey30}`)
expect(button).toHaveStyle(`color: ${COLORS.grey50}`)
})

it('applies the correct states to the button - focus', () => {
const { getByText } = render(props)
const button = getByText('primary button')
expect(button).toHaveStyleRule('background-color', `${COLORS.blue55}`, {
modifier: ':focus',
})
render(props)
const button = screen.getByText('primary button')
fireEvent.focus(button)
expect(button).toHaveStyle(`background-color: ${COLORS.blue55}`)
})

it('applies the correct states to the button - hover', () => {
const { getByText } = render(props)
const button = getByText('primary button')
expect(button).toHaveStyleRule('background-color', `${COLORS.blue55}`, {
modifier: ':hover',
})
})

it('applies the correct states to the button - active', () => {
const { getByText } = render(props)
const button = getByText('primary button')
expect(button).toHaveStyleRule('background-color', `${COLORS.blue60}`, {
modifier: ':active',
})
render(props)
const button = screen.getByText('primary button')
fireEvent.mouseOver(button)
expect(button).toHaveStyle(`background-color: ${COLORS.blue55}`)
})

it('applies the correct states to the button - focus-visible', () => {
const { getByText } = render(props)
const button = getByText('primary button')
expect(button).toHaveStyleRule(
'box-shadow',
`0 0 0 3px ${COLORS.yellow50}`,
{
modifier: ':focus-visible',
}
)
it('applies the correct states to the button - default', () => {
render(props)
const button = screen.getByText('primary button')
fireEvent.mouseLeave(button)
expect(button).toHaveStyle(`background-color: ${COLORS.blue50}`)
})

it('renders primary button with text and different background color', () => {
props.backgroundColor = COLORS.red50
const { getByText } = render(props)
const button = getByText('primary button')
render(props)
const button = screen.getByText('primary button')
expect(button).toHaveStyle(`background-color: ${COLORS.red50}`)
expect(button).toHaveStyle(`color: ${COLORS.white}`)
})
Expand Down
38 changes: 10 additions & 28 deletions components/src/atoms/buttons/__tests__/SecondaryButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'jest-styled-components'
import * as React from 'react'
import { describe, it, beforeEach, expect } from 'vitest'
import { screen } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { renderWithProviders } from '../../../testing/utils'
import { BORDERS, TYPOGRAPHY, SPACING } from '../../../ui-style-constants'
import { COLORS } from '../../../helix-design-system'
Expand All @@ -20,8 +22,8 @@ describe('SecondaryButton', () => {
})

it('renders primary button with text', () => {
const { getByText } = render(props)
const button = getByText('secondary button')
render(props)
const button = screen.getByText('secondary button')
expect(button).toHaveStyle(`background-color: ${COLORS.transparent}`)
expect(button).toHaveStyle(
`padding: ${SPACING.spacing8} ${SPACING.spacing16}`
Expand All @@ -33,40 +35,20 @@ describe('SecondaryButton', () => {
expect(button).toHaveStyle(
`text-transform: ${TYPOGRAPHY.textTransformNone}`
)
expect(button).toHaveStyle(`color: ${COLORS.blue50}`)
expect(button).toHaveStyle(`color: ${COLORS.blue55}`)
})

it('renders secondary button with text and disabled', () => {
props.disabled = true
const { getByText } = render(props)
const button = getByText('secondary button')
render(props)
const button = screen.getByText('secondary button')
expect(button).toBeDisabled()
})

it('applies the correct states to the button - hover', () => {
const { getByText } = render(props)
const button = getByText('secondary button')
expect(button).toHaveStyleRule('box-shadow', '0 0 0', {
modifier: ':hover',
})
})

it('applies the correct states to the button - focus-visible', () => {
const { getByText } = render(props)
const button = getByText('secondary button')
expect(button).toHaveStyleRule(
'box-shadow',
`0 0 0 3px ${COLORS.yellow50}`,
{
modifier: ':focus-visible',
}
)
})

it('renders secondary button with text and different background color', () => {
props.color = COLORS.red50
const { getByText } = render(props)
const button = getByText('secondary button')
render(props)
const button = screen.getByText('secondary button')
expect(button).toHaveStyle(`color: ${COLORS.red50}`)
})
})
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('DeprecatedCheckboxField', () => {
it.todo('replace deprecated enzyme test')
})
2 changes: 2 additions & 0 deletions components/src/forms/__tests__/DropdownField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('DropdownField', () => {
it.todo('replace deprecated enzyme test')
})
2 changes: 2 additions & 0 deletions components/src/forms/__tests__/InputField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('InputField', () => {
it.todo('replace deprecated enzyme test')
})
2 changes: 2 additions & 0 deletions components/src/forms/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('Select', () => {
it.todo('replace deprecated enzyme test')
})
2 changes: 2 additions & 0 deletions components/src/forms/__tests__/SelectField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('SelectField', () => {
it.todo('replace deprecated enzyme test')
})
2 changes: 2 additions & 0 deletions components/src/forms/__tests__/ToggleField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, it } from 'vitest'

describe('ToggleField', () => {
it.todo('replace deprecated enzyme test')
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as React from 'react'
import { render } from '@testing-library/react'
import { resetAllWhenMocks, when } from 'jest-when'
import _uncasted_troughFixture12 from '@opentrons/shared-data/labware/fixtures/2/fixture_12_trough_v2.json'
import { describe, it, vi, beforeEach } from 'vitest'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { when } from 'vitest-when'
import { fixture12Trough } from '@opentrons/shared-data'
import { componentPropsMatcher } from '../../../testing/utils'
import {
StaticLabwareComponent as StaticLabware,
Expand All @@ -11,71 +13,61 @@ import {
import { LabwareRender, WELL_LABEL_OPTIONS } from '../LabwareRender'
import type { LabwareDefinition2 } from '@opentrons/shared-data'

jest.mock('../labwareInternals')
vi.mock('../labwareInternals')

const mockStaticLabware = StaticLabware as jest.MockedFunction<
typeof StaticLabware
>
const mockWellLabels = WellLabels as jest.MockedFunction<typeof WellLabels>
const mockStrokedWells = StrokedWells as jest.MockedFunction<
typeof StrokedWells
>

const troughFixture12 = _uncasted_troughFixture12 as LabwareDefinition2
const troughFixture12 = fixture12Trough as LabwareDefinition2

describe('LabwareRender', () => {
beforeEach(() => {
when(mockStaticLabware)
when(StaticLabware)
.calledWith(componentPropsMatcher({ definition: troughFixture12 }))
.mockReturnValue(<div>mock static labware</div>)
})
afterEach(() => {
resetAllWhenMocks()
.thenReturn(<div>mock static labware</div>)
})

it('should render a static labware component', () => {
const props = { definition: troughFixture12 }
const { getByText } = render(
render(
<svg>
<LabwareRender {...props} />
</svg>
)
getByText('mock static labware')
screen.getByText('mock static labware')
})
it('should render stroked wells', () => {
const props = { definition: troughFixture12, wellStroke: { A1: 'blue' } }
when(mockStrokedWells)
when(StrokedWells)
.calledWith(
componentPropsMatcher({
definition: troughFixture12,
strokeByWell: { A1: 'blue' },
})
)
.mockReturnValue(<div>mock stroked wells</div>)
const { getByText } = render(
.thenReturn(<div>mock stroked wells</div>)
render(
<svg>
<LabwareRender {...props} />
</svg>
)
getByText('mock stroked wells')
screen.getByText('mock stroked wells')
})
it('should render well labels', () => {
const props = {
definition: troughFixture12,
wellLabelOption: WELL_LABEL_OPTIONS.SHOW_LABEL_INSIDE,
}
when(mockWellLabels)
when(WellLabels)
.calledWith(
componentPropsMatcher({
definition: troughFixture12,
wellLabelOption: WELL_LABEL_OPTIONS.SHOW_LABEL_INSIDE,
})
)
.mockReturnValue(<div>mock well labels</div>)
const { getByText } = render(
.thenReturn(<div>mock well labels</div>)
render(
<svg>
<LabwareRender {...props} />
</svg>
)
getByText('mock well labels')
screen.getByText('mock well labels')
})
})
Loading

0 comments on commit 5acb14d

Please sign in to comment.