Skip to content

Commit

Permalink
review comment: test guidlines
Browse files Browse the repository at this point in the history
  • Loading branch information
casparneumann-cap committed Nov 20, 2023
1 parent 6f30244 commit 98c0bf2
Showing 1 changed file with 174 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { createMock } from '@golevelup/ts-jest';
import { ContentStorage, LibraryStorage } from '@src/modules/h5p-editor/service';
import { IHubContentType, ILibraryAdministrationOverviewItem } from '@lumieducation/h5p-server/build/src/types';
import { s3ConfigContent, s3ConfigLibraries } from '@src/modules/h5p-editor/h5p-editor.config';
import { H5PLibraryManagementService } from './h5p-library-management.service';

jest.mock('@lumieducation/h5p-server', () => {
Expand All @@ -25,7 +24,7 @@ jest.mock('@lumieducation/h5p-server', () => {
describe('H5PLibraryManagementService', () => {
let module: TestingModule;

const setup = async () => {
beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
H5PLibraryManagementService,
Expand All @@ -39,126 +38,198 @@ describe('H5PLibraryManagementService', () => {
},
],
}).compile();

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 { service, libraryStorageMock, libraries } = await setup();
await service.uninstallUnwantedLibraries(['a', 'b'], libraries);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith(libraries[2]);
describe('uninstallUnwantedLibraries is called', () => {
describe('when wantedLibraries have no dependands', () => {
const setup = () => {
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 };
};
it('should delete libraries not in the wanted list and with no dependents', async () => {
const { service, libraryStorageMock, libraries } = setup();
const wantedLibraries = ['a', 'c'];
await service.uninstallUnwantedLibraries(wantedLibraries, libraries);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith(libraries[0]);
expect(libraryStorageMock.deleteLibrary).not.toHaveBeenCalledWith(libraries[1]);
expect(libraryStorageMock.deleteLibrary).not.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, libraries);
expect(libraryStorageMock.deleteLibrary).not.toHaveBeenCalledWith({ machineName: 'b', dependentsCount: 1 });
describe('when wantedLIbraries have dependands', () => {
const setup = () => {
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 };
};
it('should not delete libraries with dependents', async () => {
const { service, libraryStorageMock, libraries } = setup();
libraryStorageMock.deleteLibrary = jest.fn().mockResolvedValueOnce({});
const wantedLibraries = ['a', 'b'];
await service.uninstallUnwantedLibraries(wantedLibraries, libraries);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith(libraries[0]);
expect(libraryStorageMock.deleteLibrary).not.toHaveBeenCalledWith(libraries[1]);
expect(libraryStorageMock.deleteLibrary).toHaveBeenCalledWith(libraries[2]);
});
});
});

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);
for (const libName of wantedLibraries) {
expect(installContentTypeSpy).toHaveBeenCalledWith(libName, expect.anything());
}
describe('installLibraries is called', () => {
describe('when libraries exist', () => {
const setup = () => {
const service = module.get(H5PLibraryManagementService);

return { service };
};
it('should install all libraries in the list', async () => {
const { service } = setup();
const wantedLibraries = ['a', 'b', 'c'];
const installContentTypeSpy = jest.spyOn(service.contentTypeRepo, 'installContentType').mockResolvedValue([]);
await service.installLibraries(wantedLibraries);
for (const libName of wantedLibraries) {
expect(installContentTypeSpy).toHaveBeenCalledWith(libName, expect.anything());
}
});
});

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');
describe('when libraries does not exist', () => {
const setup = () => {
const service = module.get(H5PLibraryManagementService);

return { service };
};
it('should throw an error if the library does not exist', async () => {
const { service } = setup();
const nonExistentLibrary = 'nonExistentLibrary';
jest
.spyOn(service.contentTypeCache, 'get')
.mockResolvedValueOnce(undefined as unknown as Promise<IHubContentType[]>);
await expect(service.installLibraries([nonExistentLibrary])).rejects.toThrow('this library does not exist');
});
});
});

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');
describe('run is called', () => {
describe('when run has been called successfully', () => {
const setup = () => {
const service = module.get(H5PLibraryManagementService);

uninstallSpy.mockResolvedValue(undefined);
installSpy.mockResolvedValue(undefined);
return { service };
};
it('should trigger uninstallUnwantedLibraries and installLibraries', async () => {
const { service } = setup();
const uninstallSpy = jest.spyOn(service, 'uninstallUnwantedLibraries').mockResolvedValueOnce(undefined);
const installSpy = jest.spyOn(service, 'installLibraries').mockResolvedValueOnce(undefined);

await service.run();
await service.run();

expect(uninstallSpy).toHaveBeenCalledTimes(1);
expect(installSpy).toHaveBeenCalledTimes(1);
expect(uninstallSpy).toHaveBeenCalledTimes(1);
expect(installSpy).toHaveBeenCalledTimes(1);

uninstallSpy.mockRestore();
installSpy.mockRestore();
uninstallSpy.mockRestore();
installSpy.mockRestore();
});
});
});
});

describe('config', () => {
it('should get Object s3ConfigLibraries', () => {
const s3ConfigLibrariesObj = s3ConfigLibraries;
expect(s3ConfigLibrariesObj).toBeDefined();
});
it('should get Object s3ConfigLibraries', () => {
const s3ConfigContentObj = s3ConfigContent;
expect(s3ConfigContentObj).toBeDefined();
});
});

0 comments on commit 98c0bf2

Please sign in to comment.