Skip to content

Commit

Permalink
refactor(mespapiers): Extract helper and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Feb 23, 2023
1 parent 9cb5b0f commit d291d0c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React from 'react'
import { render } from '@testing-library/react'
import flag from 'cozy-flags'

import AppLike from '../../../../test/components/AppLike'
import Empty from './Empty'

jest.mock('cozy-flags')
jest.mock('../HarvestBanner', () => () => <div data-testid="HarvestBanner" />)

const setup = ({ connector, accounts } = {}) => {
return render(
<AppLike>
<Empty connector={connector} accounts={accounts} />
</AppLike>
)
}

describe('MesPapiersLibProviders', () => {
beforeEach(() => {
jest.resetAllMocks()
})

it('should display basic text without harvest banner', () => {
const { queryByTestId, getByText } = setup({
connector: undefined,
accounts: undefined
})

expect(queryByTestId('HarvestBanner')).toBeFalsy()
expect(getByText('Add your personal documents'))
})

it('should display specific text', () => {
const { queryByTestId, getByText } = setup({
connector: {},
accounts: [{}]
})

expect(queryByTestId('HarvestBanner')).toBeFalsy()
expect(getByText('Add manually'))
})

it('should display specific text and harvest banner', () => {
flag.mockReturnValue(true)

const { queryByTestId, getByText } = setup({
connector: {},
accounts: [{}]
})

expect(queryByTestId('HarvestBanner')).toBeTruthy()
expect(getByText('Add manually'))
})

it('should display logins', () => {
const { queryByTestId, getByText } = setup({
connector: {},
accounts: [
{ auth: { login: 'myLogin' } },
{ auth: { login: 'myOtherLogin' } }
]
})

expect(queryByTestId('HarvestBanner')).toBeFalsy()
expect(getByText('myLogin'))
expect(getByText('myOtherLogin'))
})

it('should display logins and harvest banner', () => {
flag.mockReturnValue(true)

const { queryAllByTestId, getByText } = setup({
connector: {},
accounts: [
{ auth: { login: 'myLogin' } },
{ auth: { login: 'myOtherLogin' } }
]
})

expect(queryAllByTestId('HarvestBanner')).toBeTruthy()
expect(getByText('myLogin'))
expect(getByText('myOtherLogin'))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,13 @@ import Empty from 'cozy-ui/transpiled/react/Empty'
import Button from 'cozy-ui/transpiled/react/Buttons'
import PapersIcon from 'cozy-ui/transpiled/react/Icons/Papers'

import { findPlaceholderByLabelAndCountry } from '../../../helpers/findPlaceholders'
import { useMultiSelection } from '../../Hooks/useMultiSelection'
import { usePapersDefinitions } from '../../Hooks/usePapersDefinitions'
import { makeCountrySearchParam } from './helpers'
import { getCurrentFileTheme } from '../helpers'
import HarvestBanner from '../HarvestBanner'
import styles from './styles.styl'

const makeCountrySearchParam = ({ papersDefinitions, params, search }) => {
const country = new URLSearchParams(search).get('country')
const paperDefinition = findPlaceholderByLabelAndCountry(
papersDefinitions,
params.fileTheme,
country
)[0]

return paperDefinition.country ? `country=${paperDefinition.country}` : ''
}

const EmptyNoHeader = ({ connector, accounts }) => {
const { t } = useI18n()
const params = useParams()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { findPlaceholderByLabelAndCountry } from '../../../helpers/findPlaceholders'

export const makeCountrySearchParam = ({
papersDefinitions,
params,
search
}) => {
const country = new URLSearchParams(search).get('country')
const placeholders = findPlaceholderByLabelAndCountry(
papersDefinitions,
params.fileTheme,
country
)
const placeholder = placeholders?.[0]

return placeholder?.country ? `country=${placeholder.country}` : ''
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { makeCountrySearchParam } from './helpers'

// no need to test all cases here
// already tested on findPlaceholderByLabelAndCountry function

describe('makeCountrySearchParam', () => {
it('should return empty string if no match between paperDefinitions and params', () => {
const res = makeCountrySearchParam({
papersDefinitions: [{ label: 'caf' }],
params: { fileTheme: 'isp_invoice' },
search: ''
})

expect(res).toBe('')
})

it('should return empty string if no match between paperDefinitions and params even with a search param', () => {
const res = makeCountrySearchParam({
papersDefinitions: [{ label: 'caf' }],
params: { fileTheme: 'isp_invoice' },
search: '?country=fr'
})

expect(res).toBe('')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PapersListByContact from './PapersListByContact'
import { buildFilesByContacts } from '../Papers/helpers'

jest.mock('../Papers/helpers', () => ({
...jest.requireActual('../Papers/helpers'),
getCurrentFileTheme: jest.fn(),
buildFilesByContacts: jest.fn()
}))
Expand Down

0 comments on commit d291d0c

Please sign in to comment.