test(backend): Testen LearningObjectService.getLearningObject[Id]sFromPath toegevoegd

This commit is contained in:
Gerald Schmittinger 2025-03-11 05:07:35 +01:00
parent 7af1404088
commit b630b9a45d

View file

@ -1,12 +1,14 @@
import { beforeAll, describe, expect, it } from 'vitest';
import { setupTestApp } from '../../setup-tests';
import { LearningObject } from '../../../src/entities/content/learning-object.entity';
import { getLearningObjectRepository } from '../../../src/data/repositories';
import {beforeAll, describe, expect, it} from 'vitest';
import {setupTestApp} from '../../setup-tests';
import {LearningObject} from '../../../src/entities/content/learning-object.entity';
import {getLearningObjectRepository, getLearningPathRepository} from '../../../src/data/repositories';
import learningObjectExample from '../../test-assets/learning-objects/pn-werkingnotebooks/pn-werkingnotebooks-example';
import learningObjectService from '../../../src/services/learning-objects/learning-object-service';
import { LearningObjectIdentifier } from '../../../src/interfaces/learning-content';
import { Language } from '../../../src/entities/content/language';
import { EnvVars, getEnvVar } from '../../../src/util/envvars';
import {LearningObjectIdentifier, LearningPathIdentifier} from '../../../src/interfaces/learning-content';
import {Language} from '../../../src/entities/content/language';
import {EnvVars, getEnvVar} from '../../../src/util/envvars';
import {LearningPath} from "../../../src/entities/content/learning-path.entity";
import learningPathExample from "../../test-assets/learning-paths/pn-werking-example";
const TEST_LEARNING_OBJECT_TITLE = 'Test title';
const EXPECTED_DWENGO_LEARNING_OBJECT_TITLE = 'Werken met notebooks';
@ -16,20 +18,31 @@ const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifier = {
version: 3,
};
async function initExampleData(): Promise<LearningObject> {
const DWENGO_TEST_LEARNING_PATH_ID: LearningPathIdentifier = {
hruid: 'pn_werking',
language: Language.Dutch
};
const DWENGO_TEST_LEARNING_PATH_HRUIDS = new Set(["pn_werkingnotebooks", "pn_werkingnotebooks2", "pn_werkingnotebooks3"]);
async function initExampleData(): Promise<{ learningObject: LearningObject; learningPath: LearningPath }> {
const learningObjectRepo = getLearningObjectRepository();
const learningPathRepo = getLearningPathRepository();
const learningObject = learningObjectExample.createLearningObject();
learningObject.title = TEST_LEARNING_OBJECT_TITLE;
const learningPath = learningPathExample.createLearningPath();
await learningObjectRepo.save(learningObject);
return learningObject;
await learningPathRepo.save(learningPath);
return { learningObject, learningPath };
}
describe('LearningObjectService', () => {
let exampleLearningObject: LearningObject;
let exampleLearningPath: LearningPath;
beforeAll(async () => {
await setupTestApp();
exampleLearningObject = await initExampleData();
const exampleData = await initExampleData();
exampleLearningObject = exampleData.learningObject;
exampleLearningPath = exampleData.learningPath;
});
describe('getLearningObjectById', () => {
@ -80,4 +93,34 @@ describe('LearningObjectService', () => {
expect(result).toBeNull();
});
});
describe('getLearningObjectsFromPath', () => {
it("returns all learning objects when a learning path in the database is queried", async () => {
const result = await learningObjectService.getLearningObjectsFromPath(exampleLearningPath);
expect(result.map(it => it.key)).toEqual(exampleLearningPath.nodes.map(it => it.learningObjectHruid));
});
it("also returns all learning objects when a learning path from the Dwengo API is queried", async () => {
const result = await learningObjectService.getLearningObjectsFromPath(DWENGO_TEST_LEARNING_PATH_ID);
expect(new Set(result.map(it => it.key))).toEqual(DWENGO_TEST_LEARNING_PATH_HRUIDS);
});
it("returns an empty list when queried with a non-existing learning path id", async () => {
const result = await learningObjectService.getLearningObjectsFromPath({hruid: "non_existing", language: Language.Dutch});
expect(result).toEqual([]);
});
});
describe('getLearningObjectIdsFromPath', () => {
it("returns all learning objects when a learning path in the database is queried", async () => {
const result = await learningObjectService.getLearningObjectIdsFromPath(exampleLearningPath);
expect(result).toEqual(exampleLearningPath.nodes.map(it => it.learningObjectHruid));
});
it("also returns all learning object hruids when a learning path from the Dwengo API is queried", async () => {
const result = await learningObjectService.getLearningObjectIdsFromPath(DWENGO_TEST_LEARNING_PATH_ID);
expect(new Set(result)).toEqual(DWENGO_TEST_LEARNING_PATH_HRUIDS);
});
it("returns an empty list when queried with a non-existing learning path id", async () => {
const result = await learningObjectService.getLearningObjectIdsFromPath({hruid: "non_existing", language: Language.Dutch});
expect(result).toEqual([]);
});
});
});