From bb49c800f1e0645847f41fe49739807c7608bc5c Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 5 Dec 2024 16:25:01 -0500 Subject: [PATCH] test IfInterface (#1574) --- src/components/IfInterface/IfInterface.js | 20 ++++++----- .../IfInterface/IfInterface.test.js | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/components/IfInterface/IfInterface.test.js 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(); + }); +});