Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'jest-fetch-mock' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
test('', async () => {
mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');
const deleteOldBranches = require('../../lib/commands/delete-old-branches');
mockedChildProcess.__setResponse('git fetch', ' ');
mockedChildProcess.__setResponse('git log -1 --format=%cd origin/master', 'Fri May 4 10:40:15 2018 +0300');
mockedChildProcess.__setResponse('git ls-remote --heads', 'refs/heads/master');
mockedFetch.mockResponses(
[
// https://api.crowdin.com/api/project/info
JSON.stringify({
files: [
{
node_type: 'branch',
name: 'sample-old-branch',
files: [{
node_type: 'file',
name: 'sample-file-name.json',
last_updated: 'Fri May 4 10:40:15 2018 +0300'
}]
}
]
}),
{ status: 200 }
test('should perform upload source and then download and unzip translation', async () => {
mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');
const uploadSources = require('../../lib/commands/upload-sources');
expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
consoleData = '';
mockedFetch.mockResponses(
[
// https://api.crowdin.com/api/project/my-project-name/add-directory
JSON.stringify({ success: true }),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/my-project-name/add-directory
JSON.stringify({ success: true }),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/my-project-name/add-file
JSON.stringify({ success: true }),
{ status: 200 }
],
[
test('should perform upload source and then download and unzip translation', async () => {
mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');
const downloadTranslations = require('../../lib/commands/download-translations');
expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
consoleData = '';
mockedFetch.mockResponses(
[
// https://api.crowdin.com/api/project/my-project-name/add-directory
JSON.stringify({ success: true }),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/my-project-name/add-directory
JSON.stringify({ success: true }),
{ status: 200 }
],
[
// https://api.crowdin.com/api/project/my-project-name/add-file
JSON.stringify({ success: true }),
{ status: 200 }
],
[
test('should say "no new phrases" if phrases === 0', async () => {
mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');
const triggerAutoTranslation = require('../../lib/commands/trigger-auto-translation');
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');
});
});
test('should trigger auto translate and then exit on master branch', async () => {
mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'master');
const checkProgressOnBranch = require('../../lib/commands/check-progress-on-branch');
expect(consoleData.indexOf('Working on git branch: master') !== -1).toBeTruthy();
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');
});
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);
})
}, {
'type': 'Feature',
'id': 'osm-busstops.fid-4dec2e09_163976bf70d_-73ab',
'geometry': {
'type': 'Point',
'coordinates': [981048.0170072, 7001258.45667367]
},
'geometry_name': 'geometry',
'properties': {
'osm_id': 29077474
}
}]
};
// mock responses of WFS DescribeFeatureType and WFS GetFeature
fetch.mockResponses([describeFeatureTypeResponse], [JSON.stringify(getFeatureResponse)]);
const wfsParser = new WfsDataParser();
const resultPromise = wfsParser.readData({
url: '//ows.terrestris.de/geoserver/osm/wfs',
version: '2.0.0',
typeName: 'osm:osm-country-borders',
maxFeatures: 1
});
const onFullFilled = (result: any) => {
expect(result).toBeDefined();
expect(result.schema.type).toEqual('object');
expect(typeof result.schema.properties).toBe('object');
expect(result.schema.properties.osm_id).toEqual({
type: 'number'
});