Skip to content

Commit

Permalink
create tests for library.repo
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Oct 26, 2023
1 parent 3079af6 commit bef74d0
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 65 deletions.
65 changes: 0 additions & 65 deletions apps/server/src/modules/h5p-editor/repo/libary.repo.spec.ts

This file was deleted.

134 changes: 134 additions & 0 deletions apps/server/src/modules/h5p-editor/repo/library.repo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { cleanupCollections } from '@shared/testing';
import { MongoMemoryDatabaseModule } from '@shared/infra/database';

import { ILibraryMetadata } from '@lumieducation/h5p-server';
import { LibraryRepo } from './library.repo';
import { FileMetadata, InstalledLibrary } from '../entity';

describe('LibraryRepo', () => {
let module: TestingModule;
let libraryRepo: LibraryRepo;
let addonLibVersionOne: InstalledLibrary;
let addonLibVersionTwo: InstalledLibrary;
let em: EntityManager;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [MongoMemoryDatabaseModule.forRoot({ entities: [InstalledLibrary] })],
providers: [LibraryRepo],
}).compile();
libraryRepo = module.get(LibraryRepo);
em = module.get(EntityManager);

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

const addonLibMetadataVersionOne: ILibraryMetadata = {
runnable: false,
title: '',
patchVersion: 3,
machineName: 'addonVersionOne',
majorVersion: 1,
minorVersion: 2,
};
addonLibVersionOne = new InstalledLibrary(addonLibMetadataVersionOne);
addonLibVersionOne.addTo = { player: { machineNames: [testingLibVersionOne.machineName] } };

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

const addonLibMetadataVersionTwo: ILibraryMetadata = {
runnable: false,
title: '',
patchVersion: 4,
machineName: 'addonVersionTwo',
majorVersion: 2,
minorVersion: 3,
};
addonLibVersionTwo = new InstalledLibrary(addonLibMetadataVersionTwo);
addonLibVersionTwo.addTo = { player: { machineNames: [testingLibVersionTwo.machineName] } };

await libraryRepo.createLibrary(addonLibVersionOne);
await libraryRepo.createLibrary(addonLibVersionTwo);
});

afterAll(async () => {
await cleanupCollections(em);
await module.close();
});

describe('createLibrary', () => {
it('should save a Library', async () => {
const saveSpy = jest.spyOn(libraryRepo, 'save').mockResolvedValueOnce(undefined);
await libraryRepo.createLibrary(addonLibVersionOne);
expect(saveSpy).toHaveBeenCalledWith(addonLibVersionOne);
saveSpy.mockRestore();
});
});

describe('getAll', () => {
it('should get all libaries', async () => {
const result = await libraryRepo.getAll();
expect(result).toBeDefined();
expect(result).toHaveLength(2);
});
});

describe('findOneByNameAndVersionOrFail', () => {
it('should get library', async () => {
const result = await libraryRepo.findOneByNameAndVersionOrFail('addonVersionOne', 1, 2);
expect(result).toBeDefined();
});

it('should throw error', async () => {
try {
await libraryRepo.findOneByNameAndVersionOrFail('notexistinglibrary', 1, 2);
fail('Expected Error');
} catch (error) {
expect(error).toBeDefined();
}
});
});

describe('findNewestByNameAndVersion', () => {
it('should get a library by name and version', async () => {
const result = await libraryRepo.findNewestByNameAndVersion('addonVersionTwo', 2, 3);
expect(result).toBeDefined();
expect(result).toEqual(addonLibVersionTwo);
});
});

describe('findByNameAndExactVersion', () => {
it('should get a library by name and exact version', async () => {
const result = await libraryRepo.findByNameAndExactVersion('addonVersionOne', 1, 2, 3);
expect(result).toBeDefined();
expect(result).toEqual(addonLibVersionOne);
});
});
});

0 comments on commit bef74d0

Please sign in to comment.