Skip to content

Commit

Permalink
chore: fix tests by using doMock
Browse files Browse the repository at this point in the history
  • Loading branch information
rudouglas committed Nov 30, 2021
1 parent bb6c24f commit d693b35
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
74 changes: 42 additions & 32 deletions scripts/actions/__tests__/add-files-to-translation-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,23 @@ describe('add-files-to-translation-queue tests', () => {
]);
});

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',
contentType: 'landingPage',
locale: 'ja-JP',
project_id: 'HT_ID',
},
]);
});

test('Doesnt exclude any files from translation', async () => {
const files = [
{ filename: 'included/path/content/bar.mdx', locale: 'jp' },
{ filename: 'included/path/content/foo.mdx', locale: 'jp' },
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' },
{ filename: 'included/path/content/foo.mdx', locale: 'ko-KR' },
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);

expect(includedFiles).toEqual([
{ filename: 'included/path/content/bar.mdx', locale: 'jp' },
{ filename: 'included/path/content/foo.mdx', locale: 'jp' },
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' },
{ filename: 'included/path/content/foo.mdx', locale: 'ko-KR' },
]);
});

Expand All @@ -148,21 +137,28 @@ describe('add-files-to-translation-queue tests', () => {
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'kr',
locale: 'ko-KR',
},
{
filename: 'excluded/path/content/bar.mdx',
contentType: 'doc',
locale: 'jp',
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);

expect(includedFiles).toEqual([
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'kr',
locale: 'ko-KR',
},
]);
});
Expand All @@ -172,21 +168,28 @@ describe('add-files-to-translation-queue tests', () => {
{
filename: 'included/path/content/bar.mdx',
contentType: 'excludedType',
locale: 'kr',
locale: 'ko-KR',
},
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'jp',
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);

expect(includedFiles).toEqual([
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'jp',
locale: 'ja-JP',
},
]);
});
Expand All @@ -196,26 +199,33 @@ describe('add-files-to-translation-queue tests', () => {
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'kr',
locale: 'ko-KR',
},
{
filename: 'included/path/content/bar.mdx',
contentType: 'excludedType',
locale: 'jp',
locale: 'ja-JP',
},
{
filename: 'excluded/path/content/bar.mdx',
contentType: 'doc',
locale: 'jp',
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);

expect(includedFiles).toEqual([
{
filename: 'included/path/content/bar.mdx',
contentType: 'doc',
locale: 'kr',
locale: 'ko-KR',
},
]);
});
Expand Down
14 changes: 9 additions & 5 deletions scripts/actions/add-files-to-translation-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ const getExclusions = () => {
* @returns {Object[]} The files that should be included
*/
const excludeFiles = (fileData, exclusions) => {
return fileData.filter(
({ filename, locale, contentType }) =>
!exclusions.excludePath[locale]?.some((path) =>
return fileData.filter(({ filename, locale, contentType }) => {
const localeKey = Object.keys(LOCALE_IDS).find(
(localeKey) => LOCALE_IDS[localeKey] === locale
);
return (
!exclusions.excludePath[localeKey]?.some((path) =>
filename.startsWith(path)
) && !exclusions.excludeType[locale]?.some((type) => contentType === type)
);
) && !exclusions.excludeType[localeKey]?.some((type) => contentType === type)
);
});
};

/**
Expand Down

0 comments on commit d693b35

Please sign in to comment.