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
91 changes: 90 additions & 1 deletion translate/src/context/Editor.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function mountSpy(
fetching: false,
source: '',
composed: [],
translations: machinery ?? [],
translations: [],
...(Array.isArray(machinery) ? { translations: machinery } : machinery),
};

const Wrapper = () => (
Expand Down Expand Up @@ -866,4 +867,92 @@ describe('<EditorProvider>', () => {
act(() => actions.setResultFromInput());
expect(unsaved.check()).toBe(true);
});

it('reports pending changes for a manually copied translation', () => {
let editor, actions, unsaved;
const Spy = () => {
editor = useContext(EditorData);
actions = useContext(EditorActions);
unsaved = useContext(UnsavedChanges);
return null;
};
mountSpy(Spy, 'simple', undefined, 'message');

act(() =>
actions.setEditorFromHelpers(
'messaggio_it',
['translation-memory'],
true,
),
);

expect(editor.autofilled).toBeNull();
act(() => actions.setResultFromInput());
expect(unsaved.check()).toBe(true);
});

it('autofills a multi-field entry from a composed 100% match', () => {
let editor, actions, unsaved;
const Spy = () => {
editor = useContext(EditorData);
actions = useContext(EditorActions);
unsaved = useContext(UnsavedChanges);
return null;
};
mountSpy(
Spy,
'fluent',
undefined,
ftl`
key = Value
.label = Label
`,
{
composed: [
{
sources: ['translation-memory'],
quality: 100,
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
},
);

expect(editor.fields.map((f) => f.handle.current.value)).toEqual([
'Valeur',
'Étiquette',
]);

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

it('does not autofill a multi-field entry without a perfect composed match', () => {
let editor;
const Spy = () => {
editor = useContext(EditorData);
return null;
};
mountSpy(
Spy,
'fluent',
undefined,
ftl`
key = Value
.label = Label
`,
{
composed: [
{
sources: ['google-translate'],
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
},
);

expect(editor.fields.map((f) => f.handle.current.value)).toEqual(['', '']);
});
});
24 changes: 18 additions & 6 deletions translate/src/context/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,14 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
const status = useTranslationStatus(entity);
useEffect(() => {
if (
status === 'missing' &&
state.machinery === null &&
!state.sourceView &&
state.fields.length === 1 &&
state.fields[0].handle.current.value === ''
status !== 'missing' ||
state.machinery !== null ||
state.sourceView ||
state.fields.some((field) => field.handle.current.value !== '')
) {
return;
}
if (state.fields.length === 1) {
const perfect = machinery.translations.find((tx) => tx.quality === 100);
if (perfect) {
actions.setEditorFromHelpers(
Expand All @@ -432,8 +434,18 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
false,
);
}
} else if (state.fields.length > 1) {
const perfect = machinery.composed.find((tx) => tx.quality === 100);
if (perfect) {
actions.setEditorFromComposed(
perfect.value,
perfect.properties,
perfect.sources,
false,
);
}
}
}, [state, actions, status, machinery.translations]);
}, [state, actions, status, machinery.translations, machinery.composed]);

useEffect(() => {
// Changes in `result` need to be reflected in `UnsavedChanges`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,69 @@ import React from 'react';
import { render } from '@testing-library/react';
import { describe, expect, it } from 'vitest';

import { EditorData } from '~/context/Editor';
import { EditorData, EditorResult } from '~/context/Editor';
import { MockLocalizationProvider } from '~/test/utils';

import { MachinerySourceIndicator } from './MachinerySourceIndicator';

const field = (value) => ({
id: '',
name: '',
keys: [],
labels: [],
handle: { current: { value } },
});
const entry = (value) => ({ format: 'plain', id: 'key', value: [value] });

const mount = (editor) =>
const mount = ({
autofilled = entry('Bonjour'),
result = entry('Bonjour'),
sourceView = false,
}) =>
render(
<MockLocalizationProvider
resources={[
`editor-MachinerySourceIndicator--match = <stress>100%</stress> MATCH
.title = 100% Translation Memory match`,
]}
>
<EditorData.Provider
value={{
fields: [field('Bonjour')],
machinery: { manual: false, sources: [], translation: 'Bonjour' },
sourceView: false,
...editor,
}}
>
<MachinerySourceIndicator />
<EditorData.Provider value={{ autofilled, sourceView }}>
<EditorResult.Provider value={result}>
<MachinerySourceIndicator />
</EditorResult.Provider>
</EditorData.Provider>
</MockLocalizationProvider>,
);

describe('<MachinerySourceIndicator>', () => {
it('shows for content that was filled in automatically', () => {
const { container } = mount();
const { container } = mount({});

const indicator = container.querySelector('.tm-source');
expect(indicator).not.toBeNull();
expect(indicator.textContent).toContain('100%');
expect(indicator.getAttribute('title')).toBeTruthy();
});

it('shows nothing for a manual copy', () => {
const { container } = mount({
machinery: { manual: true, sources: [], translation: 'Bonjour' },
});
it('shows for a multi-field entry filled in from a composed match', () => {
const composed = {
format: 'fluent',
id: 'key',
value: ['Valeur'],
attributes: new Map([['label', ['Étiquette']]]),
};
const { container } = mount({ autofilled: composed, result: composed });

expect(container.querySelector('.tm-source')).not.toBeNull();
});

it('shows in the source view as well', () => {
const { container } = mount({ sourceView: true });

expect(container.querySelector('.tm-source')).not.toBeNull();
});

it('shows nothing when nothing was filled in', () => {
const { container } = mount({ autofilled: null });

expect(container.querySelector('.tm-source')).toBeNull();
});

it('shows nothing once the filled-in content has been edited', () => {
const { container } = mount({ fields: [field('Bonjour !')] });
const { container } = mount({ result: entry('Bonjour !') });

expect(container.querySelector('.tm-source')).toBeNull();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import { Localized } from '@fluent/react';
import React, { useContext } from 'react';

import { EditorData, EditorResult } from '~/context/Editor';
import { pojoEquals } from '~/utils/pojo';

import './MachinerySourceIndicator.css';

export function MachinerySourceIndicator() {
const { fields, machinery, sourceView } = useContext(EditorData);
// Included to re-render on input changes
useContext(EditorResult);
const { autofilled } = useContext(EditorData);
const result = useContext(EditorResult);

if (
!machinery ||
machinery.manual ||
sourceView ||
fields.length !== 1 ||
machinery.translation !== fields[0].handle.current.value
) {
if (!autofilled || !pojoEquals(autofilled, result)) {
return null;
}

Expand Down
Loading