2021-01-07 17:08:19 +00:00
|
|
|
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
|
|
|
|
2021-01-07 17:08:19 +00:00
|
|
|
import {
|
2022-09-05 17:16:54 +00:00
|
|
|
cleanSearchParams,
|
|
|
|
getRulesParamNames,
|
2024-02-09 11:09:08 +00:00
|
|
|
getSearchParams,
|
2022-09-15 10:31:13 +00:00
|
|
|
getSituationFromSearchParams,
|
2021-01-07 17:08:19 +00:00
|
|
|
} from '../source/components/utils/useSearchParamsSimulationSharing'
|
|
|
|
|
|
|
|
describe('identifiant court', () => {
|
2022-02-10 11:07:19 +00:00
|
|
|
const questions = Object.entries(parsePublicodes(rules).parsedRules)
|
2021-01-07 17:08:19 +00:00
|
|
|
.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(
|
2021-01-07 17:08:19 +00:00
|
|
|
new Set(questions.map(([, name]) => name)).size
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('useSearchParamsSimulationSharing', () => {
|
2022-09-05 17:16:54 +00:00
|
|
|
const engine = new Engine(
|
|
|
|
yaml.parse(`
|
2021-01-07 17:08:19 +00:00
|
|
|
rule with:
|
2022-09-05 17:16:54 +00:00
|
|
|
identifiant court: panta
|
|
|
|
formule: 0
|
2021-01-07 17:08:19 +00:00
|
|
|
rule without:
|
2022-09-05 17:16:54 +00:00
|
|
|
formule: 0
|
|
|
|
`)
|
|
|
|
)
|
2021-01-07 17:08:19 +00:00
|
|
|
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(
|
2021-01-07 17:08:19 +00:00
|
|
|
engine,
|
|
|
|
{ 'rule with': '2000€/mois', 'rule without': '1000€/mois' },
|
2024-02-09 11:09:08 +00:00
|
|
|
dottedNameParamName,
|
|
|
|
'€/an'
|
2021-01-07 17:08:19 +00:00
|
|
|
).toString()
|
2024-02-23 10:31:44 +00:00
|
|
|
).toBe(
|
2021-01-07 17:08:19 +00:00
|
|
|
new URLSearchParams(
|
2024-02-09 11:09:08 +00:00
|
|
|
'panta=2000€/mois&rule without=1000€/mois&unite=€/an'
|
2021-01-07 17:08:19 +00:00
|
|
|
).toString()
|
|
|
|
)
|
|
|
|
})
|
2022-08-02 10:17:17 +00:00
|
|
|
it('builds search params with object', () => {
|
2021-01-07 17:08:19 +00:00
|
|
|
expect(
|
2024-02-09 11:09:08 +00:00
|
|
|
getSearchParams(
|
2021-01-07 17:08:19 +00:00
|
|
|
engine,
|
|
|
|
{ 'rule without': { 1: 2, 3: { 4: '5' } } },
|
2024-02-09 11:09:08 +00:00
|
|
|
dottedNameParamName,
|
|
|
|
'€/an'
|
2021-01-07 17:08:19 +00:00
|
|
|
).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'
|
2021-01-07 17:08:19 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
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')
|
2021-01-07 17:08:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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({
|
2021-01-07 17:08:19 +00:00
|
|
|
'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({})
|
2021-01-07 17:08:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('useSearchParamsSimulationSharing hook', () => {
|
2022-09-05 17:16:54 +00:00
|
|
|
const { parsedRules } = parsePublicodes(
|
|
|
|
yaml.parse(
|
|
|
|
`
|
2021-01-07 17:08:19 +00:00
|
|
|
rule with:
|
2022-09-05 17:16:54 +00:00
|
|
|
identifiant court: panta
|
|
|
|
formule: 0
|
2021-01-07 17:08:19 +00:00
|
|
|
rule without:
|
2022-09-05 17:16:54 +00:00
|
|
|
formule: 0
|
|
|
|
`
|
|
|
|
)
|
|
|
|
)
|
2021-01-07 17:08:19 +00:00
|
|
|
|
2021-02-16 13:30:42 +00:00
|
|
|
const dottedNameParamName = getRulesParamNames(parsedRules)
|
2021-01-07 17:08:19 +00:00
|
|
|
let setSearchParams
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-12-27 15:03:49 +00:00
|
|
|
setSearchParams = vi.fn()
|
2021-01-07 17:08:19 +00:00
|
|
|
})
|
|
|
|
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'
|
|
|
|
)
|
2021-01-07 17:08:19 +00:00
|
|
|
const newSituation = getSituationFromSearchParams(
|
|
|
|
searchParams,
|
|
|
|
dottedNameParamName
|
|
|
|
)
|
|
|
|
cleanSearchParams(
|
|
|
|
searchParams,
|
|
|
|
setSearchParams,
|
|
|
|
dottedNameParamName,
|
|
|
|
Object.keys(newSituation)
|
|
|
|
)
|
2021-12-27 15:03:49 +00:00
|
|
|
expect(setSearchParams).toHaveBeenCalledWith('', { replace: true })
|
2021-01-07 17:08:19 +00:00
|
|
|
})
|
|
|
|
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)
|
|
|
|
)
|
2021-12-27 15:03:49 +00:00
|
|
|
expect(setSearchParams).toHaveBeenCalledWith('utm_campaign=marketing', {
|
|
|
|
replace: true,
|
|
|
|
})
|
2021-01-07 17:08:19 +00:00
|
|
|
})
|
|
|
|
})
|