Skip to content

Commit

Permalink
tests: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Dec 6, 2023
1 parent d11d95b commit 56027c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import OrganizationDetails from './OrganizationDetails';
jest.mock('@folio/stripes-acq-components', () => ({
...jest.requireActual('@folio/stripes-acq-components'),
useAcqRestrictions: jest.fn().mockReturnValue({ restrictions: {} }),
PrivilegedDonorsListContainer: jest.fn(() => 'PrivilegedDonorsListContainer'),
TagsPane: jest.fn(() => 'TagsPane'),
}));
jest.mock('@folio/stripes-smart-components/lib/Notes/NotesSmartAccordion', () => () => 'NotesSmartAccordion');
Expand Down
78 changes: 37 additions & 41 deletions src/contacts/EditContact/util.test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
import { saveContact } from './util';

const contactId = 'contactId';
const contact = { name: 'Mark' };
const contactWithId = { id: contactId, name: 'Mark' };
const org = { id: 'org1', name: 'Amazon', contacts: [] };

const contactMutator = {
contact: {
POST: jest.fn().mockReturnValue(Promise.resolve({ ...contact, id: contactId })),
PUT: jest.fn().mockReturnValue(Promise.resolve()),
},
privilegedContact: {
PUT: jest.fn().mockReturnValue(Promise.resolve()),
POST: jest.fn().mockReturnValue(Promise.resolve({ ...contact, id: contactId })),
},
contactsOrg: {
PUT: jest.fn().mockReturnValue(Promise.resolve()),
},
};

describe('EditContact utils', () => {
it('should create contact when contact without id', async () => {
const contact = { name: 'Mark' };
const mutator = {
contact: {
POST: jest.fn().mockReturnValue(Promise.resolve()),
},
};

await saveContact(mutator, contact);
await saveContact(contactMutator, contact);

expect(mutator.contact.POST).toHaveBeenCalledWith(contact);
expect(contactMutator.contact.POST).toHaveBeenCalledWith(contact);
});

it('should update contact when contact with id', async () => {
const contact = { id: 'contactId', name: 'Mark' };
const mutator = {
contact: {
PUT: jest.fn().mockReturnValue(Promise.resolve()),
},
};
await saveContact(contactMutator, contactWithId);

await saveContact(mutator, contact);

expect(mutator.contact.PUT).toHaveBeenCalledWith(contact);
expect(contactMutator.contact.PUT).toHaveBeenCalledWith(contactWithId);
});

it('should assign contact to org when contact is saved successfully and org is provided', async () => {
const contactId = 'contactId';
const contact = { name: 'Mark' };
const org = { id: 'org1', name: 'Amazon', contacts: [] };
const mutator = {
contact: {
POST: jest.fn().mockReturnValue(Promise.resolve({ ...contact, id: contactId })),
},
contactsOrg: {
PUT: jest.fn().mockReturnValue(Promise.resolve()),
},
};

await saveContact(mutator, contact, org);

expect(mutator.contactsOrg.PUT).toHaveBeenCalledWith({ ...org, contacts: [contactId] });
await saveContact(contactMutator, contact, org);

expect(contactMutator.contactsOrg.PUT).toHaveBeenCalledWith({ ...org, contacts: [contactId] });
});

it('should create privileged contact', async () => {
const isPrivilegedContactUrl = true;

await saveContact(contactMutator, contact, org, isPrivilegedContactUrl);

expect(contactMutator.privilegedContact.POST).toHaveBeenCalledWith(contact);
});

it('should update privileged contact when with id', async () => {
const contact = { id: 'contactId', name: 'Mark' };
const mutator = {
privilegedContact: {
PUT: jest.fn().mockReturnValue(Promise.resolve()),
},
};
const isPrivilegedContactUrl = true;

await saveContact(mutator, contact);
await saveContact(contactMutator, contactWithId, org, isPrivilegedContactUrl);

expect(mutator.contact.PUT).toHaveBeenCalledWith(contact);
expect(contactMutator.privilegedContact.PUT).toHaveBeenCalledWith(contactWithId);
});
});
19 changes: 7 additions & 12 deletions src/contacts/ViewContact/ViewContact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jest.mock('@folio/stripes-components/lib/Commander', () => ({
collapseAllSections: jest.fn(),
}));

const pushMock = jest.fn();
const defaultProps = {
categories: [{ value: 'Customer Service', id: 'f52ceea4-8e35' }],
contact,
Expand All @@ -40,6 +41,12 @@ const renderViewContact = (props = defaultProps) => render(
describe('ViewContact', () => {
beforeEach(() => {
global.document.createRange = global.document.originalCreateRange;
useHistory.mockClear().mockReturnValue({
push: pushMock,
location: {
pathname: '/contacts/view/123',
},
});
});

afterEach(() => {
Expand Down Expand Up @@ -106,25 +113,13 @@ describe('ViewContact', () => {
});

it('should navigate to edit view when edit shortcut is called', () => {
const pushMock = jest.fn();

useHistory.mockClear().mockReturnValue({
push: pushMock,
});

renderViewContact();
HasCommand.mock.calls[0][0].commands.find(c => c.name === 'edit').handler();

expect(pushMock).toHaveBeenCalledWith(defaultProps.editUrl);
});

it('should navigate to list view when search shortcut is called', () => {
const pushMock = jest.fn();

useHistory.mockClear().mockReturnValue({
push: pushMock,
});

renderViewContact();
HasCommand.mock.calls[0][0].commands.find(c => c.name === 'search').handler();

Expand Down

0 comments on commit 56027c1

Please sign in to comment.