Created
February 29, 2020 06:39
-
-
Save Farenheith/54a0571a1a53397af8c7db0f001f8927 to your computer and use it in GitHub Desktop.
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
import { expect } from 'chai'; | |
import { stub, match, SinonStub } from 'sinon'; | |
import request = require('superagent'); | |
import { superagentExample } from '../src/superagent-example'; | |
describe('superAgentExample()', () => { | |
let send: SinonStub; | |
let set1: SinonStub; | |
let set2: SinonStub; | |
beforeEach(() => { | |
set2 = stub().returns(Promise.resolve({ body: 'expected body' })); | |
set1 = stub().returns({ set: set2 }); | |
send = stub().returns({ set: set1 }) | |
stub(request, 'post').returns({ send } as any); | |
stub(console, 'log'); | |
}); | |
it('should run request and log result', async () => { | |
const result = await superagentExample(); | |
expect(request.post).to.have.been.calledOnceWithExactly('/api/pet'); | |
expect(send).to.have.been.calledOnceWithExactly(match.object); | |
expect(set1).to.have.been.calledOnceWithExactly('X-API-Key', 'foobar') | |
expect(set2).to.have.been.calledOnceWithExactly('Accept', 'application/json'); | |
expect(console.log).to.have.been.calledOnceWithExactly('yay got "expected body"'); | |
expect(result).to.be.undefined; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment