-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mespapiers): Extract helper and add tests
- Loading branch information
Showing
5 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
packages/cozy-mespapiers-lib/src/components/Papers/Empty/Empty.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/cozy-mespapiers-lib/src/components/Papers/Empty/helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` : '' | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/cozy-mespapiers-lib/src/components/Papers/Empty/helpers.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters