From df6dc55fa3ff3f7b80d6e2660d9c1abcdd872359 Mon Sep 17 00:00:00 2001 From: johnnadeluy Date: Wed, 15 Jul 2026 14:55:18 +0200 Subject: [PATCH] test: Add unit tests for parseCreateStatisticStatus [MIM-2674] --- backend/src/services/statisticsService.ts | 1 - backend/test/statisticsService.test.ts | 51 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/backend/src/services/statisticsService.ts b/backend/src/services/statisticsService.ts index 95056e5c..4f416871 100644 --- a/backend/src/services/statisticsService.ts +++ b/backend/src/services/statisticsService.ts @@ -458,7 +458,6 @@ export async function createStatistic( return await mapStatisticDetails(result) } -// TODO: MIM-2674: Add tests export function parseCreateStatisticStatus(body?: StatisticCreate): CreatableStatisticStatus { const statusCode = body?.status?.code diff --git a/backend/test/statisticsService.test.ts b/backend/test/statisticsService.test.ts index d63d433e..02530081 100644 --- a/backend/test/statisticsService.test.ts +++ b/backend/test/statisticsService.test.ts @@ -7,6 +7,7 @@ import { getStatisticByShortname, parseStatisticVariants, mapStatisticDetails, + parseCreateStatisticStatus, parseCreateStatisticInput, parseUpdateStatisticInput, updateStatistic, @@ -731,6 +732,22 @@ describe('statisticService', () => { expect(result).toStrictEqual(expectedResult) }) + test('returns validated statistic input for approved status when english name is provided', () => { + input.status = { code: 'A' } + input.name_en = 'Health and health services' + input.main_language = 'nn' + input.variants = [] + input.contacts = [] + + const result = parseCreateStatisticInput(input, 'A') + + expect(result).toStrictEqual({ + ...expectedResult, + name_en: 'Health and health services', + main_language: 'nn', + }) + }) + test('throws error when name is an empty string', () => { input.name = '' @@ -773,6 +790,40 @@ describe('statisticService', () => { expect(result).toStrictEqual(expectedResult) }) + test('throws error when approved status has empty english name', () => { + input.status = { code: 'A' } + input.main_language = 'nb' + input.name_en = '' + input.variants = [] + input.contacts = [] + + expect(() => parseCreateStatisticInput(input, 'A')).toThrow({ + statregError: "Field 'name_en' must be a non-empty string.", + }) + }) + + describe('parseCreateStatisticStatus', () => { + test('returns K when status code is K', () => { + expect(parseCreateStatisticStatus({ status: { code: 'K' } } as any)).toBe('K') + }) + + test('returns A when status code is A', () => { + expect(parseCreateStatisticStatus({ status: { code: 'A' } } as any)).toBe('A') + }) + + test('throws when status code is missing', () => { + expect(() => parseCreateStatisticStatus(undefined)).toThrow({ + statregError: "Field 'status' must be one of these: K, A.", + }) + }) + + test('throws when status code is not creatable', () => { + expect(() => parseCreateStatisticStatus({ status: { code: 'IA' } } as any)).toThrow({ + statregError: "Field 'status' must be one of these: K, A.", + }) + }) + }) + describe('parseUpdateStatisticInput', async () => { let input: any let expectedResult: any