fix: i18n duplicate fields (#580)

This commit is contained in:
Daniel Lautzenheiser
2023-02-24 08:09:06 -05:00
committed by GitHub
parent c66b7b0b27
commit 021bd72799
4 changed files with 212 additions and 95 deletions

View File

@ -2,6 +2,7 @@ import { DRAFT_CHANGE_FIELD, DRAFT_CREATE_EMPTY } from '@staticcms/core/constant
import mockEntry from '@staticcms/core/lib/test-utils/mock-data/MockEntry';
import entryDraftReducer from '../entryDraft';
import type { I18nSettings, StringOrTextField } from '@staticcms/core/interface';
import type { EntryDraftState } from '../entryDraft';
describe('entryDraft', () => {
@ -72,6 +73,119 @@ describe('entryDraft', () => {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
});
});
describe('i18n', () => {
it('duplicate values to other locales', () => {
const state = entryDraftReducer(startState, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1.path2',
field: {
widget: 'string',
name: 'stringInput',
i18n: 'duplicate',
},
value: 'newValue',
i18n: {
locales: ['en', 'fr', 'es'],
defaultLocale: 'en',
currentLocale: 'en',
},
},
});
expect(state.entry?.data).toEqual({
path1: {
path2: 'newValue',
},
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: {
path2: 'newValue',
},
},
},
es: {
data: {
path1: {
path2: 'newValue',
},
},
},
});
});
it('should duplicate values to other locales for singleton list', () => {
const field: StringOrTextField = {
widget: 'string',
name: 'stringInput',
i18n: 'duplicate',
};
const i18n: I18nSettings = {
locales: ['en', 'fr', 'es'],
defaultLocale: 'en',
currentLocale: 'en',
};
let state = entryDraftReducer(startState, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1',
field,
value: ['newValue1', 'newValue2', 'newValue3'],
i18n,
},
});
expect(state.entry?.data).toEqual({
path1: ['newValue1', 'newValue2', 'newValue3'],
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: ['newValue1', 'newValue2', 'newValue3'],
},
},
es: {
data: {
path1: ['newValue1', 'newValue2', 'newValue3'],
},
},
});
state = entryDraftReducer(state, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1.1',
field,
value: 'newValue2Updated',
i18n,
},
});
expect(state.entry?.data).toEqual({
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
},
},
es: {
data: {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
},
},
});
});
});
});
});
});

View File

@ -163,7 +163,7 @@ function entryDraftReducer(
};
if (i18n) {
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale);
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale, path);
}
const newData = get(newState.entry, dataPath) ?? {};