Last active
January 2, 2018 20:52
-
-
Save michaelwclark/9c896e362880af85aa3a2c57be21bf8c to your computer and use it in GitHub Desktop.
testing pattern
This file contains hidden or 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
| import makeGetUser from './github' | |
| import request from './request' | |
| const getUser = makeGetUser({request}) | |
| getUser('michaelwclark') | |
| .then(console.log) |
This file contains hidden or 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
| //github.js | |
| //but lets make it liek your example | |
| const makeGetUser = | |
| services => | |
| user => | |
| services.request(`https://api.github.com/users/${user}`) | |
| export default makeGetUser |
This file contains hidden or 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
| //github.spec.js | |
| // Let's mock this bit$ch | |
| import {assert, expect} from 'chai' | |
| import makeGetUser from './github' //where did this come from? I get the exporting makeSerive but serviceMaker? | |
| describe('makeGetUser', ()=> { | |
| it('should return a function',()=>{ | |
| const mock_services = {} | |
| assert.isFunction(makeGetUser(mock_services)) | |
| })//so this calls services request but enters nothing? why the need to make mock_services then instead of | |
| //just doing makeGetUser()/makeGetUser('') | |
| //the pattern i use is that services is always an object...so even in my tests i try to be explicit on how I call makeXXXYYY... i do a lot of m.akeGetUser, make makeGetMyBeer,makeGetMeMoreMoney...always passing in a service object to the make`r so i just wanted you to see services is just an object literal. { } | |
| it('should call request', ()=>{ | |
| let requestCalled = false | |
| const mock_request = | |
| //under the covers this is what __mocks__ is doing with jest.mock... but instead of you passing | |
| //it in, jest is tricky, black magic, and intercepting the request. They are doing it via convention, | |
| //we are doing it explicitly so even if you don't know a lick of jest you can figure this code out.. | |
| //i think | |
| (user) => { | |
| requestCalled=true | |
| expect(user).to.eql('michaelwclark') | |
| return 'hi wes' | |
| } | |
| const mock_services = { request: mock_request} | |
| const getUser = makeGetUser(mock_services) | |
| const result = getUser('michaelwclark') | |
| assert.isTrue(requestCalled) | |
| expect(result).to.eql('hi wes') | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment