-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[97180] Add unit tests for api files (#33437)
* add api unit tests * remove duplicate fixture
- Loading branch information
Showing
4 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/applications/representative-appoint/tests/api/fetchRepStatus.unit.spec.js
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,69 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import environment from '@department-of-veterans-affairs/platform-utilities/environment'; | ||
import fetchRepStatus from '../../api/fetchRepStatus'; | ||
|
||
describe('fetchRepStatus', () => { | ||
let sandbox; | ||
let fetchStub; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
global.fetch = sandbox.stub(global, 'fetch'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should return representative status successfully', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
statusText: 'OK', | ||
json: () => Promise.resolve({ data: 'Mocked Data' }), | ||
}; | ||
fetchStub = fetch.resolves(mockResponse); | ||
|
||
const result = await fetchRepStatus(); | ||
|
||
const expectedUrl = `${ | ||
environment.API_URL | ||
}/representation_management/v0/power_of_attorney`; | ||
sinon.assert.calledWith(fetchStub, expectedUrl, { | ||
'Content-Type': 'application/json', | ||
mode: 'cors', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Key-Inflection': 'camel', | ||
}, | ||
}); | ||
expect(result).to.have.nested.property('data', 'Mocked Data'); | ||
}); | ||
|
||
it('should throw an error if the response is not ok', async () => { | ||
const mockErrorResponse = { | ||
ok: false, | ||
statusText: 'Internal Server Error', | ||
json: () => Promise.resolve({ error: 'Some error' }), | ||
}; | ||
fetchStub = fetch.resolves(mockErrorResponse); | ||
|
||
try { | ||
await fetchRepStatus(); | ||
} catch (error) { | ||
expect(error.message).to.equal('Internal Server Error'); | ||
} | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const mockError = new Error('Network error'); | ||
fetchStub = fetch.rejects(mockError); | ||
|
||
try { | ||
await fetchRepStatus(); | ||
} catch (error) { | ||
expect(error).to.equal(mockError); | ||
} | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
src/applications/representative-appoint/tests/api/fetchRepresentatives.unit.spec.js
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,76 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import environment from '@department-of-veterans-affairs/platform-utilities/environment'; | ||
import { fetchRepresentatives } from '../../api/fetchRepresentatives'; | ||
import repResults from '../fixtures/data/representative-results.json'; | ||
|
||
describe('fetchRepresentatives', () => { | ||
let sandbox; | ||
let fetchStub; | ||
const query = 'Bob'; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
global.fetch = sandbox.stub(global, 'fetch'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should call the api with the query param', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
statusText: 'OK', | ||
json: () => Promise.resolve({ repResults }), | ||
}; | ||
fetchStub = fetch.resolves(mockResponse); | ||
|
||
await fetchRepresentatives({ query }); | ||
|
||
const expectedUrl = `${ | ||
environment.API_URL | ||
}/representation_management/v0/original_entities?query=${query}`; | ||
|
||
sinon.assert.calledWith(fetchStub, expectedUrl); | ||
}); | ||
|
||
it('returns a list of representatives', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
statusText: 'OK', | ||
json: () => Promise.resolve({ repResults }), | ||
}; | ||
fetchStub = fetch.resolves(mockResponse); | ||
|
||
const result = await fetchRepresentatives({ query }); | ||
|
||
expect(result.repResults).to.deep.equal(repResults); | ||
}); | ||
|
||
it('should throw an error if the response is not ok', async () => { | ||
const mockErrorResponse = { | ||
ok: false, | ||
statusText: 'Internal Server Error', | ||
json: () => Promise.resolve({ error: 'Some error' }), | ||
}; | ||
fetchStub = fetch.resolves(mockErrorResponse); | ||
|
||
try { | ||
await fetchRepresentatives({ query }); | ||
} catch (error) { | ||
expect(error.message).to.equal('Internal Server Error'); | ||
} | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const mockError = new Error('Network error'); | ||
fetchStub = fetch.rejects(mockError); | ||
|
||
try { | ||
await fetchRepresentatives({ query }); | ||
} catch (error) { | ||
expect(error).to.equal(mockError); | ||
} | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
src/applications/representative-appoint/tests/api/generatePDF.unit.spec.js
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,58 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { mockApiRequest } from 'platform/testing/unit/helpers'; | ||
import { generatePDF } from '../../api/generatePDF'; | ||
import formData from '../fixtures/data/form-data.json'; | ||
import formData2122a from '../fixtures/data/21-22a/form-data.json'; | ||
|
||
describe('generatePDF', () => { | ||
context('when submitting Form 21-22', () => { | ||
it('sets the pdfUrl in local storage', async () => { | ||
expect(localStorage.getItem('pdfUrl')).to.be.null; | ||
|
||
mockApiRequest({ | ||
blob: () => new Blob(['my blob'], { type: 'application/pdf' }), | ||
}); | ||
|
||
const createObjectURL = sinon | ||
.stub(URL, 'createObjectURL') | ||
.returns('my_stubbed_url.com'); | ||
|
||
await generatePDF(formData); | ||
|
||
expect(localStorage.getItem('pdfUrl')).to.eql('my_stubbed_url.com'); | ||
|
||
createObjectURL.restore(); | ||
}); | ||
}); | ||
|
||
context('when submitting Form 21-22a', () => { | ||
it('sets the pdfUrl in local storage', async () => { | ||
expect(localStorage.getItem('pdfUrl')).to.be.null; | ||
|
||
mockApiRequest({ | ||
blob: () => new Blob(['my blob'], { type: 'application/pdf' }), | ||
}); | ||
|
||
const createObjectURL = sinon | ||
.stub(URL, 'createObjectURL') | ||
.returns('my_stubbed_url.com'); | ||
|
||
await generatePDF(formData2122a); | ||
|
||
expect(localStorage.getItem('pdfUrl')).to.eql('my_stubbed_url.com'); | ||
|
||
createObjectURL.restore(); | ||
}); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
mockApiRequest(); | ||
|
||
try { | ||
await generatePDF(formData); | ||
} catch (error) { | ||
expect(error).to.not.be.null; | ||
} | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
src/applications/representative-appoint/tests/api/sendNextStepsEmail.unit.spec.js
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,97 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import environment from '@department-of-veterans-affairs/platform-utilities/environment'; | ||
import sendNextStepsEmail from '../../api/sendNextStepsEmail'; | ||
|
||
describe('sendNextStepsEmail', () => { | ||
let sandbox; | ||
let fetchStub; | ||
const body = { | ||
formNumber: '21-22', | ||
formName: | ||
"Appointment of Veterans Service Organization as Claimant's Representative", | ||
firstName: 'Bob', | ||
emailAddress: 'test@test,com', | ||
entityId: '12345', | ||
entityType: 'organization', | ||
}; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
global.fetch = sandbox.stub(global, 'fetch'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should call the api with the provided body', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
statusText: 'OK', | ||
json: () => Promise.resolve({ message: 'email enqueued' }), | ||
}; | ||
fetchStub = fetch.resolves(mockResponse); | ||
|
||
await sendNextStepsEmail(body); | ||
|
||
const expectedUrl = `${ | ||
environment.API_URL | ||
}/representation_management/v0/next_steps_email`; | ||
|
||
sinon.assert.calledWith(fetchStub, expectedUrl, { | ||
body: JSON.stringify(body), | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Sec-Fetch-Mode': 'cors', | ||
'Source-App-Name': 'appoint-a-representative', | ||
'X-CSRF-Token': null, | ||
'X-Key-Inflection': 'camel', | ||
}, | ||
method: 'POST', | ||
mode: 'cors', | ||
}); | ||
}); | ||
|
||
it('returns the api response as json', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
statusText: 'OK', | ||
json: () => Promise.resolve({ message: 'email enqueued' }), | ||
}; | ||
fetchStub = fetch.resolves(mockResponse); | ||
|
||
const result = await sendNextStepsEmail(body); | ||
|
||
expect(result).to.eql({ message: 'email enqueued' }); | ||
}); | ||
|
||
it('should throw an error if the response is not ok', async () => { | ||
const mockErrorResponse = { | ||
ok: false, | ||
statusText: 'Internal Server Error', | ||
json: () => Promise.resolve({ error: 'Some error' }), | ||
}; | ||
fetchStub = fetch.resolves(mockErrorResponse); | ||
const expectedError = | ||
'Error on API request to https://dev-api.va.gov/representation_management/v0/next_steps_email: Internal Server Error. Some error'; | ||
|
||
try { | ||
await sendNextStepsEmail(body); | ||
} catch (error) { | ||
expect(error.message).to.equal(expectedError); | ||
} | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const mockError = new Error('Network error'); | ||
fetchStub = fetch.rejects(mockError); | ||
|
||
try { | ||
await sendNextStepsEmail(body); | ||
} catch (error) { | ||
expect(error).to.equal(mockError); | ||
} | ||
}); | ||
}); |