-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
285 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
process.env.HUMAN_TRANSLATION_PROJECT_ID = 'HT_ID'; | ||
process.env.MACHINE_TRANSLATION_PROJECT_ID = 'MT_ID'; | ||
process.env.DB_CONNECTION_INFO = '{}'; | ||
|
||
global.___loader = { | ||
enqueue: jest.fn(), | ||
}; |
221 changes: 221 additions & 0 deletions
221
scripts/actions/__tests__/add-files-to-translation-queue.test.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,221 @@ | ||
'use strict'; | ||
const { | ||
describe, | ||
expect, | ||
test, | ||
beforeEach, | ||
afterEach, | ||
} = require('@jest/globals'); | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const frontmatter = require('@github-docs/frontmatter'); | ||
const { | ||
getLocalizedFileData, | ||
excludeFiles, | ||
} = require('../add-files-to-translation-queue'); | ||
|
||
const MOCK_CONSTANTS = { | ||
LOCALE_IDS: { | ||
jp: 'ja-JP', | ||
kr: 'ko-KR', | ||
}, | ||
}; | ||
|
||
jest.mock('fs'); | ||
jest.mock('path'); | ||
jest.mock('@github-docs/frontmatter'); | ||
jest.mock('../translation_workflow/database'); | ||
jest.mock('../utils/vendor-request'); | ||
|
||
const mockMdx = (translate = []) => { | ||
return `--- | ||
title: A test file | ||
${translate.length ? `translate:\n - ${translate.join('\n - ')}` : ''} | ||
--- | ||
This is a test file | ||
`; | ||
}; | ||
|
||
const EXCLUSIONS = { | ||
excludePath: { 'ja-JP': ['excluded/path'] }, | ||
excludeType: { 'ja-JP': ['excludedType'] }, | ||
}; | ||
|
||
const mockReadFileSync = (translate = []) => { | ||
const mdx = mockMdx(translate); | ||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
}; | ||
|
||
describe('add-files-to-translation-queue tests', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
describe('Queue translations', () => { | ||
test('Adds the Human Translation Project Id for locale under `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
mockReadFileSync(['jp']); | ||
frontmatter.mockReturnValueOnce({ data: { translate: ['jp'] } }); | ||
const toBeTranslated = getLocalizedFileData(file); | ||
|
||
expect(toBeTranslated).toEqual([ | ||
{ filename: '/content/bar.mdx', locale: 'ja-JP', project_id: 'HT_ID' }, | ||
]); | ||
}); | ||
|
||
test('Adds the Machine Translation Project Id when there is no `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
mockReadFileSync(); | ||
frontmatter.mockReturnValueOnce({ data: {} }); | ||
const toBeTranslated = getLocalizedFileData(file); | ||
|
||
expect(toBeTranslated).toEqual([ | ||
{ filename: '/content/bar.mdx', locale: 'ja-JP', project_id: 'MT_ID' }, | ||
]); | ||
}); | ||
|
||
test('Adds the relevant Project Id when there are multiple locales and only one `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
const originalAdd = jest.requireActual('../utils/constants.js'); | ||
|
||
jest.doMock('../utils/constants.js', () => { | ||
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS }; | ||
}); | ||
|
||
const { | ||
getLocalizedFileData, | ||
} = require('../add-files-to-translation-queue'); | ||
const fs = require('fs'); | ||
const frontmatter = require('@github-docs/frontmatter'); | ||
|
||
jest.doMock('fs'); | ||
jest.doMock('@github-docs/frontmatter'); | ||
|
||
const mdx = mockMdx(['jp']); | ||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
frontmatter.mockReturnValueOnce({ data: { translate: ['jp'] } }); | ||
|
||
const toBeTranslated = getLocalizedFileData(file); | ||
expect(toBeTranslated).toEqual([ | ||
{ | ||
filename: '/content/bar.mdx', | ||
locale: 'ja-JP', | ||
project_id: 'HT_ID', | ||
}, | ||
{ | ||
filename: '/content/bar.mdx', | ||
locale: 'ko-KR', | ||
project_id: 'MT_ID', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Adds the relevant Project Id when there are multiple locales and only one `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
mockReadFileSync(['jp']); | ||
frontmatter.mockReturnValueOnce({ | ||
data: { type: 'landingPage', translate: ['jp'] }, | ||
}); | ||
const toBeTranslated = getLocalizedFileData(file); | ||
|
||
expect(toBeTranslated).toEqual([ | ||
{ | ||
filename: '/content/bar.mdx', | ||
fileType: 'landingPage', | ||
locale: 'ja-JP', | ||
project_id: 'HT_ID', | ||
}, | ||
]); | ||
}); | ||
test('Doesnt exclude any files', async () => { | ||
const files = [ | ||
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' }, | ||
{ filename: 'included/path/content/foo.mdx', locale: 'ja-JP' }, | ||
]; | ||
const includedFiles = excludeFiles(files, EXCLUSIONS); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' }, | ||
{ filename: 'included/path/content/foo.mdx', locale: 'ja-JP' }, | ||
]); | ||
}); | ||
test('Excludes files under a set path', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
{ | ||
filename: 'excluded/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
const includedFiles = excludeFiles(files, EXCLUSIONS); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Excludes specific file types', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'excludedType', | ||
locale: 'ja-JP', | ||
}, | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
const includedFiles = excludeFiles(files, EXCLUSIONS); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Excludes files and file types under a set path', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'excludedType', | ||
locale: 'ja-JP', | ||
}, | ||
{ | ||
filename: 'excluded/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
const includedFiles = excludeFiles(files, EXCLUSIONS); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
fileType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const LOCALE_IDS = { | ||
jp: 'ja-JP', | ||
}; | ||
|
||
const EXCLUSIONS_FILE = 'scripts/utils/docs-content-tools/i18n-exclusions.yml'; | ||
|
||
module.exports = { | ||
LOCALE_IDS, | ||
EXCLUSIONS_FILE, | ||
}; |
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,6 @@ | ||
excludePath: | ||
ja-JP: | ||
- /whats-new | ||
excludeType: | ||
ja-JP: | ||
- landingPage |