Skip to content

Commit

Permalink
fix library.repo.spec code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Oct 27, 2023
1 parent aa9bd5b commit db22208
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('LibraryRepo', () => {
let module: TestingModule;
let libraryRepo: LibraryRepo;
let addonLibVersionOne: InstalledLibrary;
let addonLibVersionOneDuplicate: InstalledLibrary;
let addonLibVersionTwo: InstalledLibrary;
let em: EntityManager;

Expand Down Expand Up @@ -45,14 +46,25 @@ describe('LibraryRepo', () => {
majorVersion: 1,
minorVersion: 2,
};
const addonLibMetadataVersionOneDuplicate: ILibraryMetadata = {
runnable: false,
title: 'Duplicate',
patchVersion: 3,
machineName: 'addonVersionOne',
majorVersion: 1,
minorVersion: 2,
};
addonLibVersionOne = new InstalledLibrary(addonLibMetadataVersionOne);
addonLibVersionOne.addTo = { player: { machineNames: [testingLibVersionOne.machineName] } };

addonLibVersionOneDuplicate = new InstalledLibrary(addonLibMetadataVersionOneDuplicate);
addonLibVersionOneDuplicate.addTo = { player: { machineNames: [testingLibVersionOne.machineName] } };

const testingLibMetadataVersionTwo: ILibraryMetadata = {
runnable: false,
title: '',
patchVersion: 4,
machineName: 'testing',
machineName: 'addonVersionTwo',
majorVersion: 2,
minorVersion: 3,
};
Expand Down Expand Up @@ -100,6 +112,14 @@ describe('LibraryRepo', () => {
});
});

describe('findByName', () => {
it('should get libaries by name', async () => {
const result = await libraryRepo.findByName('addonVersionTwo');
expect(result).toBeDefined();
expect(result).toEqual([addonLibVersionTwo]);
});
});

describe('findOneByNameAndVersionOrFail', () => {
it('should get library', async () => {
const result = await libraryRepo.findOneByNameAndVersionOrFail('addonVersionOne', 1, 2);
Expand All @@ -114,6 +134,16 @@ describe('LibraryRepo', () => {
expect(error).toBeDefined();
}
});
it('should throw error', async () => {
try {
await libraryRepo.createLibrary(addonLibVersionOneDuplicate);
await libraryRepo.findOneByNameAndVersionOrFail('addonVersionOne', 1, 2);
fail('Expected Error');
} catch (error) {
expect(error).toBeDefined();
expect(error).toEqual(new Error('Multiple libraries with the same name and version found'));
}
});
});

describe('findNewestByNameAndVersion', () => {
Expand All @@ -126,9 +156,23 @@ describe('LibraryRepo', () => {

describe('findByNameAndExactVersion', () => {
it('should get a library by name and exact version', async () => {
const result = await libraryRepo.findByNameAndExactVersion('addonVersionOne', 1, 2, 3);
const result = await libraryRepo.findByNameAndExactVersion('addonVersionTwo', 2, 3, 4);
expect(result).toBeDefined();
expect(result).toEqual(addonLibVersionTwo);
});
it('should throw error', async () => {
try {
await libraryRepo.findByNameAndExactVersion('addonVersionOne', 1, 2, 3);
fail('Expected Error');
} catch (error) {
expect(error).toBeDefined();
expect(error).toEqual(new Error('too many libraries with same name and version'));
}
});
it('should return null', async () => {
const result = await libraryRepo.findByNameAndExactVersion('addonVersionTwo', 99, 3, 4);
expect(result).toBeDefined();
expect(result).toEqual(addonLibVersionOne);
expect(result).toEqual(null);
});
});
});

0 comments on commit db22208

Please sign in to comment.