-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redaktør: Automatically query for global ytelse when querying for spe…
…cific hjemmel
- Loading branch information
1 parent
cc30342
commit 2c3d122
Showing
4 changed files
with
84 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,71 @@ | ||
import { describe, expect, it } from 'bun:test'; | ||
import { GLOBAL, LIST_DELIMITER } from '@app/components/smart-editor-texts/types'; | ||
import { paramsWithGlobalSections } from '@app/redux-api/redaktør-helpers'; | ||
import { GLOBAL, LIST_DELIMITER, WILDCARD } from '@app/components/smart-editor-texts/types'; | ||
import { getListWithGlobal, paramsWithGlobalQueries } from '@app/redux-api/redaktør-helpers'; | ||
import type { IGetMaltekstseksjonParams } from '@app/types/common-text-types'; | ||
|
||
describe('paramsWithGlobalSections', () => { | ||
it('should add global sections to templateSectionIdList', () => { | ||
const untouched = { | ||
enhetIdList: ['enhet'], | ||
describe('getListWithGlobal', () => { | ||
it('should add global items to list', () => { | ||
const list = [`some-template${LIST_DELIMITER}some-section`]; | ||
|
||
const result = getListWithGlobal(list); | ||
const expected = [`${GLOBAL}${LIST_DELIMITER}some-section`, `some-template${LIST_DELIMITER}some-section`]; | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should not do anything if list already contains all global items', () => { | ||
const list = [ | ||
`${GLOBAL}${LIST_DELIMITER}some-other-section`, | ||
`${GLOBAL}${LIST_DELIMITER}some-section`, | ||
`some-other-template${LIST_DELIMITER}some-other-section`, | ||
`some-template${LIST_DELIMITER}some-section`, | ||
]; | ||
|
||
expect(getListWithGlobal(list)).toEqual(list); | ||
}); | ||
|
||
it('should add GLOBAL to list if last part if wildcard', () => { | ||
const list = [`some-template${LIST_DELIMITER}${WILDCARD}`]; | ||
|
||
const result = getListWithGlobal(list); | ||
|
||
const expected = [GLOBAL, `some-template${LIST_DELIMITER}${WILDCARD}`]; | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should not add GLOBAL if GLOBAL is already in list', () => { | ||
const list = [`some-template${LIST_DELIMITER}${WILDCARD}`, GLOBAL]; | ||
|
||
const result = getListWithGlobal(list); | ||
|
||
const expected = [GLOBAL, `some-template${LIST_DELIMITER}${WILDCARD}`]; | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('paramsWithGlobalQueries', () => { | ||
it('should add global sections and hjemler', () => { | ||
const untouched: Partial<IGetMaltekstseksjonParams> = { | ||
enhetIdList: ['some-enhet'], | ||
utfallIdList: 'utfall', | ||
}; | ||
|
||
const params: IGetMaltekstseksjonParams = { | ||
const params = { | ||
...untouched, | ||
templateSectionIdList: [`some-template${LIST_DELIMITER}some-section`], | ||
ytelseHjemmelIdList: [`some-ytelse${LIST_DELIMITER}some-hjemmel`], | ||
}; | ||
|
||
const result = paramsWithGlobalSections(params); | ||
const result = paramsWithGlobalQueries(params); | ||
|
||
const expected = { | ||
...untouched, | ||
templateSectionIdList: [`${GLOBAL}${LIST_DELIMITER}some-section`, `some-template${LIST_DELIMITER}some-section`], | ||
ytelseHjemmelIdList: [`${GLOBAL}${LIST_DELIMITER}some-hjemmel`, `some-ytelse${LIST_DELIMITER}some-hjemmel`], | ||
}; | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should not do anything if templateSectionIdList already contains all global templates', () => { | ||
const params: IGetMaltekstseksjonParams = { | ||
enhetIdList: ['enhet'], | ||
utfallIdList: 'utfall', | ||
templateSectionIdList: [ | ||
`${GLOBAL}${LIST_DELIMITER}some-other-section`, | ||
`${GLOBAL}${LIST_DELIMITER}some-section`, | ||
`some-other-template${LIST_DELIMITER}some-other-section`, | ||
`some-template${LIST_DELIMITER}some-section`, | ||
], | ||
}; | ||
|
||
expect(params).toEqual(paramsWithGlobalSections(params)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,38 @@ | ||
import { GLOBAL, LIST_DELIMITER } from '@app/components/smart-editor-texts/types'; | ||
import { GLOBAL, LIST_DELIMITER, WILDCARD } from '@app/components/smart-editor-texts/types'; | ||
import type { IGetMaltekstseksjonParams, IGetTextsParams } from '@app/types/common-text-types'; | ||
|
||
type Params = IGetMaltekstseksjonParams | IGetTextsParams; | ||
|
||
export const paramsWithGlobalSections = <T extends Params>(params: T): T => { | ||
if (params.templateSectionIdList === undefined) { | ||
return params; | ||
} | ||
export const getListWithGlobal = (list: string[]) => { | ||
const newList = [...list]; | ||
|
||
for (const item of list) { | ||
const [, lastPart] = item.split(LIST_DELIMITER); | ||
|
||
const list = [...params.templateSectionIdList]; | ||
if (lastPart === WILDCARD || lastPart === undefined) { | ||
if (!newList.includes(GLOBAL)) { | ||
newList.push(GLOBAL); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
for (const templateSectionId of list) { | ||
const [, sectionId] = templateSectionId.split(LIST_DELIMITER); | ||
const globalSection = `${GLOBAL}${LIST_DELIMITER}${sectionId}`; | ||
const withGlobal = `${GLOBAL}${LIST_DELIMITER}${lastPart}`; | ||
|
||
if (!list.includes(globalSection)) { | ||
list.push(globalSection); | ||
if (!newList.includes(withGlobal)) { | ||
newList.push(withGlobal); | ||
} | ||
} | ||
|
||
return { ...params, templateSectionIdList: list.toSorted((a, b) => a.localeCompare(b)) }; | ||
return newList.toSorted((a, b) => a.localeCompare(b)); | ||
}; | ||
|
||
export const paramsWithGlobalQueries = <T extends Params>(params: T): T => { | ||
const templateSectionIdList = | ||
params.templateSectionIdList === undefined ? undefined : getListWithGlobal(params.templateSectionIdList); | ||
|
||
const ytelseHjemmelIdList = | ||
params.ytelseHjemmelIdList === undefined ? undefined : getListWithGlobal(params.ytelseHjemmelIdList); | ||
|
||
return { ...params, templateSectionIdList, ytelseHjemmelIdList }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters