diff --git a/package.json b/package.json index 5c974a660..ce3fd074c 100644 --- a/package.json +++ b/package.json @@ -51,13 +51,16 @@ "devDependencies": { "@dhis2/cli-app-scripts": "^5", "@dhis2/cli-style": "^7.0.0", + "@testing-library/jest-dom": "^5.11.4", + "@testing-library/react": "^11.1.0", "d2-manifest": "^1.0.0", "enzyme": "^3.11.0", "enzyme-adapter-react-16": "^1.15.2", "enzyme-to-json": "^3.5.0", "immutability-helper": "^3.1.1", "patch-package": "^6.2.2", - "postinstall-postinstall": "^2.1.0" + "postinstall-postinstall": "^2.1.0", + "redux-mock-store": "^1.5.4" }, "jest": { "setupFilesAfterEnv": [ diff --git a/src/components/Item/AppItem/Item.js b/src/components/Item/AppItem/Item.js index 6a83be901..a23200c9b 100644 --- a/src/components/Item/AppItem/Item.js +++ b/src/components/Item/AppItem/Item.js @@ -3,26 +3,23 @@ import PropTypes from 'prop-types' import { connect } from 'react-redux' import NotInterestedIcon from '@material-ui/icons/NotInterested' +import ItemHeader from '../ItemHeader/ItemHeader' +import Line from '../../../widgets/Line' + import { FILTER_ORG_UNIT } from '../../../actions/itemFilters' import { sGetItemFiltersRoot, DEFAULT_STATE_ITEM_FILTERS, } from '../../../reducers/itemFilters' -import ItemHeader from '../ItemHeader/ItemHeader' -import Line from '../../../widgets/Line' import { isEditMode } from '../../Dashboard/dashboardModes' const getIframeSrc = (appDetails, item, itemFilters) => { let iframeSrc = `${appDetails.launchUrl}?dashboardItemId=${item.id}` - if ( - itemFilters && - itemFilters[FILTER_ORG_UNIT] && - itemFilters[FILTER_ORG_UNIT].length - ) { + if (itemFilters[FILTER_ORG_UNIT] && itemFilters[FILTER_ORG_UNIT].length) { const ouIds = itemFilters[FILTER_ORG_UNIT].map( - ouPath => ouPath.split('/').slice(-1)[0] + ouFilter => ouFilter.path.split('/').slice(-1)[0] ) iframeSrc += `&userOrgUnit=${ouIds.join(',')}` diff --git a/src/components/Item/AppItem/__tests__/Item.spec.js b/src/components/Item/AppItem/__tests__/Item.spec.js new file mode 100644 index 000000000..a4ce4ec5c --- /dev/null +++ b/src/components/Item/AppItem/__tests__/Item.spec.js @@ -0,0 +1,117 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { render } from '@testing-library/react' +import { Provider } from 'react-redux' +import configureMockStore from 'redux-mock-store' + +import Item from '../Item' + +const mockStore = configureMockStore() + +const item = { + appKey: 'scorecard', + id: 'rainbowdash', + shortened: false, +} + +test('renders a valid App item in view mode', () => { + const store = { + itemFilters: {}, + } + const { container } = render( + + + + + + ) + expect(container).toMatchSnapshot() +}) + +test('renders a valid App item with filter in view mode', () => { + const store = { + itemFilters: { + ou: [{ path: '/rainbow' }], + }, + } + + const { container } = render( + + + + + + ) + expect(container).toMatchSnapshot() +}) + +test('renders a valid App item with filter in edit mode', () => { + const store = { + itemFilters: { + ou: [{ path: '/rainbow' }], + }, + } + + const { container } = render( + + + + + + ) + expect(container).toMatchSnapshot() +}) + +test('renders an invalid App item', () => { + const store = { + itemFilters: { + ou: [{ path: '/rainbow' }], + }, + } + + const invalidItem = { + appKey: 'unknownApp', + id: 'unknown', + shortened: false, + } + + const { container } = render( + + + + + + ) + expect(container).toMatchSnapshot() +}) + +// Mock context provider +class D2Provider extends React.Component { + getChildContext() { + return { + d2: { + system: { + installedApps: [ + { + key: 'scorecard', + name: 'Scorecard', + launchUrl: 'launchurl', + }, + ], + }, + }, + } + } + + render() { + return this.props.children + } +} + +D2Provider.childContextTypes = { + d2: PropTypes.object, +} + +D2Provider.propTypes = { + children: PropTypes.node, +} diff --git a/src/components/Item/AppItem/__tests__/__snapshots__/Item.spec.js.snap b/src/components/Item/AppItem/__tests__/__snapshots__/Item.spec.js.snap new file mode 100644 index 000000000..f37589f7c --- /dev/null +++ b/src/components/Item/AppItem/__tests__/__snapshots__/Item.spec.js.snap @@ -0,0 +1,124 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders a valid App item in view mode 1`] = ` +
+
+

+ Scorecard +

+
+
+