-
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
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
scripts/actions/__tests__/check-for-outdated-translations.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,78 @@ | ||
const fs = require('fs'); | ||
const fetch = require('node-fetch'); | ||
|
||
const checkForOutdatedTranslations = require('../check-for-outdated-translations'); | ||
|
||
// Mock node-fetch so we can avoid calling out to GitHub | ||
jest.mock('node-fetch'); | ||
|
||
global.process.exit = jest.fn(); | ||
|
||
// Helper function to mock a response from Github | ||
const mockGithubResponse = (result) => { | ||
fetch.mockResolvedValueOnce({ | ||
json: jest.fn(() => Promise.resolve(result)), | ||
headers: { get: jest.fn() }, | ||
}); | ||
}; | ||
|
||
// Mock fs so we don't manipulate local files | ||
jest.mock('fs'); | ||
|
||
// helper function to construct and mock the response from readFileSync | ||
const mockReadFileSync = (translate = []) => { | ||
const mdx = `--- | ||
title: A test file | ||
${translate.length ? `translate:\n - ${translate.join('\n - ')}` : ''} | ||
--- | ||
This is a test file | ||
`; | ||
|
||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
}; | ||
|
||
const mockExistsSync = (bool) => fs.existsSync.mockReturnValueOnce(bool); | ||
|
||
const STATUS = { | ||
ADDED: 'added', | ||
MODIFIED: 'modified', | ||
REMOVED: 'removed', | ||
}; | ||
|
||
describe('Action: Check for outdated translations', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should succeed when no files are found for deletion', async () => { | ||
const filename = '/content/bar.mdx'; | ||
mockGithubResponse([ | ||
{ | ||
filename, | ||
status: STATUS.ADDED, | ||
}, | ||
]); | ||
|
||
mockReadFileSync(['jp']); | ||
mockExistsSync(false); | ||
|
||
await checkForOutdatedTranslations(); | ||
|
||
expect(global.process.exit).toHaveBeenLastCalledWith(0); | ||
}); | ||
|
||
test('should fail when files are found for deletion', async () => { | ||
const filename = '/content/bar.mdx'; | ||
mockGithubResponse([ | ||
{ | ||
filename, | ||
status: STATUS.REMOVED, | ||
}, | ||
]); | ||
|
||
mockExistsSync(true); | ||
await checkForOutdatedTranslations(); | ||
|
||
expect(global.process.exit).toHaveBeenLastCalledWith(1); | ||
}); | ||
}); |