-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow translating previously untranslated strings in React editor (ZN…
…TA-1767) (#227) * fix(ZNTA-1767): allow adding new translations in React editor * test(editor utils): increase to 100% unit test coverage of util module * test(editor utils): increase to 100% coverage for phrase utils module
- Loading branch information
1 parent
7983adf
commit 0933773
Showing
5 changed files
with
309 additions
and
10 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
server/zanata-frontend/src/editor/__tests__/utils/UtilTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
jest.disableAutomock() | ||
|
||
import { | ||
parseNPlurals, | ||
prepareDocs, | ||
prepareLocales, | ||
prepareStats | ||
} from '../../app/utils/Util' | ||
|
||
|
||
describe('parseNPluralsTest', () => { | ||
it('can parse valid Plural-Forms string', () => { | ||
// Valid plural forms from | ||
// https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html | ||
expect(parseNPlurals('nplurals=2; plural=n == 1 ? 0 : 1;')).toEqual(2) | ||
expect(parseNPlurals('nplurals=1; plural=0;')).toEqual(1) | ||
expect(parseNPlurals('nplurals=2; plural=n != 1;')).toEqual(2) | ||
expect(parseNPlurals('nplurals=2; plural=n>1;')).toEqual(2) | ||
expect(parseNPlurals( | ||
'nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2;')) | ||
.toEqual(3) | ||
expect(parseNPlurals( | ||
'nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 ' + | ||
': n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;')).toEqual(6) | ||
}) | ||
|
||
it('returns undefined when plural string is null or empty', () => { | ||
expect(parseNPlurals(undefined)).toBeUndefined() | ||
expect(parseNPlurals(null)).toBeUndefined() | ||
expect(parseNPlurals('')).toBeUndefined() | ||
}) | ||
|
||
it('returns undefined for strings without valid nplurals', () => { | ||
expect(parseNPlurals('nplurals=x; plural=y;')).toBeUndefined() | ||
expect(parseNPlurals('not even a plural forms string')).toBeUndefined() | ||
expect(parseNPlurals('mplurals=7; the mumber of plurals')).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('prepareLocalesTest', () => { | ||
it('Can transform locales to the expected form', () => { | ||
// Values taken from API response. | ||
const unpreparedLocales = | ||
[ | ||
{ | ||
'localeId': 'de', | ||
'displayName': 'German', | ||
'nativeName': 'Deutsch', | ||
'enabled': true, | ||
'enabledByDefault': true, | ||
'pluralForms': 'nplurals=2; plural=(n != 1)' | ||
}, { | ||
'localeId': 'ja', | ||
'displayName': 'Japanese', | ||
'nativeName': '日本語', | ||
'enabled': true, | ||
'enabledByDefault': true, | ||
'pluralForms': 'nplurals=1; plural=0' | ||
} | ||
] | ||
|
||
const preparedLocales = { | ||
de: { | ||
id: 'de', | ||
name: 'German', | ||
nplurals: 2 | ||
}, | ||
ja: { | ||
id: 'ja', | ||
name: 'Japanese', | ||
nplurals: 1 | ||
} | ||
} | ||
|
||
expect(prepareLocales(unpreparedLocales)).toEqual(preparedLocales) | ||
}) | ||
}) | ||
|
||
|
||
describe('prepareStatsTest', () => { | ||
it('Can can translate statistics to the expected form.', () => { | ||
// Values taken from API response. | ||
const unpreparedStats = [ | ||
{ | ||
'total': 4592, | ||
'untranslated': 4592, | ||
'needReview': 0, | ||
'translated': 0, | ||
'approved': 0, | ||
'rejected': 0, | ||
'fuzzy': 0, | ||
'unit': 'WORD', | ||
'locale': 'de', | ||
'lastTranslated': null, | ||
'translatedOnly': 0 | ||
}, { | ||
'total': 495, | ||
'untranslated': 460, | ||
'needReview': 0, | ||
'translated': 15, | ||
'approved': 5, | ||
'rejected': 0, | ||
'fuzzy': 20, | ||
'unit': 'MESSAGE', | ||
'locale': 'de', | ||
'lastTranslated': null, | ||
'translatedOnly': 10 | ||
} | ||
] | ||
|
||
// just the MESSAGE stats | ||
const preparedStats = { | ||
'total': 495, | ||
'untranslated': 460, | ||
'approved': 5, | ||
'rejected': 0, | ||
'needswork': 20, | ||
'translated': 10 | ||
} | ||
|
||
expect(prepareStats(unpreparedStats)).toEqual(preparedStats) | ||
}) | ||
}) | ||
|
||
describe('prepareDocsTest', () => { | ||
it('Can handle a null document list', () => { | ||
expect(prepareDocs([])).toEqual([]) | ||
}) | ||
|
||
it('Can transform a list of documents to the expected structure.', () => { | ||
// Values taken from API response. | ||
const unpreparedDocs = [ | ||
{ | ||
'name': 'template20161102.pot', | ||
'contentType': 'application/x-gettext', | ||
'lang': 'en-US', | ||
'type': 'FILE', | ||
'revision': 1 | ||
}, { | ||
'name': 'flags/template20161102.pot', | ||
'contentType': 'application/x-gettext', | ||
'lang': 'en-US', | ||
'type': 'FILE', | ||
'revision': 1 | ||
}, { | ||
'name': 'msgctxt/template20161102.pot', | ||
'contentType': 'application/x-gettext', | ||
'lang': 'en-US', | ||
'type': 'FILE', | ||
'revision': 1 | ||
} | ||
] | ||
const preparedDocs = [ | ||
'template20161102.pot', | ||
'flags/template20161102.pot', | ||
'msgctxt/template20161102.pot' | ||
] | ||
expect(prepareDocs(unpreparedDocs)).toEqual(preparedDocs) | ||
}) | ||
}) |
101 changes: 101 additions & 0 deletions
101
server/zanata-frontend/src/editor/__tests__/utils/phraseTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
jest.disableAutomock() | ||
|
||
import { | ||
getSaveButtonStatus, | ||
} from '../../app/utils/phrase' | ||
import { | ||
STATUS_UNTRANSLATED, | ||
STATUS_NEEDS_WORK, | ||
STATUS_TRANSLATED | ||
} from '../../app/utils/status' | ||
|
||
describe('getSaveButtonStatusTest', () => { | ||
it('Returns UNTRANSLATED when nothing is translated', () => { | ||
const phrase1 = { | ||
newTranslations: [] | ||
} | ||
|
||
const phrase2 = { | ||
newTranslations: [ | ||
'', | ||
'' | ||
] | ||
} | ||
|
||
expect(getSaveButtonStatus(phrase1)).toEqual(STATUS_UNTRANSLATED) | ||
expect(getSaveButtonStatus(phrase2)).toEqual(STATUS_UNTRANSLATED) | ||
}) | ||
|
||
it('Returns NEEDS_WORK when some but not all translations are empty', () => { | ||
const phrase1 = { | ||
newTranslations: [ | ||
'', | ||
'Hello' | ||
] | ||
} | ||
const phrase2 = { | ||
newTranslations: [ | ||
'Hi', | ||
'', | ||
'Hello' | ||
] | ||
} | ||
|
||
expect(getSaveButtonStatus(phrase1)).toEqual(STATUS_NEEDS_WORK) | ||
expect(getSaveButtonStatus(phrase2)).toEqual(STATUS_NEEDS_WORK) | ||
}) | ||
|
||
it('Returns TRANSLATED when all translated and something changed', () => { | ||
const phrase1 = { | ||
translations: [ | ||
'Hi', | ||
'' | ||
], | ||
newTranslations: [ | ||
'Hi', | ||
'Hello' | ||
] | ||
} | ||
const phrase2 = { | ||
translations: [ | ||
'Hi', | ||
'Hello' | ||
], | ||
newTranslations: [ | ||
'Hi', | ||
'Haldo' | ||
] | ||
} | ||
|
||
expect(getSaveButtonStatus(phrase1)).toEqual(STATUS_TRANSLATED) | ||
expect(getSaveButtonStatus(phrase2)).toEqual(STATUS_TRANSLATED) | ||
}) | ||
|
||
it('Returns the current status when nothing has changed', () => { | ||
const phrase1 = { | ||
status: STATUS_NEEDS_WORK, | ||
translations: [ | ||
'Hi', | ||
'Hello' | ||
], | ||
newTranslations: [ | ||
'Hi', | ||
'Hello' | ||
] | ||
} | ||
const phrase2 = { | ||
status: STATUS_TRANSLATED, | ||
translations: [ | ||
'Hi', | ||
'Hello' | ||
], | ||
newTranslations: [ | ||
'Hi', | ||
'Hello' | ||
] | ||
} | ||
|
||
expect(getSaveButtonStatus(phrase1)).toEqual(STATUS_NEEDS_WORK) | ||
expect(getSaveButtonStatus(phrase2)).toEqual(STATUS_TRANSLATED) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters