Ensured settings migration function does not crash if settings are not set

This commit is contained in:
Alejandro Celaya 2022-01-01 09:40:26 +01:00
parent af0d2d3cdc
commit e77b4d7a82
2 changed files with 9 additions and 1 deletions

View file

@ -1,6 +1,10 @@
import { ShlinkState } from '../../container/types';
export const migrateDeprecatedSettings = (state: ShlinkState): ShlinkState => {
export const migrateDeprecatedSettings = (state: Partial<ShlinkState>): Partial<ShlinkState> => {
if (!state.settings) {
return state;
}
// The "last180Days" interval had a typo, with a lowercase d
if ((state.settings.visits?.defaultInterval as any) === 'last180days') {
state.settings.visits && (state.settings.visits.defaultInterval = 'last180Days');

View file

@ -4,6 +4,10 @@ import { ShlinkState } from '../../../src/container/types';
describe('settings-helpers', () => {
describe('migrateDeprecatedSettings', () => {
it('returns object as is if settings are not set', () => {
expect(migrateDeprecatedSettings({})).toEqual({});
});
it('updates settings as expected', () => {
const state = Mock.of<ShlinkState>({
settings: {