Skip to content

Commit

Permalink
test IfInterface (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke authored Dec 5, 2024
1 parent a99341c commit bb49c80
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/components/IfInterface/IfInterface.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<StripesContext.Consumer>
{stripes => (
stripes.hasInterface(name, version) ? children : null
)}
</StripesContext.Consumer>
);
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,
Expand Down
33 changes: 33 additions & 0 deletions src/components/IfInterface/IfInterface.test.js
Original file line number Diff line number Diff line change
@@ -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(<IfInterface name="foo">monkey</IfInterface>);
expect(screen.queryByText(/monkey/)).toBeTruthy();
});

it('returns false if interface is absent', () => {
useStripes.mockReturnValue(stripes);
render(<IfInterface name="paul,is,dead">monkey</IfInterface>);
expect(screen.queryByText(/monkey/)).toBeFalsy();
});
});

0 comments on commit bb49c80

Please sign in to comment.