Skip to content

Commit

Permalink
add tests for library.entity
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Nov 1, 2023
1 parent 1b5d4fa commit 6912a15
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
87 changes: 85 additions & 2 deletions apps/server/src/modules/h5p-editor/entity/library.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ILibraryMetadata } from '@lumieducation/h5p-server';
import { FileMetadata, InstalledLibrary } from './library.entity';
import { FileMetadata, InstalledLibrary, LibraryName, Path } from './library.entity';

describe('InstalledLibrary', () => {
let addonLibVersionOne: InstalledLibrary;
let addonLibVersionOneMinorChange: InstalledLibrary;
let addonLibVersionOnePatchChange: InstalledLibrary;
let addonLibVersionTwo: InstalledLibrary;

beforeAll(() => {
Expand Down Expand Up @@ -59,6 +60,32 @@ describe('InstalledLibrary', () => {
addonLibVersionOneMinorChange = new InstalledLibrary(addonLibMetadataVersionOneMinorChange);
addonLibVersionOneMinorChange.addTo = { player: { machineNames: [testingLibVersionOneMinorChange.machineName] } };

const testingLibMetadataVersionOnePatchChange: ILibraryMetadata = {
runnable: false,
title: '',
patchVersion: 5,
machineName: 'testing',
majorVersion: 1,
minorVersion: 2,
};
const testingLibVersionOnePatchChange = new InstalledLibrary(testingLibMetadataVersionOnePatchChange);
testingLibVersionOne.files.push(
new FileMetadata('file1', new Date(), 2),
new FileMetadata('file2', new Date(), 4),
new FileMetadata('file3', new Date(), 6)
);

const addonLibMetadataVersionOnePatchChange: ILibraryMetadata = {
runnable: false,
title: '',
patchVersion: 5,
machineName: 'addonVersionOne',
majorVersion: 1,
minorVersion: 2,
};
addonLibVersionOnePatchChange = new InstalledLibrary(addonLibMetadataVersionOnePatchChange);
addonLibVersionOnePatchChange.addTo = { player: { machineNames: [testingLibVersionOnePatchChange.machineName] } };

const testingLibMetadataVersionTwo: ILibraryMetadata = {
runnable: false,
title: '',
Expand Down Expand Up @@ -119,7 +146,7 @@ describe('InstalledLibrary', () => {
});

describe('compareVersions', () => {
describe('when calling compareVersions with different Libraries', () => {
describe('when calling compareVersions with Major Change', () => {
it('should return -1 and call simple_compare once', () => {
const simpleCompareSpy = jest.spyOn(InstalledLibrary, 'simple_compare');
const result = addonLibVersionOne.compareVersions(addonLibVersionTwo);
Expand All @@ -136,5 +163,61 @@ describe('InstalledLibrary', () => {
expect(simpleCompareSpy).toHaveBeenCalledTimes(3);
});
});

describe('when calling compareVersions with same Major & Minor Versions', () => {
it('should return call simple_compare with patch versions', () => {
const simpleCompareSpy = jest.spyOn(InstalledLibrary, 'simple_compare');
const result = addonLibVersionOne.compareVersions(addonLibVersionOnePatchChange);
expect(result).toBe(-1);
expect(simpleCompareSpy).toHaveBeenCalledWith(
addonLibVersionOne.patchVersion,
addonLibVersionOnePatchChange.patchVersion
);
});
});
});
});

describe('LibraryName', () => {
let libraryName: LibraryName;

beforeEach(() => {
libraryName = new LibraryName('test', 1, 2);
});

it('should be defined', () => {
expect(libraryName).toBeDefined();
});

it('should create libraryName', () => {
const newlibraryName = new LibraryName('newtest', 1, 2);
expect(newlibraryName.machineName).toEqual('newtest');
});

it('should change libraryName', () => {
libraryName.machineName = 'changed-name';
expect(libraryName.machineName).toEqual('changed-name');
});
});

describe('Path', () => {
let path: Path;

beforeEach(() => {
path = new Path('');
});

it('should be defined', () => {
expect(path).toBeDefined();
});

it('should create path', () => {
const newPath = new Path('test-path');
expect(newPath.path).toEqual('test-path');
});

it('should change path', () => {
path.path = 'new-path';
expect(path.path).toEqual('new-path');
});
});
11 changes: 1 addition & 10 deletions apps/server/src/modules/h5p-editor/entity/library.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,7 @@ export class InstalledLibrary extends BaseEntityWithTimestamps implements IInsta
if (result !== 0) {
return result;
}
if (this.patchVersion === undefined) {
if (otherLibrary.patchVersion === undefined) {
return 0;
}
return -1;
}
if (otherLibrary.patchVersion === undefined) {
return 1;
}
return InstalledLibrary.simple_compare(this.patchVersion, otherLibrary.patchVersion);
return InstalledLibrary.simple_compare(this.patchVersion, otherLibrary.patchVersion as number);
}

constructor(libraryMetadata: ILibraryMetadata, restricted = false, files: FileMetadata[] = []) {
Expand Down

0 comments on commit 6912a15

Please sign in to comment.