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

[Tests] Icon - Enhance Unit Tests #362

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Changes from all 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
204 changes: 144 additions & 60 deletions packages/components/src/components/Icon/Icon.test.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,163 @@
import { Icon } from './Icon'
import { Icon, IconProps } from './Icon'
import { render, screen } from '@testing-library/react-native'
import React from 'react'

import CustomSVG from '../../assets/svgs/custom.svg'

const mockedColorScheme = jest.fn()

jest.mock('react-native/Libraries/Utilities/useColorScheme', () => {
return {
default: mockedColorScheme,
}
})

describe('Icon', () => {
const commonProps: IconProps = {
name: 'HomeOutlined',
testID: 'myId',
}

const getIcon = () => screen.getByTestId('myId')

it('renders correctly at default size', async () => {
render(
<Icon
name="HomeOutlined"
testID="myId"
fill="#565c65"
preventScaling={true}
/>,
)
afterEach(() => {
mockedColorScheme.mockReset()
})

const icon = getIcon()
expect(icon).toBeOnTheScreen()
const { width, height, fill } = icon.props
describe('Basic tests and scaling', () => {
it('renders correctly at default size', async () => {
render(<Icon {...commonProps} preventScaling={true} />)

expect(width).toBe(24)
expect(height).toBe(24)
expect(fill).toBe('#565c65')
})
const icon = getIcon()
const { width, height, fill } = icon.props

it('renders correctly at set size despite fontScale 2', async () => {
render(
<Icon
name="HomeOutlined"
testID="myId"
height={50}
width={50}
preventScaling={true}
/>,
)
expect(icon).toBeOnTheScreen()
expect(width).toBe(24)
expect(height).toBe(24)
expect(fill).toBe('#005ea2')
})

const icon = getIcon()
expect(icon).toBeOnTheScreen()
const { width, height } = icon.props
it('renders correctly at set size despite fontScale 2', async () => {
render(
<Icon {...commonProps} height={50} width={50} preventScaling={true} />,
)

expect(width).toBe(50)
expect(height).toBe(50)
})
const icon = getIcon()
const { width, height } = icon.props

it('renders correctly with maxWidth overriding fontScale 2', async () => {
render(
<Icon
name="HomeOutlined"
testID="myId"
height={50}
width={50}
maxWidth={75}
/>,
)
expect(icon).toBeOnTheScreen()
expect(width).toBe(50)
expect(height).toBe(50)
})

const icon = getIcon()
expect(icon).toBeOnTheScreen()
const { width, height } = icon.props
it('renders correctly with maxWidth overriding fontScale 2', async () => {
render(<Icon {...commonProps} height={50} width={50} maxWidth={75} />)

const icon = getIcon()
const { width, height } = icon.props

expect(icon).toBeOnTheScreen()
expect(width).toBe(75)
expect(height).toBe(75)
})

it('renders correctly with fontScale 2', async () => {
render(<Icon {...commonProps} height={50} width={50} />)

const icon = getIcon()
const { width, height } = icon.props

expect(width).toBe(75)
expect(height).toBe(75)
expect(icon).toBeOnTheScreen()
expect(width).toBe(100)
expect(height).toBe(100)
})
})

it('renders correctly with fontScale 2', async () => {
render(<Icon name="HomeOutlined" testID="myId" height={50} width={50} />)
describe('Color tests', () => {
describe('Single string color', () => {
it('renders correctly in light mode', async () => {
render(<Icon {...commonProps} fill="#565c65" />)

const icon = getIcon()
expect(icon).toBeOnTheScreen()
const { width, height } = icon.props
const icon = getIcon()

expect(width).toBe(100)
expect(height).toBe(100)
expect(icon.props.fill).toBe('#565c65')
})

it('renders correctly in dark mode', async () => {
mockedColorScheme.mockImplementation(() => 'dark')

render(<Icon {...commonProps} fill="#565c65" />)

const icon = getIcon()

expect(icon.props.fill).toBe('#565c65')
})
})

describe('"default" colors', () => {
it('renders correctly in light mode', async () => {
render(<Icon {...commonProps} />)

const icon = getIcon()

expect(icon.props.fill).toBe('#005ea2')
})

it('renders correctly in dark mode', async () => {
mockedColorScheme.mockImplementation(() => 'dark')

render(<Icon {...commonProps} />)

const icon = getIcon()

expect(icon.props.fill).toBe('#58b4ff')
})
})

describe('"base" colors', () => {
it('renders correctly in light mode', async () => {
render(<Icon {...commonProps} fill="base" />)

const icon = getIcon()

expect(icon.props.fill).toBe('#1b1b1b')
})

it('renders correctly in dark mode', async () => {
mockedColorScheme.mockImplementation(() => 'dark')

render(<Icon {...commonProps} fill="base" />)

const icon = getIcon()

expect(icon.props.fill).toBe('#f0f0f0')
})
})

describe('Custom light/dark mode colors', () => {
const lightDarkModeFill: IconProps['fill'] = {
light: '#f0f',
dark: '#0f0',
}

it('renders correctly in light mode', async () => {
render(<Icon {...commonProps} fill={lightDarkModeFill} />)

const icon = getIcon()

expect(icon.props.fill).toBe('#f0f')
})

it('renders correctly in dark mode', async () => {
mockedColorScheme.mockImplementation(() => 'dark')

render(<Icon {...commonProps} fill={lightDarkModeFill} />)

const icon = getIcon()

expect(icon.props.fill).toBe('#0f0')
})
})
})

it('renders custom SVG correctly', async () => {
Expand All @@ -82,18 +167,17 @@ describe('Icon', () => {
testID="myId"
height={100}
width={100}
fill="#565c65"
maxWidth={100}
preventScaling={true}
/>,
)

const icon = getIcon()
expect(icon).toBeOnTheScreen()
const { width, height, fill } = icon.props
const { name, width, height, fill } = icon.props

expect(icon).toBeOnTheScreen()
expect(name).not.toBeDefined()
expect(width).toBe(100)
expect(height).toBe(100)
expect(fill).toBe('#565c65')
expect(fill).toBe('#005ea2')
})
})
Loading