Skip to content

Commit

Permalink
UISACQCOMP-171 Add unit test for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Dec 14, 2023
1 parent 8cf9d92 commit e6563a8
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/hooks/useCampuses/useCampuses.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { campus } from '../../../test/jest/fixtures';
import { useCampuses } from './useCampuses';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('useCampuses', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
get: () => ({
json: () => Promise.resolve({ loccamps: [campus] }),
}),
});
});

it('should return list of campuses', async () => {
const { result, waitFor } = renderHook(() => useCampuses(), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

expect(result.current.campuses).toEqual([campus]);
});
});
32 changes: 32 additions & 0 deletions lib/hooks/useInstitutions/useInstitutions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { institution } from '../../../test/jest/fixtures';
import { useInstitutions } from './useInstitutions';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('useInstitutions', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
get: () => ({
json: () => Promise.resolve({ locinsts: [institution] }),
}),
});
});

it('should return list of institutions', async () => {
const { result, waitFor } = renderHook(() => useInstitutions(), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

expect(result.current.institutions).toEqual([institution]);
});
});
32 changes: 32 additions & 0 deletions lib/hooks/useLibraries/useLibraries.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { library } from '../../../test/jest/fixtures';
import { useLibraries } from './useLibraries';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('useLibraries', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
get: () => ({
json: () => Promise.resolve({ loclibs: [library] }),
}),
});
});

it('should return list of libraries', async () => {
const { result, waitFor } = renderHook(() => useLibraries(), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

expect(result.current.libraries).toEqual([library]);
});
});
32 changes: 32 additions & 0 deletions lib/hooks/useLocations/useLocations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { location } from '../../../test/jest/fixtures';
import { useLocations } from './useLocations';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('useLocations', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
get: () => ({
json: () => Promise.resolve({ locations: [location] }),
}),
});
});

it('should return list of locations', async () => {
const { result, waitFor } = renderHook(() => useLocations(), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

expect(result.current.locations).toEqual([location]);
});
});
1 change: 1 addition & 0 deletions test/jest/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './batchGroup';
export * from './invoice';
export * from './invoiceExportReport';
export * from './invoiceLine';
export * from './location';
export * from './orderAuditEvent';
export * from './orderLine';
export * from './orderLineAuditEvent';
Expand Down
39 changes: 39 additions & 0 deletions test/jest/fixtures/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const institution = {
id: '28a372df-a8c9-439e-91fd-6d606b52e1f4',
name: 'Test institution',
code: 'TS'
};

export const campus = {
id: '34790631-dd30-4ed0-b895-39125d866e44',
name: 'Riverside Campus',
code: 'RS',
institutionId: [institution.id],
};

export const library = {
id: 'e81d1fc0-6801-4dec-b9b9-e105fa575cf0',
name: 'Main Library',
code: 'ML',
campusId: [campus.id],
};

export const location = {
id: 'd9cd0bed-1b49-4b5e-a7bd-064b8d177231',
name: 'Miller General Stacks',
code: 'TS/RS/ML/GS',
isActive: true,
description: 'The very general stacks of Miller',
discoveryDisplayName: 'Miller General',
institutionId: [institution.id],
campusId: [campus.id],
libraryId: [library.id],
details: {
a: 'b',
foo: 'bar'
},
primaryServicePoint: '79faacf1-4ba4-42c7-8b2a-566b259e4641',
servicePointIds: [
'79faacf1-4ba4-42c7-8b2a-566b259e4641',
],
};

0 comments on commit e6563a8

Please sign in to comment.