2020-12-01 18:10:34 +00:00
|
|
|
import { createMemoryHistory } from 'history'
|
|
|
|
import { DottedName } from 'modele-social'
|
2021-05-14 14:14:22 +00:00
|
|
|
import { createStore } from 'redux'
|
2021-12-27 15:03:49 +00:00
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
2022-11-03 16:32:04 +00:00
|
|
|
|
2023-02-23 17:49:22 +00:00
|
|
|
import { setupSimulationPersistence } from '@/storage/persistSimulation'
|
|
|
|
import * as safeLocalStorage from '@/storage/safeLocalStorage'
|
2021-05-14 14:14:22 +00:00
|
|
|
import {
|
|
|
|
loadPreviousSimulation,
|
|
|
|
setSimulationConfig,
|
|
|
|
updateSituation,
|
2023-02-23 17:49:22 +00:00
|
|
|
} from '@/store/actions/actions'
|
2020-12-01 18:10:34 +00:00
|
|
|
import reducers, {
|
|
|
|
Simulation,
|
|
|
|
SimulationConfig,
|
2023-02-23 17:49:22 +00:00
|
|
|
} from '@/store/reducers/rootReducer'
|
2020-12-01 18:10:34 +00:00
|
|
|
|
2021-05-14 14:14:22 +00:00
|
|
|
function delay(ms: number) {
|
2020-12-01 18:10:34 +00:00
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
|
|
}
|
|
|
|
|
|
|
|
const simulationConfig: SimulationConfig = {
|
2022-11-01 16:36:41 +00:00
|
|
|
'objectifs exclusifs': [],
|
2020-12-01 18:10:34 +00:00
|
|
|
objectifs: [],
|
|
|
|
situation: {},
|
|
|
|
'unité par défaut': '€/mois',
|
|
|
|
}
|
|
|
|
const initialSimulation: Simulation = {
|
|
|
|
config: simulationConfig,
|
|
|
|
url: '/someurl',
|
|
|
|
hiddenNotifications: [],
|
|
|
|
situation: {},
|
|
|
|
targetUnit: '€/mois',
|
|
|
|
foldedSteps: ['somestep' as DottedName],
|
|
|
|
unfoldedStep: null,
|
2022-09-14 12:25:58 +00:00
|
|
|
shouldFocusField: false,
|
2020-12-01 18:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('[persistence] When simulation persistence is setup', () => {
|
2021-05-14 14:14:22 +00:00
|
|
|
let store: any
|
2021-12-27 15:03:49 +00:00
|
|
|
const setItemSpy = vi.spyOn(safeLocalStorage, 'setItem')
|
2020-12-01 18:10:34 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
store = createStore(reducers, {
|
|
|
|
simulation: initialSimulation,
|
|
|
|
activeTargetInput: 'sometargetinput',
|
2021-05-14 14:14:22 +00:00
|
|
|
} as any)
|
2020-12-01 18:10:34 +00:00
|
|
|
|
|
|
|
setupSimulationPersistence(store, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the state is changed with some data that is persistable', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
store.dispatch(updateSituation('dotted name' as DottedName, '42'))
|
|
|
|
await delay(0)
|
|
|
|
})
|
|
|
|
it('saves state in localStorage with all fields', () => {
|
2021-12-27 15:03:49 +00:00
|
|
|
expect(setItemSpy).toHaveBeenCalled()
|
|
|
|
expect(setItemSpy.mock.calls[0]![1]).to.equal(
|
2020-12-01 18:10:34 +00:00
|
|
|
'{"situation":{"dotted name":"42"},"activeTargetInput":"sometargetinput","foldedSteps":["somestep"]}'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
it('saves state in localStorage with a key dependent on the simulation url', () => {
|
2021-12-27 15:03:49 +00:00
|
|
|
expect(setItemSpy).toHaveBeenCalled()
|
|
|
|
expect(setItemSpy.mock.calls[0]![0]).to.contain('someurl')
|
2020-12-01 18:10:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('[persistence] When simulation config is set', () => {
|
|
|
|
const serializedPreviousSimulation =
|
|
|
|
'{"situation":{"dotted name . other":"42"},"activeTargetInput":"someothertargetinput","foldedSteps":["someotherstep"]}'
|
|
|
|
|
2021-05-14 14:14:22 +00:00
|
|
|
let store: any
|
2020-12-01 18:10:34 +00:00
|
|
|
|
2021-12-27 15:03:49 +00:00
|
|
|
vi.spyOn(safeLocalStorage, 'getItem').mockReturnValue(
|
|
|
|
serializedPreviousSimulation
|
|
|
|
)
|
|
|
|
|
2020-12-01 18:10:34 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
store = createStore(reducers)
|
|
|
|
const history = createMemoryHistory()
|
|
|
|
history.replace('/someotherurl')
|
|
|
|
|
|
|
|
store.dispatch(
|
|
|
|
setSimulationConfig(simulationConfig, history.location.pathname)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
describe('when previous simulation is loaded in state', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
store.dispatch(loadPreviousSimulation())
|
|
|
|
})
|
|
|
|
it('loads url in state', () => {
|
|
|
|
expect(store.getState().simulation.url).to.eq('/someotherurl')
|
|
|
|
})
|
|
|
|
it('loads situation in state', () => {
|
|
|
|
expect(store.getState().simulation.situation).to.deep.eq({
|
|
|
|
'dotted name . other': '42',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('loads activeTargetInput in state', () => {
|
|
|
|
expect(store.getState().activeTargetInput).to.eq('someothertargetinput')
|
|
|
|
})
|
|
|
|
it('loads foldedSteps in state', () => {
|
|
|
|
expect(store.getState().simulation.foldedSteps).to.deep.eq([
|
|
|
|
'someotherstep',
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|