Skip to content

Commit

Permalink
review comment: test structure & setup
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Nov 20, 2023
1 parent dee0496 commit c6e04bc
Showing 1 changed file with 67 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Test, TestingModule } from '@nestjs/testing';
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { createMock } from '@golevelup/ts-jest';
import {
ContentStorage,
LibraryStorage,
s3ConfigContent,
s3ConfigLibraries,
} from '@modules/h5p-editor/h5p-editor.module';
import { LibraryAdministration, ContentTypeCache } from '@lumieducation/h5p-server';
import { IHubContentType } from '@lumieducation/h5p-server/build/src/types';
import { IHubContentType, ILibraryAdministrationOverviewItem } from '@lumieducation/h5p-server/build/src/types';
import { H5PLibraryManagementService } from './h5p-library-management.service';

jest.mock('@lumieducation/h5p-server', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return {
...jest.requireActual('@lumieducation/h5p-server'),
LibraryAdministration: jest.fn().mockImplementation(() => {
Expand All @@ -23,17 +20,16 @@ jest.mock('@lumieducation/h5p-server', () => {
{ machineName: 'b', dependentsCount: 1 },
{ machineName: 'c', dependentsCount: 0 },
]),
deleteLibraries: jest.fn().mockResolvedValue({}),
};
}),
};
});

describe('H5PLibraryManagementService', () => {
let module: TestingModule;
let service: H5PLibraryManagementService;
let libraryStorageMock: DeepMocked<LibraryStorage>;

beforeAll(async () => {
const setup = async () => {
module = await Test.createTestingModule({
providers: [
H5PLibraryManagementService,
Expand All @@ -48,33 +44,82 @@ describe('H5PLibraryManagementService', () => {
],
}).compile();

service = module.get(H5PLibraryManagementService);
libraryStorageMock = module.get(LibraryStorage);
});
const libraries: ILibraryAdministrationOverviewItem[] = [
{
canBeDeleted: true,
canBeUpdated: true,
dependentsCount: 0,
instancesAsDependencyCount: 0,
instancesCount: 0,
isAddon: false,
machineName: 'a',
majorVersion: 1,
minorVersion: 1,
patchVersion: 1,
restricted: false,
runnable: true,
title: 'a',
},
{
canBeDeleted: true,
canBeUpdated: true,
dependentsCount: 1,
instancesAsDependencyCount: 0,
instancesCount: 0,
isAddon: false,
machineName: 'b',
majorVersion: 1,
minorVersion: 1,
patchVersion: 1,
restricted: false,
runnable: true,
title: 'b',
},
{
canBeDeleted: true,
canBeUpdated: true,
dependentsCount: 0,
instancesAsDependencyCount: 0,
instancesCount: 0,
isAddon: false,
machineName: 'c',
majorVersion: 1,
minorVersion: 1,
patchVersion: 1,
restricted: false,
runnable: true,
title: 'c',
},
];
const service = module.get(H5PLibraryManagementService);
const libraryStorageMock = module.get(LibraryStorage);

return { service, libraryStorageMock, libraries };
};

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

describe('uninstallUnwantedLibraries', () => {
it('should delete libraries not in the wanted list and with no dependents', async () => {
const s3ConfigLibrariess = s3ConfigLibraries;
libraryStorageMock.deleteLibrary.mockRejectedValueOnce({});
await service.uninstallUnwantedLibraries(['a', 'b']);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith({ machineName: 'c', dependentsCount: 0 });
expect(libraryStorageMock.deleteLibrary).not.toHaveBeenCalledWith({ machineName: 'a', dependentsCount: 0 });
const { service, libraryStorageMock, libraries } = await setup();
await service.uninstallUnwantedLibraries(['a', 'b'], libraries);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith(libraries[2]);
});

it('should not delete libraries with dependents', async () => {
const { service, libraryStorageMock, libraries } = await setup();
libraryStorageMock.deleteLibrary = jest.fn().mockResolvedValue({});
const wantedLibraries = ['a', 'b'];
await service.uninstallUnwantedLibraries(wantedLibraries);
await service.uninstallUnwantedLibraries(wantedLibraries, libraries);
expect(libraryStorageMock.deleteLibrary).not.toHaveBeenCalledWith({ machineName: 'b', dependentsCount: 1 });
});
});

describe('installLibraries', () => {
it('should install all libraries in the list', async () => {
const { service } = await setup();
const wantedLibraries = ['a', 'b', 'c'];
const installContentTypeSpy = jest.spyOn(service.contentTypeRepo, 'installContentType').mockResolvedValue([]);
await service.installLibraries(wantedLibraries);
Expand All @@ -84,6 +129,7 @@ describe('H5PLibraryManagementService', () => {
});

it('should throw an error if the library does not exist', async () => {
const { service } = await setup();
const nonExistentLibrary = 'nonExistentLibrary';
jest.spyOn(service.contentTypeCache, 'get').mockResolvedValue(undefined as unknown as Promise<IHubContentType[]>);
await expect(service.installLibraries([nonExistentLibrary])).rejects.toThrow('this library does not exist');
Expand All @@ -92,6 +138,7 @@ describe('H5PLibraryManagementService', () => {

describe('run', () => {
it('should trigger uninstallUnwantedLibraries and installLibraries', async () => {
const { service } = await setup();
const uninstallSpy = jest.spyOn(service, 'uninstallUnwantedLibraries');
const installSpy = jest.spyOn(service, 'installLibraries');

Expand All @@ -100,8 +147,8 @@ describe('H5PLibraryManagementService', () => {

await service.run();

expect(uninstallSpy).toHaveBeenCalledWith(service.libraryWishList);
expect(installSpy).toHaveBeenCalledWith(service.libraryWishList);
expect(uninstallSpy).toHaveBeenCalledTimes(1);
expect(installSpy).toHaveBeenCalledTimes(1);

uninstallSpy.mockRestore();
installSpy.mockRestore();
Expand Down

0 comments on commit c6e04bc

Please sign in to comment.