Skip to content

Commit

Permalink
[97180] Add unit tests for api files (#33437)
Browse files Browse the repository at this point in the history
* add api unit tests

* remove duplicate fixture
  • Loading branch information
jvcAdHoc authored Dec 12, 2024
1 parent c3052a7 commit 178d0f4
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
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);
}
});
});
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);
}
});
});
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;
}
});
});
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);
}
});
});

0 comments on commit 178d0f4

Please sign in to comment.