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

147 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'
import {
2022-09-05 17:16:54 +00:00
cleanSearchParams,
getRulesParamNames,
getSearchParamsFromSituation,
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', () => {
expect(questions.length).to.greaterThan(0)
expect(questions.length).to.eq(
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(
getSearchParamsFromSituation(
engine,
{ 'rule with': '2000€/mois', 'rule without': '1000€/mois' },
dottedNameParamName
).toString()
).to.equal(
new URLSearchParams(
'panta=2000€/mois&rule without=1000€/mois'
).toString()
)
})
2022-08-02 10:17:17 +00:00
it('builds search params with object', () => {
expect(
getSearchParamsFromSituation(
engine,
{ 'rule without': { 1: 2, 3: { 4: '5' } } },
dottedNameParamName
).toString()
).to.equal(
new URLSearchParams('rule without={"1":2,"3":{"4":"5"}}').toString()
)
})
it('handles empty situation with proper defaults', () => {
expect(
getSearchParamsFromSituation(engine, {}, dottedNameParamName).toString()
).to.equal('')
})
})
describe('getSituationFromSearchParams', () => {
it('reads search params with and without identifiant court', () => {
expect(
getSituationFromSearchParams(
new URLSearchParams('panta=2000€/mois&rule without=1000€/mois'),
dottedNameParamName
)
).to.deep.equal({
'rule with': '2000€/mois',
'rule without': '1000€/mois',
})
})
it('handles empty search params with proper defaults', () => {
expect(
getSituationFromSearchParams(
new URLSearchParams(''),
dottedNameParamName
)
).to.deep.equal({})
})
})
})
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', () => {
const searchParams = new URLSearchParams('panta=123&rule without=333')
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,
})
})
})