Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
module.exports = require('@folio/stripes-acq-components/jest.config');
const path = require('path');
const config = require('@folio/jest-config-stripes');

module.exports = {
...config,
setupFiles: [
...config.setupFiles,
path.join(__dirname, './test/jest/setupFiles.js'),
],
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '@folio/stripes-acq-components/test/jest/__mock__';

import {
LOG_EVENT_OBJECTS,
} from '../constants';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';

import { render } from '@folio/jest-config-stripes/testing-library/react';

import '@folio/stripes-acq-components/test/jest/__mock__';

import {
useStripes,
} from '@folio/stripes/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
fireEvent,
} from '@folio/jest-config-stripes/testing-library/react';

import '@folio/stripes-acq-components/test/jest/__mock__';

import { CirculationLogEventItems } from './CirculationLogEventItems';

const getItemBarcode = () => Math.random().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import React from 'react';
import { render } from '@folio/jest-config-stripes/testing-library/react';
import user from '@folio/jest-config-stripes/testing-library/user-event';

import '@folio/stripes-acq-components/test/jest/__mock__';

import {
useCirculationLogExport,
} from './useCirculationLogExport';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { QueryClient, QueryClientProvider } from 'react-query';

import { renderHook } from '@folio/jest-config-stripes/testing-library/react';

import '@folio/stripes-acq-components/test/jest/__mock__';
import { useOkapiKy } from '@folio/stripes/core';

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';

import { render } from '@folio/jest-config-stripes/testing-library/react';

import '@folio/stripes-acq-components/test/jest/__mock__';

import { AcqDateRangeFilter } from '@folio/stripes-acq-components';

import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '@folio/stripes-acq-components/test/jest/__mock__';
import { markOldPatronInfoAsSuperseded } from './markOldPatronInfoAsSuperseded';
import { LOAN_ACTIONS } from './constants';

Expand Down
2 changes: 0 additions & 2 deletions src/CirculationLogList/utils.hollow.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '@folio/stripes-acq-components/test/jest/__mock__';

import {
makeQueryBuilder,
} from '@folio/stripes-acq-components';
Expand Down
2 changes: 0 additions & 2 deletions src/CirculationLogList/utils.integration.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '@folio/stripes-acq-components/test/jest/__mock__';

import { buildLogEventsQuery } from './utils';

describe('CirculationLogList utils', () => {
Expand Down
18 changes: 18 additions & 0 deletions test/jest/__mock__/currencyData.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
jest.mock('currency-codes/data', () => ({
filter: () => [
{
code: 'USD',
countries: [],
currency: 'US Dollar',
digits: 2,
number: '840',
},
{
code: 'BYN',
countries: [],
currency: 'Rubles',
digits: 2,
number: '841',
},
],
}));
5 changes: 5 additions & 0 deletions test/jest/__mock__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './currencyData.mock';
import './stripesCore.mock';
import './stripesConfig.mock';
import './reactIntl.mock';
import './matchMedia.mock';
12 changes: 12 additions & 0 deletions test/jest/__mock__/matchMedia.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
window.matchMedia = jest.fn().mockImplementation(query => {
return {
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
};
});
27 changes: 27 additions & 0 deletions test/jest/__mock__/reactIntl.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
jest.mock('react-intl', () => {
const intl = {
formatDate: (value) => (value ? value.substring(0, 10) : value),
formatDisplayName: (value) => value,
formatMessage: ({ id }) => id,
};

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;
}),
useIntl: () => intl,
injectIntl: (Component) => (props) => <Component {...props} intl={intl} />,
};
});
4 changes: 4 additions & 0 deletions test/jest/__mock__/stripesConfig.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jest.mock('stripes-config', () => ({
modules: [],
metadata: {},
}), { 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 @@
/* eslint-disable filenames/match-exported */

const buildStripes = (otherProperties = {}) => ({
actionNames: [],
clone: buildStripes,
connect: Comp => Comp,
config: {},
currency: 'USD',
hasInterface: jest.fn().mockReturnValue(true),
hasPerm: jest.fn().mockReturnValue(true),
locale: 'en-US',
logger: {
log: () => { },
},
okapi: {
tenant: 'diku',
url: 'https://folio-testing-okapi.dev.folio.org',
},
plugins: {},
setBindings: () => { },
setCurrency: () => { },
setLocale: () => { },
setSinglePlugin: () => { },
setTimezone: () => { },
setToken: () => { },
store: {
getState: () => { },
dispatch: () => { },
subscribe: () => { },
replaceReducer: () => { },
},
timezone: 'UTC',
user: {
perms: {},
user: {
id: 'b1add99d-530b-5912-94f3-4091b4d87e2c',
username: 'diku_admin',
consortium: {
centralTenantId: 'consortia',
},
},
},
withOkapi: true,
...otherProperties,
});

const STRIPES = buildStripes();

const mockStripesCore = {
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} resources={fakeResources} stripes={fakeStripes} />;
},

useOkapiKy: jest.fn().mockReturnValue({
get: jest.fn().mockReturnValue({ json: jest.fn().mockResolvedValue({}) }),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn(),
extend: jest.fn().mockReturnValue(this),
}),

useStripes: jest.fn(() => STRIPES),

withStripes: Component => ({ stripes, ...rest }) => {
const fakeStripes = stripes || STRIPES;

return <Component {...rest} stripes={fakeStripes} />;
},

// eslint-disable-next-line react/prop-types
Pluggable: props => <>{props.children}</>,

// eslint-disable-next-line react/prop-types
IfPermission: jest.fn(props => <>{props.children}</>),

withOkapiKy: jest.fn((Component) => (props) => <Component {...props} />),

// eslint-disable-next-line react/prop-types
IfInterface: jest.fn(props => <>{props.children}</>),

useNamespace: jest.fn(() => ['@folio/inventory']),

useCallout: jest.fn().mockReturnValue({
sendCallout: jest.fn(),
}),

TitleManager: ({ children }) => <>{children}</>,

checkIfUserInMemberTenant: jest.fn(() => true),

checkIfUserInCentralTenant: jest.fn(() => false),

useUserTenantPermissions: jest.fn().mockReturnValue({
userPermissions: [],
isFetched: true,
isFetching: false,
isLoading: false,
}),

updateTenant: jest.fn(() => {}),

validateUser: jest.fn(() => {}),

useSettings: jest.fn(() => ({ settings: {} })),
};

jest.mock('@folio/stripes/core', () => ({
...jest.requireActual('@folio/stripes/core'),
...mockStripesCore,
}), { virtual: true });


export default buildStripes;
2 changes: 2 additions & 0 deletions test/jest/setupFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://github.com/facebook/jest/issues/335#issuecomment-703691592
import './__mock__';