Skip to content

Commit

Permalink
My VA - increase unit test coverage pt 2 (#28481)
Browse files Browse the repository at this point in the history
* 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
allisonlu authored and Peter Hill committed Mar 14, 2024
1 parent b465a38 commit a000243
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 17 deletions.
12 changes: 0 additions & 12 deletions src/applications/personalization/dashboard/actions/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import {
} from '../utils/helpers';
import environment from '~/platform/utilities/environment';
import { apiRequest } from '~/platform/utilities/api';
import { mockFolderResponse } from '~/applications/personalization/dashboard/utils/mocks/messaging/folder';
import { mockMessagesResponse } from '~/applications/personalization/dashboard/utils/mocks/messaging/messages';
import { shouldMockApiRequest } from '~/applications/personalization/dashboard/tests/helpers';
import {
FETCH_FOLDER_FAILURE,
FETCH_FOLDER_SUCCESS,
Expand Down Expand Up @@ -56,15 +53,6 @@ export const fetchFolder = (id, query = {}) => async dispatch => {
const folderUrl = `/folders/${id}`;
const messagesUrl = createUrlWithQuery(`${folderUrl}/messages`, query);

if (shouldMockApiRequest()) {
dispatch({
type: FETCH_FOLDER_SUCCESS,
folder: mockFolderResponse,
messages: mockMessagesResponse,
});
return;
}

try {
const folderResponse = await apiRequest(`${baseUrl}${folderUrl}`);
const messagesResponse = await apiRequest(`${baseUrl}${messagesUrl}`);
Expand Down
19 changes: 19 additions & 0 deletions src/applications/personalization/dashboard/mocks/claims/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ const createClaimsSuccess = (updatedDaysAgo = 1, open = true) => {
const formattedDaysAgo = format(daysAgo, 'yyyy-MM-dd');
return {
data: [
{
id: '333333',
type: 'claim',
attributes: {
claimDate: '2023-10-18',
claimPhaseDates: {
phaseChangeDate: formattedDaysAgo,
},
claimType: 'Compensation',
closeDate: open ? null : '2023-10-31',
decisionLetterSent: false,
developmentLetterSent: false,
documentsNeeded: true,
endProductCode: '404',
evidenceWaiverSubmitted5103: false,
lighthouseId: 266374,
status: open ? 'EVIDENCE_GATHERING_REVIEW_DECISION' : 'COMPLETE',
},
},
{
id: '266374',
type: 'claim',
Expand Down
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);
});
});
});
});
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);
});
});
});

0 comments on commit a000243

Please sign in to comment.