Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Melnyshyn committed Dec 15, 2023
1 parent 9dd70e7 commit 0b84249
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,7 @@ export const INSTANCE_SHARING_STATUSES = {
export const HTTP_RESPONSE_STATUS_CODES = {
FORBIDDEN: 403,
};

export const events = {
SWITCH_ACTIVE_AFFILIATION: 'SWITCH_ACTIVE_AFFILIATION',
};
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
} from './routes';
import Settings from './settings';
import { DataProvider, HoldingsProvider, LastSearchTermsProvider } from './providers';
import { events } from './constants';
import { clearStorage } from './utils';

const InventoryRouting = (props) => {
const history = useHistory();
Expand Down Expand Up @@ -202,8 +204,8 @@ const InventoryRouting = (props) => {
};

InventoryRouting.eventHandler = (event) => {
if ([coreEvents.LOGIN, 'SWITCH_ACTIVE_AFFILIATION'].includes(event)) {
sessionStorage.clear();
if ([coreEvents.LOGIN, events.SWITCH_ACTIVE_AFFILIATION].includes(event)) {
clearStorage();
}
};

Expand Down
31 changes: 22 additions & 9 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { coreEvents } from '@folio/stripes/core';

import InventoryRouting from './index';
import { removeItem } from './storage';
import { events } from './constants';

import { coreEvents } from '@folio/stripes/core';
jest.mock('./storage');

global.Storage.prototype.clear = jest.fn();
const searchTermsExpectations = () => {
expect(removeItem).toHaveBeenNthCalledWith(1, '@folio/inventory/search.instances.lastSearch');
expect(removeItem).toHaveBeenNthCalledWith(2, '@folio/inventory/search.holdings.lastSearch');
expect(removeItem).toHaveBeenNthCalledWith(3, '@folio/inventory/search.items.lastSearch');
expect(removeItem).toHaveBeenNthCalledWith(4, '@folio/inventory/browse.lastSearch');
expect(removeItem).toHaveBeenNthCalledWith(5, '@folio/inventory/search.instances.lastOffset');
expect(removeItem).toHaveBeenNthCalledWith(6, '@folio/inventory/search.holdings.lastOffset');
expect(removeItem).toHaveBeenNthCalledWith(7, '@folio/inventory/search.items.lastOffset');
expect(removeItem).toHaveBeenNthCalledWith(8, '@folio/inventory/browse.lastOffset');
}

Check failure on line 18 in src/index.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Missing semicolon

Check failure on line 18 in src/index.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Missing semicolon

describe('InventoryRouting', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('when LOGIN event is fired', () => {
it('should clear session storage', () => {
describe('when the user logs in', () => {
it('should reset search terms in session storage', () => {
InventoryRouting.eventHandler(coreEvents.LOGIN);
expect(sessionStorage.clear).toHaveBeenCalled();
searchTermsExpectations();
});
});

describe('when SWITCH_ACTIVE_AFFILIATION event is fired', () => {
it('should clear session storage', () => {
InventoryRouting.eventHandler('SWITCH_ACTIVE_AFFILIATION');
expect(sessionStorage.clear).toHaveBeenCalled();
describe('when the user switch affiliation', () => {
it('should reset search terms in session storage', () => {
InventoryRouting.eventHandler(events.SWITCH_ACTIVE_AFFILIATION);
searchTermsExpectations();
});
});
});
21 changes: 20 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import {
OKAPI_TENANT_HEADER,
CONTENT_TYPE_HEADER,
OKAPI_TOKEN_HEADER,
AUTHORITY_LINKED_FIELDS,
AUTHORITY_LINKED_FIELDS, segments,
} from './constants';
import { removeItem } from './storage';

export const areAllFieldsEmpty = fields => fields.every(item => (isArray(item)
? (isEmpty(item) || item.every(element => !element || element === '-'))
Expand Down Expand Up @@ -866,3 +867,21 @@ export const getLinkedAuthorityIds = instance => {

return flatten(authorityIdList);
};

export const clearStorage = () => {
const namespace = '@folio/inventory';

removeItem(`${namespace}/search.${segments.instances}.lastSearch`);
removeItem(`${namespace}/search.${segments.holdings}.lastSearch`);
removeItem(`${namespace}/search.${segments.items}.lastSearch`);
removeItem(`${namespace}/browse.lastSearch`);
removeItem(`${namespace}/search.${segments.instances}.lastOffset`);
removeItem(`${namespace}/search.${segments.holdings}.lastOffset`);
removeItem(`${namespace}/search.${segments.items}.lastOffset`);
removeItem(`${namespace}/browse.lastOffset`);
removeItem(`${namespace}/search.lastSegment`);

Object.values(segments).forEach((segment) => {
removeItem(`${namespace}.${segment}.lastOpenRecord`);
});
};

0 comments on commit 0b84249

Please sign in to comment.