-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add coverage tests for UnitTaxonomyDrawer
- Loading branch information
1 parent
ae14d87
commit d2db1ad
Showing
12 changed files
with
614 additions
and
20 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// @ts-check | ||
|
||
/** | ||
/** | ||
* @typedef {Object} TaxonomyData | ||
* @property {number} id | ||
* @property {string} name | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// TagBubble.jsx | ||
|
||
import React from 'react'; | ||
import { | ||
Button, | ||
|
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,86 @@ | ||
import React from 'react'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { initializeMockApp } from '@edx/frontend-platform'; | ||
import { AppProvider } from '@edx/frontend-platform/react'; | ||
import { render } from '@testing-library/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import initializeStore from '../store'; | ||
|
||
import TagBubble from './TagBubble'; | ||
|
||
let store; | ||
|
||
const data = { | ||
value: 'Tag 1', | ||
}; | ||
|
||
const TagBubbleComponent = ({ value, subTagsCount, implicit }) => ( | ||
<AppProvider store={store}> | ||
<IntlProvider locale="en" messages={{}}> | ||
<TagBubble value={value} subTagsCount={subTagsCount} implicit={implicit} /> | ||
</IntlProvider> | ||
</AppProvider> | ||
); | ||
|
||
TagBubbleComponent.defaultProps = { | ||
subTagsCount: 0, | ||
implicit: true, | ||
}; | ||
|
||
TagBubbleComponent.propTypes = { | ||
value: PropTypes.string.isRequired, | ||
subTagsCount: PropTypes.number, | ||
implicit: PropTypes.bool, | ||
}; | ||
|
||
describe('<TagBubble />', async () => { | ||
beforeEach(async () => { | ||
initializeMockApp({ | ||
authenticatedUser: { | ||
userId: 3, | ||
username: 'abc123', | ||
administrator: true, | ||
roles: [], | ||
}, | ||
}); | ||
store = initializeStore(); | ||
}); | ||
|
||
it('should render only value of the implicit tag with no sub tags', () => { | ||
const { container, getByText } = render(<TagBubbleComponent value={data.value} />); | ||
expect(getByText(data.value)).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('implicit').length).toBe(1); | ||
}); | ||
|
||
it('should render value of the implicit tag with sub tags', () => { | ||
const tagBubbleData = { | ||
subTagsCount: 5, | ||
...data, | ||
}; | ||
const { container, getByText } = render( | ||
<TagBubbleComponent | ||
value={tagBubbleData.value} | ||
subTagsCount={tagBubbleData.subTagsCount} | ||
/>, | ||
); | ||
expect(getByText(`${tagBubbleData.value} (${tagBubbleData.subTagsCount})`)).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('implicit').length).toBe(1); | ||
}); | ||
|
||
it('should render value of the explicit tag with no sub tags', () => { | ||
const tagBubbleData = { | ||
implicit: false, | ||
...data, | ||
}; | ||
const { container, getByText } = render( | ||
<TagBubbleComponent | ||
value={tagBubbleData.value} | ||
implicit={tagBubbleData.implicit} | ||
/>, | ||
); | ||
expect(getByText(`${tagBubbleData.value}`)).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('implicit').length).toBe(0); | ||
expect(container.getElementsByClassName('btn-icon-after').length).toBe(1); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
src/unit-taxonomy-tags-drawer/TaxonomyTagsCollapsible.test.jsx
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,86 @@ | ||
import React from 'react'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { initializeMockApp } from '@edx/frontend-platform'; | ||
import { AppProvider } from '@edx/frontend-platform/react'; | ||
import { act, render } from '@testing-library/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import initializeStore from '../store'; | ||
|
||
import TaxonomyTagsCollapsible from './TaxonomyTagsCollapsible'; | ||
|
||
let store; | ||
|
||
jest.mock('./api/hooks/selectors', () => ({ | ||
useTaxonomyTagsDataResponse: jest.fn(), | ||
useIsTaxonomyTagsDataLoaded: jest.fn(), | ||
})); | ||
|
||
const data = { | ||
id: 123, | ||
name: 'Taxonomy 1', | ||
unitTags: [ | ||
{ | ||
name: 'Tag 1 Name', | ||
value: 'Tag 1', | ||
taxonomyId: 123, | ||
}, | ||
{ | ||
name: 'Tag 2 Name', | ||
value: 'Tag 2', | ||
taxonomyId: 123, | ||
}, | ||
], | ||
}; | ||
|
||
const TaxonomyTagsCollapsibleComponent = ({ taxonomyAndTagsData }) => ( | ||
<AppProvider store={store}> | ||
<IntlProvider locale="en" messages={{}}> | ||
<TaxonomyTagsCollapsible taxonomyAndTagsData={taxonomyAndTagsData} /> | ||
</IntlProvider> | ||
</AppProvider> | ||
); | ||
|
||
TaxonomyTagsCollapsibleComponent.propTypes = { | ||
taxonomyAndTagsData: PropTypes.shape({ | ||
id: PropTypes.number, | ||
name: PropTypes.string, | ||
unitTags: PropTypes.arrayOf(PropTypes.shape({ | ||
name: PropTypes.string, | ||
value: PropTypes.string, | ||
taxonomyId: PropTypes.number, | ||
})), | ||
}).isRequired, | ||
}; | ||
|
||
describe('<TaxonomyTagsCollapsible />', async () => { | ||
beforeEach(async () => { | ||
initializeMockApp({ | ||
authenticatedUser: { | ||
userId: 3, | ||
username: 'abc123', | ||
administrator: true, | ||
roles: [], | ||
}, | ||
}); | ||
store = initializeStore(); | ||
}); | ||
|
||
it('should render taxonomy tags data along unit tags number badge', async () => { | ||
await act(async () => { | ||
const { container, getByText } = render(<TaxonomyTagsCollapsibleComponent taxonomyAndTagsData={data} />); | ||
expect(getByText('Taxonomy 1')).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('tags-count-badge').length).toBe(1); | ||
expect(getByText('2')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should render taxonomy tags data without tags number badge', async () => { | ||
data.unitTags = []; | ||
await act(async () => { | ||
const { container, getByText } = render(<TaxonomyTagsCollapsibleComponent taxonomyAndTagsData={data} />); | ||
expect(getByText('Taxonomy 1')).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('tags-count-badge-empty').length).toBe(1); | ||
}); | ||
}); | ||
}); |
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
102 changes: 102 additions & 0 deletions
102
src/unit-taxonomy-tags-drawer/TaxonomyTagsDropDownSelector.test.jsx
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,102 @@ | ||
import React from 'react'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { initializeMockApp } from '@edx/frontend-platform'; | ||
import { AppProvider } from '@edx/frontend-platform/react'; | ||
import { act, render } from '@testing-library/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import initializeStore from '../store'; | ||
|
||
import TaxonomyTagsDropDownSelector from './TaxonomyTagsDropDownSelector'; | ||
import { useTaxonomyTagsDataResponse, useIsTaxonomyTagsDataLoaded } from './api/hooks/selectors'; | ||
|
||
jest.mock('./api/hooks/selectors', () => ({ | ||
useTaxonomyTagsDataResponse: jest.fn(), | ||
useIsTaxonomyTagsDataLoaded: jest.fn(), | ||
})); | ||
|
||
let store; | ||
|
||
const data = { | ||
taxonomyId: 123, | ||
taxonomyTag: { | ||
value: 'Tag 1', | ||
subTagsUrl: null, | ||
}, | ||
level: 0, | ||
useTaxonomyTagsData: (taxonomyId, fullPathProvided) => { | ||
const taxonomyTagsData = useTaxonomyTagsDataResponse(taxonomyId, fullPathProvided); | ||
const isTaxonomyTagsLoaded = useIsTaxonomyTagsDataLoaded(taxonomyId, fullPathProvided); | ||
return { taxonomyTagsData, isTaxonomyTagsLoaded }; | ||
}, | ||
}; | ||
|
||
const TaxonomyTagsDropDownSelectorComponent = ({ | ||
taxonomyId, taxonomyTag, level, useTaxonomyTagsData, | ||
}) => ( | ||
<AppProvider store={store}> | ||
<IntlProvider locale="en" messages={{}}> | ||
<TaxonomyTagsDropDownSelector | ||
taxonomyId={taxonomyId} | ||
taxonomyTag={taxonomyTag} | ||
level={level} | ||
useTaxonomyTagsData={useTaxonomyTagsData} | ||
/> | ||
</IntlProvider> | ||
</AppProvider> | ||
); | ||
|
||
TaxonomyTagsDropDownSelectorComponent.propTypes = { | ||
taxonomyId: PropTypes.number.isRequired, | ||
taxonomyTag: PropTypes.shape({ | ||
value: PropTypes.string, | ||
subTagsUrl: PropTypes.string, | ||
}).isRequired, | ||
level: PropTypes.number.isRequired, | ||
useTaxonomyTagsData: PropTypes.func.isRequired, | ||
}; | ||
|
||
describe('<TaxonomyTagsDropDownSelector />', async () => { | ||
beforeEach(async () => { | ||
initializeMockApp({ | ||
authenticatedUser: { | ||
userId: 3, | ||
username: 'abc123', | ||
administrator: true, | ||
roles: [], | ||
}, | ||
}); | ||
store = initializeStore(); | ||
}); | ||
|
||
it('should render taxonomy tags drop down selector with no sub tags', async () => { | ||
await act(async () => { | ||
const { container, getByText } = render( | ||
<TaxonomyTagsDropDownSelectorComponent | ||
taxonomyId={data.taxonomyId} | ||
taxonomyTag={data.taxonomyTag} | ||
level={data.level} | ||
useTaxonomyTagsData={data.useTaxonomyTagsData} | ||
/>, | ||
); | ||
expect(getByText('Tag 1')).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('taxonomy-tags-arrow-drop-down').length).toBe(0); | ||
}); | ||
}); | ||
|
||
it('should render taxonomy tags drop down selector with sub tags', async () => { | ||
data.taxonomyTag.subTagsUrl = 'https://example.com'; | ||
await act(async () => { | ||
const { container, getByText } = render( | ||
<TaxonomyTagsDropDownSelectorComponent | ||
taxonomyId={data.taxonomyId} | ||
taxonomyTag={data.taxonomyTag} | ||
level={data.level} | ||
useTaxonomyTagsData={data.useTaxonomyTagsData} | ||
/>, | ||
); | ||
expect(getByText('Tag 1')).toBeInTheDocument(); | ||
expect(container.getElementsByClassName('taxonomy-tags-arrow-drop-down').length).toBe(1); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.