Skip to content

Commit

Permalink
UIQM-724 added tests for MarcFieldContent
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanDenis committed Oct 29, 2024
1 parent 654bcfd commit 83e834d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
95 changes: 95 additions & 0 deletions src/common/entities/MarcFieldContent/MarcFieldContent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { MarcFieldContent } from './MarcFieldContent';

describe('MarcFieldContent', () => {
const content = '$a a1 $b b1 $b b2 $a a2';

let marcFieldContent = null;

beforeEach(() => {
marcFieldContent = new MarcFieldContent(content);
});

describe('when calling `forEach`', () => {
it('should call the callback with each subfield', () => {
const cb = jest.fn();

marcFieldContent.forEach(cb);

expect(cb).toHaveBeenCalledTimes(4);
expect(cb.mock.calls[0][0]).toEqual({ code: '$a', value: 'a1' });
expect(cb.mock.calls[1][0]).toEqual({ code: '$b', value: 'b1' });
expect(cb.mock.calls[2][0]).toEqual({ code: '$b', value: 'b2' });
expect(cb.mock.calls[3][0]).toEqual({ code: '$a', value: 'a2' });
});
});

describe('when calling `map`', () => {
it('should call the callback with each subfield', () => {
const cb = jest.fn();

marcFieldContent.map(cb);

expect(cb).toHaveBeenCalledTimes(4);
expect(cb.mock.calls[0][0]).toEqual({ code: '$a', value: 'a1' });
expect(cb.mock.calls[1][0]).toEqual({ code: '$b', value: 'b1' });
expect(cb.mock.calls[2][0]).toEqual({ code: '$b', value: 'b2' });
expect(cb.mock.calls[3][0]).toEqual({ code: '$a', value: 'a2' });
});
});

describe('when calling `reduce`', () => {
it('should call the callback with each subfield', () => {
const cb = jest.fn().mockImplementation((acc, cur) => [...acc, cur]);

marcFieldContent.reduce(cb, []);

expect(cb).toHaveBeenCalledTimes(4);
expect(cb.mock.calls[0][1]).toEqual({ code: '$a', value: 'a1' });
expect(cb.mock.calls[1][1]).toEqual({ code: '$b', value: 'b1' });
expect(cb.mock.calls[2][1]).toEqual({ code: '$b', value: 'b2' });
expect(cb.mock.calls[3][1]).toEqual({ code: '$a', value: 'a2' });
});
});

describe('when calling `join`', () => {
it('should transform subfields array back to a string', () => {
expect(marcFieldContent.join()).toEqual(content);
});
});

describe('when calling `append`', () => {
it('should add a new subfield to the end', () => {
marcFieldContent.append('$a', 'a3');

expect(marcFieldContent.join()).toEqual(`${content} $a a3`);
});
});

describe('when calling `prepend`', () => {
it('should add a new subfield to the beginning', () => {
marcFieldContent.prepend('$a', 'a3');

expect(marcFieldContent.join()).toEqual(`$a a3 ${content}`);
});
});

describe('when calling `removeByCode`', () => {
it('should remove all subfields by code', () => {
marcFieldContent.removeByCode('$a');

expect(marcFieldContent.join()).toEqual('$b b1 $b b2');
});
});

describe('when calling `findAllByCode`', () => {
it('should return all subfields by code', () => {
expect(marcFieldContent.findAllByCode('$a')).toEqual([{ code: '$a', value: 'a1' }, { code: '$a', value: 'a2' }]);
});
});

describe('when using a subfield getter', () => {
it('should return an array of subfields values', () => {
expect(marcFieldContent.$a).toEqual(['a1', 'a2']);
});
});
});
1 change: 0 additions & 1 deletion src/hooks/useAuthorityLinking/useAuthorityLinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from 'react';
import { useLocation } from 'react-router-dom';
import get from 'lodash/get';
import omit from 'lodash/omit';
import pick from 'lodash/pick';

import { useStripes } from '@folio/stripes/core';
Expand Down

0 comments on commit 83e834d

Please sign in to comment.