Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
consoleData = '';
mockedFetch.mockResponses(
[
// https://api.crowdin.com/api/project/pre-translate
JSON.stringify({
success: true
}),
{ status: 200 }
]
);
await checkProgressOnBranch();
expect(consoleData.indexOf('No validation performed since it is a master branch') !== -1).toBeTruthy();
expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
});
expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
consoleData = '';
mockedFetch.mockResponses(
[
// https://api.crowdin.com/api/project/pre-translate
JSON.stringify({
success: true
}),
{ status: 200 }
]
);
await triggerAutoTranslation();
expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
});
});
}),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/delete-directory
JSON.stringify({
success: true
}),
{ status: 200 }
]
);
await deleteOldBranches();
expect(consoleData.indexOf('Branch "sample-old-branch" is removed from crowdin') !== -1).toBeTruthy();
expect(mockedFetch.mock.calls[1][0])
.toEqual('https://api.crowdin.com/api/project/my-project-name/delete-directory');
});
});
],
[
// https://api.crowdin.com/api/project/my-project-name/add-file
JSON.stringify({ success: true }),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/my-project-name/pre-translate
JSON.stringify({ success: true }),
{ status: 200 }
]
);
await uploadSources();
expect(mockedFetch.mock.calls[2][1].body._streams[7].source._readableState.buffer)
.toEqual(fs.createReadStream('test/sample-source-file/en.json')._readableState.buffer);
expect(consoleData.indexOf('Uploading to branch: feature--my-feature-branch') !== -1).toBeTruthy();
expect(consoleData.indexOf('test/sample-source-file/en.json is uploaded') !== -1).toBeTruthy();
expect(consoleData.indexOf('Triggering auto-translation of a branch: feature--my-feature-branch') !== -1).toBeTruthy();
const properApiCallsOrder = [
'https://api.crowdin.com/api/project/my-project-name/add-directory',
'https://api.crowdin.com/api/project/my-project-name/add-directory',
'https://api.crowdin.com/api/project/my-project-name/add-file',
'https://api.crowdin.com/api/project/my-project-name/pre-translate'
];
properApiCallsOrder.forEach((apiCall, index) => {
expect(mockedFetch.mock.calls[index][0]).toEqual(apiCall);
})
test('crowdinFetch calls node-fetch, sends FormData and gets sample JSON response', async () => {
const response = await crowdinFetch('export');
const firstFetchArgument = fetch.mock.calls[0][0];
const secondFetchArgument = fetch.mock.calls[0][1];
expect(firstFetchArgument).toEqual('https://api.crowdin.com/api/project/my-project-name/export');
expect(secondFetchArgument.method).toBe('POST');
expect(secondFetchArgument.body._streams).toBeDefined();
expect(secondFetchArgument.body._streams[1]).toBe('my-project-api-key');
expect(response).toEqual(sampleResponse);
});
test('CrowdinApi.buildBranch calls node-fetch, sends FormData and gets sample JSON response', async () => {
const response = await CrowdinApi.buildBranch('sample-feature-branch-name');
const firstFetchArgument = fetch.mock.calls[0][0];
const secondFetchArgument = fetch.mock.calls[0][1];
expect(firstFetchArgument).toEqual('https://api.crowdin.com/api/project/my-project-name/export');
expect(secondFetchArgument.method).toBe('POST');
expect(secondFetchArgument.body._streams).toBeDefined();
expect(secondFetchArgument.body._streams[1]).toBe('my-project-api-key');
expect(response).toEqual(sampleResponse);
});
JSON.stringify({
files: [{
node_type: 'branch',
name: 'feature--my-feature-branch',
phrases: 0
}]
}),
{ status: 200 }
]
);
await checkProgressOnBranch();
expect(consoleData.indexOf('Crowdin: Checking language: nl') !== -1).toBeTruthy();
expect(consoleData.indexOf('Okay, no new phrases in this branch') !== -1).toBeTruthy();
expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/language-status');
});
properApiCallsOrder.forEach((apiCall, index) => {
expect(mockedFetch.mock.calls[index][0]).toEqual(apiCall);
})
});
autodrain: () => null
});
return {
promise: () => Promise.resolve()
};
}
})
},
{ status: 200 }
],
);
await downloadTranslations(true);
expect(mockedFetch.mock.calls[2][1].body._streams[7].source._readableState.buffer)
.toEqual(fs.createReadStream('test/sample-source-file/en.json')._readableState.buffer);
expect(consoleData.indexOf('Uploading to branch: feature--my-feature-branch') !== -1).toBeTruthy();
expect(consoleData.indexOf('Triggering auto-translation of a branch: feature--my-feature-branch') !== -1).toBeTruthy();
expect(consoleData.indexOf('Triggering branch build before downloading') !== -1).toBeTruthy();
expect(consoleData.indexOf('Unzipped sample-translation-folder/nl.json') !== -1).toBeTruthy();
const properApiCallsOrder = [
'https://api.crowdin.com/api/project/my-project-name/add-directory',
'https://api.crowdin.com/api/project/my-project-name/add-directory',
'https://api.crowdin.com/api/project/my-project-name/add-file',
'https://api.crowdin.com/api/project/my-project-name/pre-translate',
'https://api.crowdin.com/api/project/my-project-name/export',
'https://api.crowdin.com/api/project/my-project-name/download/all.zip'
];
properApiCallsOrder.forEach((apiCall, index) => {
expect(mockedFetch.mock.calls[index][0]).toEqual(apiCall);
})
});