Skip to content

Commit

Permalink
UIFPU-78 - Create jest environment and setup initial mocks (#266)
Browse files Browse the repository at this point in the history
* UIFPU-78 - create jest environment and setup initial mocks

* enable mock import in setupFiles

* resolve review comments
  • Loading branch information
manvendra-s-rathore authored Jun 11, 2024
1 parent 1691cea commit 0dc6462
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 7.2.0 IN PROGRESS

* Create Jest test environment and setup initial mocks. Refs UIPFU-78.

## [7.1.1](https://github.com/folio-org/ui-plugin-find-user/tree/v7.1.1) (2024-05-03)
[Full Changelog](https://github.com/folio-org/ui-plugin-find-user/compare/v7.1.0...v7.1.1)

Expand Down
6 changes: 6 additions & 0 deletions test/jest/__mock__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './stripesConfig.mock';
import './stripesCore.mock';
import './stripes.mock';
import './intl.mock';
import './stripesComponents.mock';
import './stripesSmartComponents.mock';
36 changes: 36 additions & 0 deletions test/jest/__mock__/intl.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';

jest.mock('react-intl', () => {
const intl = {
formatMessage: ({ id }) => id,
formatNumber: (value) => value,
formatDisplayName: (value) => value,
};

return {
...jest.requireActual('react-intl'),
FormattedMessage: jest.fn(({ id, children }) => {
if (children) {
return children([id]);
}

return id;
}),
FormattedTime: jest.fn(({ value, children }) => {
if (children) {
return children([value]);
}

return value;
}),
FormattedDate: jest.fn(({ value, children }) => {
if (children) {
return children([value]);
}

return value;
}),
useIntl: () => intl,
injectIntl: (Component) => (props) => <Component {...props} intl={intl} />,
};
});
26 changes: 26 additions & 0 deletions test/jest/__mock__/stripes.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { noop } from 'lodash';

const buildStripes = (otherProperties = {}) => ({
hasPerm: noop,
hasInterface: noop,
clone: noop,
logger: { log: noop },
config: {},
okapi: {
url: '',
tenant: '',
},
locale: 'en-US',
withOkapi: true,
setToken: noop,
actionNames: [],
setLocale: noop,
setTimezone: noop,
setCurrency: noop,
setSinglePlugin: noop,
setBindings: noop,
connect: noop,
...otherProperties,
});

export default buildStripes;
68 changes: 68 additions & 0 deletions test/jest/__mock__/stripesComponents.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';

jest.mock('@folio/stripes/components', () => ({
Button: jest.fn(({
children,
onClick = jest.fn(),
...rest
}) => (
<button
data-test-button
type="button"
{...rest}
onClick={onClick}
>
<span>
{children}
</span>
</button>
)),
Checkbox: jest.fn((props) => (
<input
type="checkbox"
{...props}
/>
)),
FilterGroups: jest.fn(({ config, filters, onChangeFilter, onClearFilter }) => (
<div>
{JSON.stringify(config)}
{JSON.stringify(filters)}
{JSON.stringify(onChangeFilter)}
{JSON.stringify(onClearFilter)}
</div>
)),
Icon: jest.fn((props) => (props && props.children ? props.children : <span />)),
Modal: jest.fn(({ children, label, dismissible, footer, ...rest }) => {
return (
<div
data-test={dismissible ? '' : ''}
{...rest}
>
<h1>{label}</h1>
{children}
{footer}
</div>
);
}),
MultiColumnList: jest.fn((props) => (
<div data-testid={props['data-testid']} />
)),
Pane: jest.fn(({ children, className, defaultWidth, paneTitle, firstMenu, ...rest }) => {
return (
<div className={className} {...rest} style={{ width: defaultWidth }}>
<div>
{firstMenu ?? null}
{paneTitle}
</div>
{children}
</div>
);
}),
PaneMenu: jest.fn((props) => <div>{props.children}</div>),
Paneset: jest.fn((props) => <div>{props.children}</div>),
SearchField: jest.fn((props) => (
<input
{...props}
/>
)),
}));
1 change: 1 addition & 0 deletions test/jest/__mock__/stripesConfig.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.mock('stripes-config', () => ({ modules: [] }), { virtual: true });
139 changes: 139 additions & 0 deletions test/jest/__mock__/stripesCore.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import React from 'react';

jest.mock('@folio/stripes/core', () => {
const STRIPES = {
actionNames: [],
bindings: {},
clone: jest.fn(),
connect: (Component) => Component,
config: {
logCategories: '',
logPrefix: '',
logTimestamp: true,
showPerms: true,
showHomeLink: true,
listInvisiblePerms: true,
disableAuth: false,
hasAllPerms: false,
},
currency: 'USD',
discovery: {
interfaces: {},
isFinished: true,
modules: {},
okapi: '',
},
epics: {
add: jest.fn(),
middleware: jest.fn(),
},
hasInterface: () => true,
hasPerm: jest.fn().mockReturnValue(true),
icons: {},
locale: 'en-US',
logger: {
log: jest.fn(),
},
metadata: {},
okapi: {
authFailure: false,
okapiReady: true,
tenant: 'diku',
token: 'c0ffee',
translations: {
'stripes-components.Datepicker.calendar': 'calendar',
'stripes-components.Datepicker.calendarDaysList': 'calendar days list.',
'stripes-core.button.cancel': [{ type: 0, value: 'Cancel' }],
'ui-users.permission.modal.list.pane.header': 'Permissions',
'ui-users.permission.modal.list.pane.header.array': [{ type: 0, value: 'Permissions' }],
default: false,
},
url: 'https://folio-testing-okapi.dev.folio.org',
withoutOkapi: false,
},
plugins: {},
setBindings: jest.fn(),
setCurrency: jest.fn(),
setLocale: jest.fn(),
setSinglePlugin: jest.fn(),
setTimezone: jest.fn(),
setToken: jest.fn(),
store: {
getState: () => ({
okapi: {
token: 'abc',
},
}),
dispatch: jest.fn(),
subscribe: jest.fn(),
replaceReducer: jest.fn(),
},
timezone: 'UTC',
user: {
perms: {},
user: {
addresses: [],
firstName: 'Testy',
lastName: 'McTesterson',
email: '[email protected]',
id: 'b1add99d-530b-5912-94f3-4091b4d87e2c',
username: 'diku_admin',
},
},
withOkapi: true,
};

// eslint-disable-next-line react/prop-types
const stripesConnect = Component => ({ mutator, resources, stripes, ...rest }) => {
const fakeMutator = mutator || Object.keys(Component.manifest || {}).reduce((acc, mutatorName) => {
const returnValue = Component.manifest[mutatorName].records ? [] : {};

acc[mutatorName] = {
GET: jest.fn().mockReturnValue(Promise.resolve(returnValue)),
PUT: jest.fn().mockReturnValue(Promise.resolve()),
POST: jest.fn().mockReturnValue(Promise.resolve()),
DELETE: jest.fn().mockReturnValue(Promise.resolve()),
reset: jest.fn(),
update: jest.fn(),
replace: jest.fn(),
};

return acc;
}, {});

const fakeResources = resources || Object.keys(Component.manifest || {}).reduce((acc, resourceName) => {
acc[resourceName] = {
records: [],
};

return acc;
}, {});

const fakeStripes = stripes || STRIPES;

return (<Component
{...rest}
mutator={fakeMutator}
refreshRemote={jest.fn()}
resources={fakeResources}
stripes={fakeStripes}
/>);
};

return {
...jest.requireActual('@folio/stripes/core'),
AppIcon: jest.fn(({ ariaLabel }) => <span>{ariaLabel}</span>),
connect: stripesConnect,
IntlConsumer: jest.fn(({ children }) => {
const intl = {
formatMessage: jest.fn(({ id }) => id),
};
return (
<div>
{children(intl)}
</div>
);
}),
stripesConnect,
};
});
51 changes: 51 additions & 0 deletions test/jest/__mock__/stripesSmartComponents.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';

jest.mock('@folio/stripes/smart-components', () => ({
makeQueryFunction: jest.fn((value) => value),
SearchAndSortQuery: jest.fn(({ children, ...rest }) => <div {...rest}>{children}</div>),
SearchAndSortSearchButton: jest.fn(({
label,
id,
onClick = jest.fn(),
disabled,
visible,
...restProps
}) => (
<div>
<button
type="button"
buttonStyle="none"
id={id}
onClick={onClick}
disabled={disabled}
visible={visible}
{...restProps}
>
<span size="small" icon="times-circle-solid">
{label}
</span>
</button>
</div>
)),
SearchAndSortNoResultsMessage: jest.fn(({
label,
filterPaneIsVisible = true,
toggleFilterPane = jest.fn(),
...rest
}) => (
<div {...rest}>
<div>
<span>{label}</span>
</div>
{!filterPaneIsVisible &&
<button
type="submit"
onClick={toggleFilterPane}
>
Show filters
</button>
}
</div>
)),
StripesConnectedSource: jest.fn(),
}));
2 changes: 1 addition & 1 deletion test/jest/setupFiles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// See https://github.com/facebook/jest/issues/335#issuecomment-703691592
// import './__mock__';
import './__mock__';

import 'regenerator-runtime/runtime';

0 comments on commit 0dc6462

Please sign in to comment.