From ac10518cb8b83717770c5403ea6424dbda64ebf2 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Sun, 23 Mar 2025 13:07:10 +0100 Subject: [PATCH] refactor: find i.p.v. filter --- backend/tests/data/questions/answers.test.ts | 4 ++-- .../services/learning-path/learning-path-service.test.ts | 4 ++-- backend/tests/test-utils/expectations.ts | 8 ++++---- eslint.config.ts | 2 ++ 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/tests/data/questions/answers.test.ts b/backend/tests/data/questions/answers.test.ts index e53f1b5f..59917c3f 100644 --- a/backend/tests/data/questions/answers.test.ts +++ b/backend/tests/data/questions/answers.test.ts @@ -23,9 +23,9 @@ describe('AnswerRepository', () => { const id = new LearningObjectIdentifier('id05', Language.English, 1); const questions = await questionRepository.findAllQuestionsAboutLearningObject(id); - const question = questions.filter((it) => it.sequenceNumber === 2)[0]; + const question = questions.find((it) => it.sequenceNumber === 2); - const answers = await answerRepository.findAllAnswersToQuestion(question); + const answers = await answerRepository.findAllAnswersToQuestion(question!); expect(answers).toBeTruthy(); expect(answers).toHaveLength(2); diff --git a/backend/tests/services/learning-path/learning-path-service.test.ts b/backend/tests/services/learning-path/learning-path-service.test.ts index 2a8906df..7a45dc43 100644 --- a/backend/tests/services/learning-path/learning-path-service.test.ts +++ b/backend/tests/services/learning-path/learning-path-service.test.ts @@ -39,8 +39,8 @@ describe('LearningPathService', () => { expect(result.success).toBeTruthy(); expect(result.data?.filter((it) => it.hruid === TEST_DWENGO_LEARNING_PATH_HRUID).length).not.toBe(0); expect(result.data?.filter((it) => it.hruid === example.learningPath.hruid).length).not.toBe(0); - expect(result.data?.filter((it) => it.hruid === TEST_DWENGO_LEARNING_PATH_HRUID)[0].title).toEqual(TEST_DWENGO_LEARNING_PATH_TITLE); - expect(result.data?.filter((it) => it.hruid === example.learningPath.hruid)[0].title).toEqual(example.learningPath.title); + expect(result.data?.find((it) => it.hruid === TEST_DWENGO_LEARNING_PATH_HRUID)?.title).toEqual(TEST_DWENGO_LEARNING_PATH_TITLE); + expect(result.data?.find((it) => it.hruid === example.learningPath.hruid)?.title).toEqual(example.learningPath.title); }); it('should include both the learning objects from the Dwengo API and learning objects from the database in its response', async () => { const result = await learningPathService.fetchLearningPaths([example.learningPath.hruid], example.learningPath.language, 'the source'); diff --git a/backend/tests/test-utils/expectations.ts b/backend/tests/test-utils/expectations.ts index 8f33bd8f..c3fa5a43 100644 --- a/backend/tests/test-utils/expectations.ts +++ b/backend/tests/test-utils/expectations.ts @@ -21,7 +21,7 @@ export function expectToBeCorrectEntity(actual: { entity: T; n } for (const property in expected.entity) { if ( - property! in IGNORE_PROPERTIES && + property in IGNORE_PROPERTIES && expected.entity[property] !== undefined && // If we don't expect a certain value for a property, we assume it can be filled in by the database however it wants. typeof expected.entity[property] !== 'function' // Functions obviously are not persisted via the database ) { @@ -136,10 +136,10 @@ export function expectToBeCorrectLearningPath( version: node.version, }; expect(expectedLearningPathNodes.keys()).toContainEqual(nodeKey); - const expectedNode = [...expectedLearningPathNodes.entries()].filter( + const expectedNode = [...expectedLearningPathNodes.entries()].find( ([key, _]) => key.learningObjectHruid === nodeKey.learningObjectHruid && key.language === node.language && key.version === node.version - )[0][1]; - expect(node.start_node).toEqual(expectedNode?.startNode); + )![1]; + expect(node.start_node).toEqual(expectedNode.startNode); expect(new Set(node.transitions.map((it) => it.next.hruid))).toEqual( new Set(expectedNode.transitions.map((it) => it.next.learningObjectHruid)) diff --git a/eslint.config.ts b/eslint.config.ts index bc42bd39..5a23b135 100644 --- a/eslint.config.ts +++ b/eslint.config.ts @@ -109,6 +109,8 @@ export default [ '@typescript-eslint/parameter-properties': 'off', + '@typescript-eslint/prefer-find': 'warn', + '@typescript-eslint/prefer-function-type': 'error', '@typescript-eslint/prefer-readonly-parameter-types': 'off',