-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
My VA - increase unit test coverage pt 2 (#28481)
* get claims actions up to 98% test coverage * add unit tests for getAllPayments * get actions/messaging to 90% unit test coverage * cleanup * remove unused vars
- Loading branch information
Showing
9 changed files
with
675 additions
and
17 deletions.
There are no files selected for viewing
File renamed without changes.
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
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
157 changes: 157 additions & 0 deletions
157
src/applications/personalization/dashboard/tests/actions/claims.unit.spec.jsx
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,157 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
mockFetch, | ||
setFetchJSONFailure, | ||
setFetchJSONResponse, | ||
} from '@department-of-veterans-affairs/platform-testing/helpers'; | ||
|
||
import { createClaimsSuccess } from '../../mocks/claims'; | ||
import { createAppealsSuccess } from '../../mocks/appeals-success'; | ||
import { getAppealsV2, getClaims } from '../../actions/claims'; | ||
|
||
import { | ||
FETCH_CLAIMS_PENDING, | ||
FETCH_CLAIMS_ERROR, | ||
FETCH_CLAIMS_SUCCESS, | ||
} from '../../utils/claims-helpers'; | ||
import { | ||
FETCH_APPEALS_PENDING, | ||
FETCH_APPEALS_SUCCESS, | ||
USER_FORBIDDEN_ERROR, | ||
RECORD_NOT_FOUND_ERROR, | ||
VALIDATION_ERROR, | ||
BACKEND_SERVICE_ERROR, | ||
FETCH_APPEALS_ERROR, | ||
} from '../../utils/appeals-helpers'; | ||
|
||
describe('/actions/claims', () => { | ||
describe('getAppealsV2', () => { | ||
let dispatchSpy; | ||
beforeEach(() => { | ||
mockFetch(); | ||
dispatchSpy = sinon.spy(); | ||
}); | ||
it('should dispatch FETCH_APPEALS_PENDING action', () => { | ||
getAppealsV2()(dispatchSpy); | ||
expect(dispatchSpy.firstCall.args[0].type).to.equal( | ||
FETCH_APPEALS_PENDING, | ||
); | ||
}); | ||
|
||
it('should dispatch a FETCH_APPEALS_SUCCESS action', () => { | ||
setFetchJSONResponse(global.fetch.onCall(0), createAppealsSuccess()); | ||
const thunk = getAppealsV2(); | ||
|
||
const dispatch = action => { | ||
dispatchSpy(action); | ||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_APPEALS_SUCCESS, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
|
||
const appealsErrors = { | ||
403: USER_FORBIDDEN_ERROR, | ||
404: RECORD_NOT_FOUND_ERROR, | ||
422: VALIDATION_ERROR, | ||
502: BACKEND_SERVICE_ERROR, | ||
504: FETCH_APPEALS_ERROR, // works for any unspecified error code | ||
}; | ||
|
||
Object.keys(appealsErrors).forEach(code => { | ||
it(`Dispatches ${ | ||
appealsErrors[code] | ||
} when GET fails with ${code}`, done => { | ||
setFetchJSONResponse( | ||
global.fetch.onCall(0), | ||
Promise.reject({ | ||
errors: [{ status: `${code}` }], | ||
}), | ||
); | ||
const thunk = getAppealsV2(); | ||
const dispatch = sinon.spy(); | ||
thunk(dispatch) | ||
.then(() => { | ||
const action = dispatch.secondCall.args[0]; | ||
expect(action.type).to.equal(appealsErrors[code]); | ||
}) | ||
.then(done, done); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getClaims', () => { | ||
let dispatchSpy; | ||
let oldDataLayer; | ||
beforeEach(() => { | ||
mockFetch(); | ||
oldDataLayer = global.window.dataLayer; | ||
global.window.dataLayer = []; | ||
dispatchSpy = sinon.spy(); | ||
}); | ||
afterEach(() => { | ||
global.window.dataLayer = oldDataLayer; | ||
}); | ||
|
||
it('should dispatch FETCH_CLAIMS_PENDING action', () => { | ||
getClaims()(dispatchSpy); | ||
expect(dispatchSpy.firstCall.args[0].type).to.equal(FETCH_CLAIMS_PENDING); | ||
}); | ||
|
||
describe('onSuccess callback', () => { | ||
it('should dispatch a FETCH_CLAIMS_SUCCESS action and record the correct event to the data layer', () => { | ||
setFetchJSONResponse(global.fetch.onCall(0), createClaimsSuccess()); | ||
const thunk = getClaims(); | ||
|
||
const dispatch = action => { | ||
dispatchSpy(action); | ||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_CLAIMS_SUCCESS, | ||
); | ||
expect(global.window.dataLayer[0]).to.eql({ | ||
event: 'api_call', | ||
'api-name': 'GET Lighthouse claims /v0/benefits_claims', | ||
'api-status': 'successful', | ||
'api-latency-ms': 0, | ||
}); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
}); | ||
|
||
describe('onError callback', () => { | ||
it('should dispatch a FETCH_CLAIMS_ERROR action and record the correct event to the data layer', () => { | ||
const response = { | ||
errors: [ | ||
{ | ||
code: '500', | ||
status: 'some status', | ||
}, | ||
], | ||
}; | ||
setFetchJSONFailure(global.fetch.onCall(0), response); | ||
const thunk = getClaims(); | ||
|
||
const dispatch = action => { | ||
dispatchSpy(action); | ||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_CLAIMS_ERROR, | ||
); | ||
expect(global.window.dataLayer[1]).to.eql({ | ||
'error-key': undefined, | ||
}); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
}); | ||
}); | ||
}); |
178 changes: 178 additions & 0 deletions
178
src/applications/personalization/dashboard/tests/actions/messaging.unit.spec.jsx
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,178 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
mockFetch, | ||
setFetchJSONFailure, | ||
setFetchJSONResponse, | ||
} from '@department-of-veterans-affairs/platform-testing/helpers'; | ||
|
||
import { | ||
fetchUnreadMessagesCount, | ||
fetchFolder, | ||
fetchRecipients, | ||
} from '../../actions/messaging'; | ||
import { | ||
FETCH_FOLDER_FAILURE, | ||
FETCH_FOLDER_SUCCESS, | ||
LOADING_FOLDER, | ||
FETCH_RECIPIENTS_SUCCESS, | ||
FETCH_RECIPIENTS_FAILURE, | ||
LOADING_RECIPIENTS, | ||
FETCH_UNREAD_MESSAGES_COUNT_SUCCESS, | ||
FETCH_UNREAD_MESSAGES_COUNT_ERROR, | ||
LOADING_UNREAD_MESSAGES_COUNT, | ||
} from '../../utils/constants'; | ||
import { | ||
messagesSuccess, | ||
messagesError, | ||
folderSuccess, | ||
folderError, | ||
recipientSuccess, | ||
recipientError, | ||
} from '../fixtures/test-messaging-response'; | ||
|
||
describe('/actions/messaging', () => { | ||
describe('fetchUnreadMessagesCount', () => { | ||
let dispatchSpy; | ||
beforeEach(() => { | ||
mockFetch(); | ||
dispatchSpy = sinon.spy(); | ||
}); | ||
|
||
it('should dispatch loading and success actions for unread messages count', () => { | ||
const response = messagesSuccess; | ||
setFetchJSONResponse(global.fetch.onCall(0), response); | ||
const thunk = fetchUnreadMessagesCount(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.firstCall.args[0].type).to.equal( | ||
LOADING_UNREAD_MESSAGES_COUNT, | ||
); | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_UNREAD_MESSAGES_COUNT_SUCCESS, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
|
||
describe('onError callback', () => { | ||
it('should dispatch error action', () => { | ||
const response = messagesError; | ||
setFetchJSONFailure(global.fetch.onCall(0), response); | ||
const thunk = fetchUnreadMessagesCount(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_UNREAD_MESSAGES_COUNT_ERROR, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
|
||
it('should dispatch error action, even if there is no error object from response', () => { | ||
setFetchJSONFailure(global.fetch.onCall(0), ''); | ||
const thunk = fetchUnreadMessagesCount(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_UNREAD_MESSAGES_COUNT_ERROR, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('fetchFolder', () => { | ||
let dispatchSpy; | ||
beforeEach(() => { | ||
mockFetch(); | ||
dispatchSpy = sinon.spy(); | ||
}); | ||
|
||
it('should dispatch loading and success actions for fetching folders', () => { | ||
const response = folderSuccess; | ||
setFetchJSONResponse(global.fetch.onCall(0), response); | ||
const thunk = fetchFolder(0, '?page=1&per_page=999&use_cache=false'); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.firstCall.args[0].type).to.equal(LOADING_FOLDER); | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_FOLDER_SUCCESS, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
|
||
it('should dispatch error action for fetching folders', () => { | ||
const response = folderError; | ||
setFetchJSONFailure(global.fetch.onCall(0), response); | ||
const thunk = fetchFolder(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_FOLDER_FAILURE, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
}); | ||
|
||
describe('fetchRecipients', () => { | ||
let dispatchSpy; | ||
beforeEach(() => { | ||
mockFetch(); | ||
dispatchSpy = sinon.spy(); | ||
}); | ||
|
||
it('should dispatch loading and success actions for fetching folders', () => { | ||
const response = recipientSuccess; | ||
setFetchJSONResponse(global.fetch.onCall(0), response); | ||
const thunk = fetchRecipients(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.firstCall.args[0].type).to.equal( | ||
LOADING_RECIPIENTS, | ||
); | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_RECIPIENTS_SUCCESS, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
|
||
it('should dispatch error action', () => { | ||
const response = recipientError; | ||
setFetchJSONFailure(global.fetch.onCall(0), response); | ||
const thunk = fetchRecipients(); | ||
const dispatch = action => { | ||
dispatchSpy(action); | ||
|
||
if (dispatchSpy.callCount === 2) { | ||
expect(dispatchSpy.secondCall.args[0].type).to.equal( | ||
FETCH_RECIPIENTS_FAILURE, | ||
); | ||
} | ||
}; | ||
thunk(dispatch); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.