mon-entreprise/site/test/useSearchParamsSimulationSh...

152 lines
3.7 KiB
JavaScript
Raw Normal View History

import rules from 'modele-social'
2022-09-05 17:16:54 +00:00
import Engine, { parsePublicodes } from 'publicodes'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import yaml from 'yaml'
2022-11-03 16:32:04 +00:00
import {
2022-09-05 17:16:54 +00:00
cleanSearchParams,
getRulesParamNames,
2024-02-09 11:09:08 +00:00
getSearchParams,
getSituationFromSearchParams,
} from '../source/components/utils/useSearchParamsSimulationSharing'
describe('identifiant court', () => {
2022-02-10 11:07:19 +00:00
const questions = Object.entries(parsePublicodes(rules).parsedRules)
.filter(([, ruleNode]) => ruleNode.rawNode['identifiant court'])
.map(([dottedName, ruleNode]) => [
dottedName,
ruleNode.rawNode['identifiant court'],
])
it('should be unique amongst rules', () => {
2024-02-23 10:31:44 +00:00
expect(questions).toHaveLength(
new Set(questions.map(([, name]) => name)).size
)
})
})
describe('useSearchParamsSimulationSharing', () => {
2022-09-05 17:16:54 +00:00
const engine = new Engine(
yaml.parse(`
rule with:
2022-09-05 17:16:54 +00:00
identifiant court: panta
formule: 0
rule without:
2022-09-05 17:16:54 +00:00
formule: 0
`)
)
const dottedNameParamName = getRulesParamNames(engine.getParsedRules())
describe('getSearchParamsFromSituation', () => {
it('builds search params with and without identifiant court', () => {
expect(
2024-02-09 11:09:08 +00:00
getSearchParams(
engine,
{ 'rule with': '2000€/mois', 'rule without': '1000€/mois' },
2024-02-09 11:09:08 +00:00
dottedNameParamName,
'€/an'
).toString()
2024-02-23 10:31:44 +00:00
).toBe(
new URLSearchParams(
2024-02-09 11:09:08 +00:00
'panta=2000€/mois&rule without=1000€/mois&unite=€/an'
).toString()
)
})
2022-08-02 10:17:17 +00:00
it('builds search params with object', () => {
expect(
2024-02-09 11:09:08 +00:00
getSearchParams(
engine,
{ 'rule without': { 1: 2, 3: { 4: '5' } } },
2024-02-09 11:09:08 +00:00
dottedNameParamName,
'€/an'
).toString()
2024-02-23 10:31:44 +00:00
).toBe(
2024-02-09 11:09:08 +00:00
new URLSearchParams('rule without={"1":2,"3":{"4":"5"}}').toString() +
'&unite=%E2%82%AC%2Fan'
)
})
it('handles empty situation with proper defaults', () => {
expect(
2024-02-09 11:09:08 +00:00
getSearchParams(engine, {}, dottedNameParamName, '€/mois').toString()
2024-02-23 10:31:44 +00:00
).toBe('unite=%E2%82%AC%2Fmois')
})
})
describe('getSituationFromSearchParams', () => {
it('reads search params with and without identifiant court', () => {
expect(
getSituationFromSearchParams(
new URLSearchParams('panta=2000€/mois&rule without=1000€/mois'),
dottedNameParamName
)
2024-02-23 10:31:44 +00:00
).toEqual({
'rule with': '2000€/mois',
'rule without': '1000€/mois',
})
})
it('handles empty search params with proper defaults', () => {
expect(
getSituationFromSearchParams(
new URLSearchParams(''),
dottedNameParamName
)
2024-02-23 10:31:44 +00:00
).toEqual({})
})
})
})
describe('useSearchParamsSimulationSharing hook', () => {
2022-09-05 17:16:54 +00:00
const { parsedRules } = parsePublicodes(
yaml.parse(
`
rule with:
2022-09-05 17:16:54 +00:00
identifiant court: panta
formule: 0
rule without:
2022-09-05 17:16:54 +00:00
formule: 0
`
)
)
const dottedNameParamName = getRulesParamNames(parsedRules)
let setSearchParams
beforeEach(() => {
setSearchParams = vi.fn()
})
it('removes searchParams that are in situation', () => {
2024-02-09 11:09:08 +00:00
const searchParams = new URLSearchParams(
'panta=123&rule without=333&unite=€/mois'
)
const newSituation = getSituationFromSearchParams(
searchParams,
dottedNameParamName
)
cleanSearchParams(
searchParams,
setSearchParams,
dottedNameParamName,
Object.keys(newSituation)
)
expect(setSearchParams).toHaveBeenCalledWith('', { replace: true })
})
it("doesn't remove other search params", () => {
const searchParams = new URLSearchParams(
'rule without=123&utm_campaign=marketing'
)
const newSituation = getSituationFromSearchParams(
searchParams,
dottedNameParamName
)
cleanSearchParams(
searchParams,
setSearchParams,
dottedNameParamName,
Object.keys(newSituation)
)
expect(setSearchParams).toHaveBeenCalledWith('utm_campaign=marketing', {
replace: true,
})
})
})