Created
August 31, 2018 14:35
-
-
Save jesterswilde/f2b33cce25db022ce5b366d0f0a973fc to your computer and use it in GitHub Desktop.
function injection
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
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