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 af86662
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/common/entities/MarcFieldContent/MarcFieldContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ export class MarcFieldContent {
return new Proxy(this, this);
}

map(callback) {
return this.subfieldsArr.map(callback);
}

reduce(...args) {
return this.subfieldsArr.reduce(...args);
}

forEach(callback) {
return this.subfieldsArr.forEach(callback);
}
Expand Down
67 changes: 67 additions & 0 deletions src/common/entities/MarcFieldContent/MarcFieldContent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 `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']);
});
});
});

0 comments on commit af86662

Please sign in to comment.