diff --git a/src/applications/representative-appoint/tests/api/fetchRepStatus.unit.spec.js b/src/applications/representative-appoint/tests/api/fetchRepStatus.unit.spec.js new file mode 100644 index 000000000000..726df18594b7 --- /dev/null +++ b/src/applications/representative-appoint/tests/api/fetchRepStatus.unit.spec.js @@ -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); + } + }); +}); diff --git a/src/applications/representative-appoint/tests/api/fetchRepresentatives.unit.spec.js b/src/applications/representative-appoint/tests/api/fetchRepresentatives.unit.spec.js new file mode 100644 index 000000000000..8214cade8f55 --- /dev/null +++ b/src/applications/representative-appoint/tests/api/fetchRepresentatives.unit.spec.js @@ -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); + } + }); +}); diff --git a/src/applications/representative-appoint/tests/api/generatePDF.unit.spec.js b/src/applications/representative-appoint/tests/api/generatePDF.unit.spec.js new file mode 100644 index 000000000000..b45710413264 --- /dev/null +++ b/src/applications/representative-appoint/tests/api/generatePDF.unit.spec.js @@ -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; + } + }); +}); diff --git a/src/applications/representative-appoint/tests/api/sendNextStepsEmail.unit.spec.js b/src/applications/representative-appoint/tests/api/sendNextStepsEmail.unit.spec.js new file mode 100644 index 000000000000..4575249214b5 --- /dev/null +++ b/src/applications/representative-appoint/tests/api/sendNextStepsEmail.unit.spec.js @@ -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); + } + }); +});