fix(backend): Testen DatabaseLearningObjectProvider gerepareerd na refactoring.
This commit is contained in:
parent
ee9afab6ca
commit
51268af79c
20 changed files with 72 additions and 210 deletions
|
@ -1,43 +1,44 @@
|
|||
import { beforeAll, describe, expect, it } from 'vitest';
|
||||
import { setupTestApp } from '../../setup-tests';
|
||||
import { getLearningObjectRepository, getLearningPathRepository } from '../../../src/data/repositories';
|
||||
import example from '../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example';
|
||||
import { LearningObject } from '../../../src/entities/content/learning-object.entity';
|
||||
import databaseLearningObjectProvider from '../../../src/services/learning-objects/database-learning-object-provider';
|
||||
import { expectToBeCorrectFilteredLearningObject } from '../../test-utils/expectations';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import learningObjectExample from '../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example';
|
||||
import learningPathExample from '../../test-assets/learning-paths/pn-werking-example';
|
||||
import { LearningPath } from '../../../src/entities/content/learning-path.entity';
|
||||
import { FilteredLearningObject } from '@dwengo-1/common/interfaces/learning-content';
|
||||
|
||||
async function initExampleData(): Promise<{ learningObject: LearningObject; learningPath: LearningPath }> {
|
||||
const learningObjectRepo = getLearningObjectRepository();
|
||||
const learningPathRepo = getLearningPathRepository();
|
||||
const learningObject = learningObjectExample.createLearningObject();
|
||||
const learningPath = learningPathExample.createLearningPath();
|
||||
await learningObjectRepo.save(learningObject);
|
||||
await learningPathRepo.save(learningPath);
|
||||
return { learningObject, learningPath };
|
||||
}
|
||||
import {
|
||||
FilteredLearningObject,
|
||||
LearningObjectNode,
|
||||
LearningPathIdentifier
|
||||
} from '@dwengo-1/common/interfaces/learning-content';
|
||||
import {
|
||||
testPartiallyDatabaseAndPartiallyDwengoApiLearningPath
|
||||
} from "../../test_assets/content/learning-paths.testdata";
|
||||
import {testLearningObjectPnNotebooks} from "../../test_assets/content/learning-objects.testdata";
|
||||
import { LearningPath } from '@dwengo-1/common/dist/interfaces/learning-content';
|
||||
import {RequiredEntityData} from "@mikro-orm/core";
|
||||
import {getHtmlRenderingForTestLearningObject} from "../../test-utils/get-html-rendering";
|
||||
|
||||
const EXPECTED_TITLE_FROM_DWENGO_LEARNING_OBJECT = 'Notebook opslaan';
|
||||
|
||||
describe('DatabaseLearningObjectProvider', () => {
|
||||
let exampleLearningObject: LearningObject;
|
||||
let exampleLearningObject: RequiredEntityData<LearningObject>;
|
||||
let exampleLearningPath: LearningPath;
|
||||
let exampleLearningPathId: LearningPathIdentifier;
|
||||
|
||||
beforeAll(async () => {
|
||||
await setupTestApp();
|
||||
const exampleData = await initExampleData();
|
||||
exampleLearningObject = exampleData.learningObject;
|
||||
exampleLearningPath = exampleData.learningPath;
|
||||
exampleLearningObject = testLearningObjectPnNotebooks;
|
||||
exampleLearningPath = testPartiallyDatabaseAndPartiallyDwengoApiLearningPath;
|
||||
|
||||
exampleLearningPathId = {
|
||||
hruid: exampleLearningPath.hruid,
|
||||
language: exampleLearningPath.language as Language
|
||||
};
|
||||
});
|
||||
describe('getLearningObjectById', () => {
|
||||
it('should return the learning object when it is queried by its id', async () => {
|
||||
const result: FilteredLearningObject | null = await databaseLearningObjectProvider.getLearningObjectById(exampleLearningObject);
|
||||
expect(result).toBeTruthy();
|
||||
expectToBeCorrectFilteredLearningObject(result, exampleLearningObject);
|
||||
expectToBeCorrectFilteredLearningObject(result!, exampleLearningObject);
|
||||
});
|
||||
|
||||
it('should return the learning object when it is queried by only hruid and language (but not version)', async () => {
|
||||
|
@ -46,7 +47,7 @@ describe('DatabaseLearningObjectProvider', () => {
|
|||
language: exampleLearningObject.language,
|
||||
});
|
||||
expect(result).toBeTruthy();
|
||||
expectToBeCorrectFilteredLearningObject(result, exampleLearningObject);
|
||||
expectToBeCorrectFilteredLearningObject(result!, exampleLearningObject);
|
||||
});
|
||||
|
||||
it('should return null when queried with an id that does not exist', async () => {
|
||||
|
@ -61,7 +62,7 @@ describe('DatabaseLearningObjectProvider', () => {
|
|||
it('should return the correct rendering of the learning object', async () => {
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectHTML(exampleLearningObject);
|
||||
// Set newlines so your tests are platform-independent.
|
||||
expect(result).toEqual(example.getHTMLRendering().replace(/\r\n/g, '\n'));
|
||||
expect(result).toEqual(getHtmlRenderingForTestLearningObject(exampleLearningObject).replace(/\r\n/g, '\n'));
|
||||
});
|
||||
it('should return null for a non-existing learning object', async () => {
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectHTML({
|
||||
|
@ -73,8 +74,10 @@ describe('DatabaseLearningObjectProvider', () => {
|
|||
});
|
||||
describe('getLearningObjectIdsFromPath', () => {
|
||||
it('should return all learning object IDs from a path', async () => {
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectIdsFromPath(exampleLearningPath);
|
||||
expect(new Set(result)).toEqual(new Set(exampleLearningPath.nodes.map((it) => it.learningObjectHruid)));
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectIdsFromPath(exampleLearningPathId);
|
||||
expect(new Set(result)).toEqual(
|
||||
new Set(exampleLearningPath.nodes.map((it: LearningObjectNode) => it.learningobject_hruid))
|
||||
);
|
||||
});
|
||||
it('should throw an error if queried with a path identifier for which there is no learning path', async () => {
|
||||
await expect(
|
||||
|
@ -89,9 +92,11 @@ describe('DatabaseLearningObjectProvider', () => {
|
|||
});
|
||||
describe('getLearningObjectsFromPath', () => {
|
||||
it('should correctly return all learning objects which are on the path, even those who are not in the database', async () => {
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectsFromPath(exampleLearningPath);
|
||||
const result = await databaseLearningObjectProvider.getLearningObjectsFromPath(exampleLearningPathId);
|
||||
expect(result.length).toBe(exampleLearningPath.nodes.length);
|
||||
expect(new Set(result.map((it) => it.key))).toEqual(new Set(exampleLearningPath.nodes.map((it) => it.learningObjectHruid)));
|
||||
expect(new Set(result.map((it) => it.key))).toEqual(
|
||||
new Set(exampleLearningPath.nodes.map((it: LearningObjectNode) => it.learningobject_hruid))
|
||||
);
|
||||
|
||||
expect(result.map((it) => it.title)).toContainEqual(EXPECTED_TITLE_FROM_DWENGO_LEARNING_OBJECT);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue