-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added family index request handler try/catch and unit tests.
- Loading branch information
1 parent
888bf4e
commit 5e4df5e
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { getFamilies } from '.'; | ||
import { CritterbaseService } from '../../../services/critterbase-service'; | ||
import { getRequestHandlerMocks } from '../../../__mocks__/db'; | ||
|
||
describe('getFamilies', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('gets families', async () => { | ||
const mockFamilies = ['family1', 'family2']; | ||
const mockGetFamilies = sinon.stub(CritterbaseService.prototype, 'getFamilies').resolves(mockFamilies); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = getFamilies(); | ||
|
||
await requestHandler(mockReq, mockRes, mockNext); | ||
|
||
expect(mockGetFamilies.calledOnce).to.be.true; | ||
expect(mockRes.statusValue).to.equal(200); | ||
expect(mockRes.json.calledWith(mockFamilies)).to.be.true; | ||
}); | ||
|
||
it('handles error', async () => { | ||
const mockError = new Error('mock error'); | ||
const mockGetFamilies = sinon.stub(CritterbaseService.prototype, 'getFamilies').rejects(mockError); | ||
|
||
const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); | ||
const requestHandler = getFamilies(); | ||
|
||
try { | ||
await requestHandler(mockReq, mockRes, mockNext); | ||
expect.fail(); | ||
} catch (actualError) { | ||
expect(actualError).to.equal(mockError); | ||
expect(mockGetFamilies.calledOnce).to.be.true; | ||
} | ||
}); | ||
}); |
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