diff --git a/src/components/IfInterface/IfInterface.js b/src/components/IfInterface/IfInterface.js index 78edb9435..cd7f9acdc 100644 --- a/src/components/IfInterface/IfInterface.js +++ b/src/components/IfInterface/IfInterface.js @@ -1,14 +1,16 @@ -import React from 'react'; import PropTypes from 'prop-types'; -import { StripesContext } from '../../StripesContext'; +import { useStripes } from '../../StripesContext'; -const IfInterface = ({ children, name, version }) => ( - - {stripes => ( - stripes.hasInterface(name, version) ? children : null - )} - -); +const IfInterface = ({ children, name, version }) => { + const stripes = useStripes(); + const hasInterface = stripes.hasInterface(name, version); + + if (typeof children === 'function') { + return children({ hasInterface }); + } + + return hasInterface ? children : null; +}; IfInterface.propTypes = { children: PropTypes.node, diff --git a/src/components/IfInterface/IfInterface.test.js b/src/components/IfInterface/IfInterface.test.js new file mode 100644 index 000000000..430df9222 --- /dev/null +++ b/src/components/IfInterface/IfInterface.test.js @@ -0,0 +1,33 @@ +import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; + +import { useStripes } from '../../StripesContext'; +import Stripes from '../../Stripes'; +import IfInterface from './IfInterface'; + +jest.mock('../../StripesContext'); +const stripes = new Stripes({ + discovery: { + interfaces: { + foo: '1.0' + } + }, + logger: { + log: jest.fn(), + } +}); + +// IfInterface is just a component version of Stripes::hasInterface +// See more extensive tests there. +describe('IfInterface', () => { + it('returns true if interface is present', () => { + useStripes.mockReturnValue(stripes); + render(monkey); + expect(screen.queryByText(/monkey/)).toBeTruthy(); + }); + + it('returns false if interface is absent', () => { + useStripes.mockReturnValue(stripes); + render(monkey); + expect(screen.queryByText(/monkey/)).toBeFalsy(); + }); +});