Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function injection
const transformData = dataArray =>{
//transforms the data in some way
}
const dataToObj = dataArray =>{
//converts the data to a new form
}
//The final object will be an object that contains all dependencies in this file.
//The functions get defaulted to the functions in the file, so it can easily be ommitted for writing non-test code
const composeData = (dataArray, {_dataToObj = dataToObj, _transformData = transformData} = {})=>{
const transformedData = transformData(dataArray);
return dataToObj(transformedData);
}
//What I like about this is that I get to worry about only compose data.
//I don't care what the other functions do. It's also really easy to test what happens under different circumstances
//Because I just state what I want the return value of each function to be.
//This also doesn't disallow more heavy integration or end to end tests. The main drawback is odd looking object at the end.
describe('composeData', ()=>{
test('composeData should transform the data and then conver it to an obj', ()=>{
const _transformData = jest.fn(()=> 'foo');
const _dataToObj = jest.fn(()=> 'bar');
const dataArray = [1,2,3];
const result = composeData(dataArray, {_transformedData, _dataToObj});
expect(_transformedData).toHaveBeenCalledWith([1,2,3]);
expect(_dataToObj).toHaveBeenCalledWith('foo');
expect(result).toBe('bar');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.