Skip to content

Commit

Permalink
test: improve coverage + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeissonnier-pass committed Nov 25, 2024
1 parent fa6566e commit c206d08
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react'

import { FILTERS_VENUE_TYPE_MAPPING } from 'features/venueMap/constant'
import * as useFeatureFlagAPI from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { render, screen } from 'tests/utils'

import { FilterBannerContainer } from './FilterBannerContainer'

const useFeatureFlagSpy = jest.spyOn(useFeatureFlagAPI, 'useFeatureFlag')

describe('FilterBannerConainer', () => {
beforeEach(() => {
useFeatureFlagSpy.mockReturnValue(true)
})

it('should render FilterCategoriesBannerContainer if FF is enabled', async () => {
render(<FilterBannerContainer />)
await screen.findAllByTestId(/[A-Z]+Label/)

Object.keys(FILTERS_VENUE_TYPE_MAPPING).forEach((id) => {
expect(screen.getByTestId(`${id}Label`)).toBeOnTheScreen()
})
})

it('should render SingleFilterBannerContainer if FF is disabled', async () => {
useFeatureFlagSpy.mockReturnValueOnce(false)

render(<FilterBannerContainer />)

expect(await screen.findByLabelText('Tous les lieux')).toBeOnTheScreen()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react'

import { FILTERS_VENUE_TYPE_MAPPING } from 'features/venueMap/constant'
import { render, screen, userEvent } from 'tests/utils'

import { FilterCategoriesBannerContainer } from './FilterCategoriesBannerContainer'

describe('FilterCategoriesBannerContainer', () => {
const user = userEvent.setup()

it('should render correctly', async () => {
render(<FilterCategoriesBannerContainer />)

await screen.findAllByTestId(/[A-Z]+Label/)

Object.keys(FILTERS_VENUE_TYPE_MAPPING).forEach((id) => {
expect(screen.getByTestId(`${id}Label`)).toBeOnTheScreen()
})
})

it.each([
{ id: 'OUTINGS', label: 'Sorties : Filtre sélectionné' },
{ id: 'SHOPS', label: 'Boutiques : Filtre sélectionné' },
{ id: 'OTHERS', label: 'Autres : Filtre sélectionné' },
])(`should select $id group`, async ({ id, label }) => {
jest.useFakeTimers()
render(<FilterCategoriesBannerContainer />)

await user.press(screen.getByTestId(`${id}Label`))

expect(await screen.findByLabelText(label)).toBeOnTheScreen()

jest.useRealTimers()
})

it('should deselect selected group', async () => {
jest.useFakeTimers()
render(<FilterCategoriesBannerContainer />)

await user.press(screen.getByTestId('OUTINGSLabel'))

await screen.findByLabelText('Sorties : Filtre sélectionné')

await user.press(screen.getByTestId('OUTINGSLabel'))

expect(screen.queryByLabelText('Sorties : Filtre sélectionné')).not.toBeOnTheScreen()

jest.useRealTimers()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'

import { VenueTypeCodeKey } from 'api/gen'
import {
useVenueTypeCode,
useVenueTypeCodeActions,
} from 'features/venueMap/store/venueTypeCodeStore'
import { parseType } from 'libs/parsers/venueType'
import { ellipseString } from 'shared/string/ellipseString'
import { render, screen } from 'tests/utils'

import { SingleFilterBannerContainer } from './SingleFilterBannerContainer'

jest.mock('features/venueMap/store/venueTypeCodeStore')
const mockSetVenueTypeCode = jest.fn()
const mockUseVenueTypeCode = useVenueTypeCode as jest.Mock
const mockUseVenueTypeCodeActions = useVenueTypeCodeActions as jest.Mock

mockUseVenueTypeCodeActions.mockReturnValue({ setVenueTypeCode: mockSetVenueTypeCode })

const VENUE_TYPE = VenueTypeCodeKey.MOVIE

describe('SingleFilterBannerContainer', () => {
beforeEach(() => {
mockUseVenueTypeCode.mockReturnValue(VENUE_TYPE)
})

it('should render correctly', async () => {
render(<SingleFilterBannerContainer />)

expect(screen.getByText(ellipseString(parseType(VENUE_TYPE), 20))).toBeOnTheScreen()
})

it('Should filter with no venueCode', async () => {
mockUseVenueTypeCode.mockReturnValueOnce(null)

render(<SingleFilterBannerContainer />)

expect(screen.getByText('Tous les lieux')).toBeOnTheScreen()
})
})
87 changes: 52 additions & 35 deletions src/features/venueMap/hook/useVenueMapFilters.native.test.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,88 @@
import { VenueTypeCodeKey } from 'api/gen'
import { FILTERS_VENUE_TYPE_MAPPING } from 'features/venueMap/constant'
import { useVenuesFilter, useVenuesFilterActions } from 'features/venueMap/store/venuesFilterStore'
import { renderHook } from 'tests/utils'
import { act, renderHook } from 'tests/utils'

import { useVenueMapFilters } from './useVenueMapFilters'

jest.mock('features/venueMap/store/venuesFilterStore')

const mockUseVenuesFilter = useVenuesFilter as jest.Mock
const mockUseVenuesFilterActions = useVenuesFilterActions as jest.Mock
const mockSetVenuesFilters = jest.fn()
const mockAddVenuesFilters = jest.fn()
const mockRemoveVenuesFilters = jest.fn()

describe('useVenueMapFilters', () => {
beforeEach(() => {
mockUseVenuesFilter.mockReturnValue([])
mockUseVenuesFilterActions.mockReturnValue({
setVenuesFilters: mockSetVenuesFilters,
addVenuesFilters: mockAddVenuesFilters,
removeVenuesFilters: mockRemoveVenuesFilters,
})
})

it('should initialize with no active filters', () => {
const { result } = renderHook(() => useVenueMapFilters())

expect(result.current.activeFilters).toEqual([])
})

it('should apply macro filters correctly', () => {
it('should add macro filters correctly', () => {
const { result } = renderHook(() => useVenueMapFilters())

result.current.applyMacroFilters('OUTINGS')
act(() => {
result.current.addMacroFilter('OUTINGS')
})

act(() => {
result.current.addMacroFilter('SHOPS')
})

expect(mockSetVenuesFilters).toHaveBeenCalledWith(FILTERS_VENUE_TYPE_MAPPING.OUTINGS)
expect(result.current.activeFilters).toMatchObject([
...FILTERS_VENUE_TYPE_MAPPING.OUTINGS,
...FILTERS_VENUE_TYPE_MAPPING.SHOPS,
])
})

it('should toggle a filter correctly (add filter)', () => {
mockUseVenuesFilter.mockReturnValueOnce([])
it('should remove macro filters correctly', () => {
const { result } = renderHook(() => useVenueMapFilters())

act(() => {
result.current.addMacroFilter('OUTINGS')
})

act(() => {
result.current.addMacroFilter('SHOPS')
})

act(() => {
result.current.removeMacroFilter('SHOPS')
})

expect(result.current.activeFilters).toMatchObject(FILTERS_VENUE_TYPE_MAPPING.OUTINGS)
})

it('should toggle a filter correctly (add filter)', () => {
const { result } = renderHook(() => useVenueMapFilters())
const filterToAdd = VenueTypeCodeKey.CONCERT_HALL

result.current.toggleFilter(filterToAdd)
act(() => {
result.current.toggleFilter(filterToAdd)
})

expect(mockAddVenuesFilters).toHaveBeenCalledWith([filterToAdd])
expect(result.current.activeFilters).toMatchObject([filterToAdd])
})

it('should toggle a filter correctly (remove filter)', () => {
mockUseVenuesFilter.mockReturnValueOnce([VenueTypeCodeKey.CONCERT_HALL])

const { result } = renderHook(() => useVenueMapFilters())
const filterToRemove = VenueTypeCodeKey.CONCERT_HALL

result.current.toggleFilter(filterToRemove)
act(() => {
result.current.toggleFilter(filterToRemove)
})

act(() => {
result.current.toggleFilter(filterToRemove)
})

expect(mockRemoveVenuesFilters).toHaveBeenCalledWith([filterToRemove])
expect(result.current.activeFilters).toHaveLength(0)
})

it('should return selected macros correctly', () => {
mockUseVenuesFilter.mockReturnValueOnce([
VenueTypeCodeKey.CONCERT_HALL,
VenueTypeCodeKey.BOOKSTORE,
])

const { result } = renderHook(() => useVenueMapFilters())

act(() => {
result.current.toggleFilter(VenueTypeCodeKey.CONCERT_HALL)
})

act(() => {
result.current.toggleFilter(VenueTypeCodeKey.BOOKSTORE)
})

const selectedMacros = result.current.getSelectedMacroFilters()

expect(selectedMacros).toEqual(['OUTINGS', 'SHOPS'])
Expand Down
24 changes: 7 additions & 17 deletions src/features/venueMap/pages/VenueMap/VenueMap.native.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import React from 'react'

import { VenueTypeCodeKey } from 'api/gen'
import * as useGoBack from 'features/navigation/useGoBack'
import { FILTERS_VENUE_TYPE_MAPPING } from 'features/venueMap/constant'
import { VenueMap } from 'features/venueMap/pages/VenueMap/VenueMap'
import {
useVenueTypeCodeActions,
useVenueTypeCode,
} from 'features/venueMap/store/venueTypeCodeStore'
import * as useFeatureFlag from 'libs/firebase/firestore/featureFlags/useFeatureFlag'
import { parseType } from 'libs/parsers/venueType'
import { ellipseString } from 'shared/string/ellipseString'
import { reactQueryProviderHOC } from 'tests/reactQueryProviderHOC'
import { fireEvent, render, screen, waitFor } from 'tests/utils'
import { fireEvent, render, screen } from 'tests/utils'

jest.mock('features/venue/api/useVenueOffers')
jest.spyOn(useFeatureFlag, 'useFeatureFlag').mockReturnValue(true)
Expand Down Expand Up @@ -60,22 +59,13 @@ describe('<VenueMap />', () => {
it('Should display venue map header', async () => {
render(reactQueryProviderHOC(<VenueMap />))

await waitFor(() => {
expect(screen.getByText('Carte des lieux')).toBeOnTheScreen()
expect(screen.getByText(ellipseString(parseType(VENUE_TYPE), 20))).toBeOnTheScreen()
})
})

it('Should display venue map header with no venueCode', async () => {
mockUseVenueTypeCode.mockReturnValueOnce(null)
mockUseVenueTypeCode.mockReturnValueOnce(null)
await screen.findAllByTestId(/[A-Z]+Label/)

render(reactQueryProviderHOC(<VenueMap />))

await waitFor(() => {
expect(screen.getByText('Carte des lieux')).toBeOnTheScreen()
expect(screen.getByText('Tous les lieux')).toBeOnTheScreen()
Object.keys(FILTERS_VENUE_TYPE_MAPPING).forEach((id) => {
expect(screen.getByTestId(`${id}Label`)).toBeOnTheScreen()
})

expect(screen.getByText('Carte des lieux')).toBeOnTheScreen()
})

it('Should handle go back action when pressing go back button', async () => {
Expand Down

0 comments on commit c206d08

Please sign in to comment.