Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions translate/src/context/Editor.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ import {
import { EntityView, EntityViewProvider } from './EntityView';
import { Locale } from './Locale';
import { Location, LocationProvider } from './Location';
import { MachineryTranslations } from './MachineryTranslations';
import { UnsavedChanges, UnsavedChangesProvider } from './UnsavedChanges';
import { fluentParseEntry, mf2ParseMessage } from '@mozilla/l10n';

function mountSpy(Spy, format, formatTranslation, formatSource = 'key = test') {
function mountSpy(
Spy,
format,
formatTranslation,
formatSource = 'key = test',
machinery = null,
) {
const history = createMemoryHistory({
initialEntries: [`/sl/pro/all/?string=42`],
});
Expand Down Expand Up @@ -100,15 +107,24 @@ function mountSpy(Spy, format, formatTranslation, formatSource = 'key = test') {
};
const store = createReduxStore(initialState);

const machineryValue = {
fetching: false,
source: '',
composed: [],
translations: machinery ?? [],
};

const Wrapper = () => (
<LocationProvider history={history}>
<Locale.Provider value={{ code: 'sl', cldrPlurals: [1, 2, 3, 5] }}>
<EntityViewProvider>
<UnsavedChangesProvider>
<EditorProvider>
<Spy />
</EditorProvider>
</UnsavedChangesProvider>
<MachineryTranslations.Provider value={machineryValue}>
<UnsavedChangesProvider>
<EditorProvider>
<Spy />
</EditorProvider>
</UnsavedChangesProvider>
</MachineryTranslations.Provider>
</EntityViewProvider>
</Locale.Provider>
</LocationProvider>
Expand Down Expand Up @@ -820,4 +836,34 @@ describe('<EditorProvider>', () => {
);
expect(unsaved.check()).toBe(false);
});

it('reports no pending changes for an autofilled translation (100% TM match)', () => {
let editor, actions, unsaved;
const Spy = () => {
editor = useContext(EditorData);
actions = useContext(EditorActions);
unsaved = useContext(UnsavedChanges);
return null;
};
mountSpy(Spy, 'simple', undefined, 'message', [
{
original: 'message',
translation: 'messaggio_it',
quality: 100,
sources: ['translation-memory'],
},
]);

expect(editor).toMatchObject({
machinery: { manual: false, translation: 'messaggio_it' },
fields: [{ handle: { current: { value: 'messaggio_it' } } }],
});

act(() => actions.setResultFromInput());
expect(unsaved.check()).toBe(false);

act(() => editor.fields[0].handle.current.setValue('manual_change'));
act(() => actions.setResultFromInput());
expect(unsaved.check()).toBe(true);
});
});
28 changes: 23 additions & 5 deletions translate/src/context/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export type EditorData = Readonly<{
/** Used for detecting unsaved changes */
initial: MessageEntry;

/**
* Content filled in automatically rather than by the user.
*/
autofilled: MessageEntry | null;

machinery: {
manual: boolean;
sources: SourceType[];
Expand Down Expand Up @@ -188,6 +193,7 @@ const initEditorData: EditorData = {
base: { format: 'plain', id: '', value: [] },
focusField: { current: null },
initial: { format: 'plain', id: '', value: [] },
autofilled: null,
machinery: null,
fields: [],
sourceView: false,
Expand Down Expand Up @@ -276,6 +282,9 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
field = focusField.current = next.fields[0];
setResult(result);
}
next.autofilled = manual
? null
: buildMessageEntry(next.base, next.fields, buildOpts);
if (manual) {
field.handle.current.focus();
}
Expand All @@ -299,8 +308,12 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
? editSource(entry)
: editMessageEntry(sourceEntry, entry);
}
const state = resetFields(prev, next);
return {
...resetFields(prev, next),
...state,
autofilled: manual
? null
: buildMessageEntry(state.base, state.fields, buildOpts),
machinery: {
manual,
translation: getPlainMessage(entry),
Expand All @@ -311,7 +324,7 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {

setEditorFromHistory: (str) =>
setState((prev) => {
const next = { ...prev };
const next = { ...prev, autofilled: null };
if (specialFormats.has(format)) {
const entry = parseEntry(format, str);
if (entry) {
Expand Down Expand Up @@ -393,6 +406,7 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
fields,
focusField: { current: fields[0] },
initial: base,
autofilled: null,
machinery: null,
sourceView,
}));
Expand Down Expand Up @@ -426,12 +440,16 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
// but the latter needs to be defined at a higher level to make it
// available in `EntitiesList`. Therefore, that state is managed here.
// Let's also avoid the calculation, unless it's actually required.
const hasChanges = !pojoEquals(state.initial, result);
// Content set by autofill (100% TM match) should not trigger a warning,
// as it would be autofilled again on the next visit.
const { autofilled, initial } = state;
const hasChanges = !pojoEquals(initial, result);
if (hasChanges) {
resetFailedChecks();
}
setUnsavedChanges(() => hasChanges);
}, [result]);
const isAutofilled = !!autofilled && pojoEquals(autofilled, result);
setUnsavedChanges(() => hasChanges && !isAutofilled);
}, [result, state.autofilled]);

return (
<EditorData.Provider value={state}>
Expand Down
Loading