Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jan 18, 2021
1 parent 30437be commit 30c2f22
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Logger } from '../../../../../../src/core/server';
import { externalServiceMock, apiParams, serviceNowCommonFields } from './mocks';
import { externalServiceMock, apiParams, serviceNowCommonFields, serviceNowChoices } from './mocks';
import { ExternalService } from './types';
import { api } from './api';
let mockedLogger: jest.Mocked<Logger>;
Expand Down Expand Up @@ -235,4 +235,14 @@ describe('api', () => {
expect(res).toEqual(serviceNowCommonFields);
});
});

describe('getChoices', () => {
test('it returns the fields correctly', async () => {
const res = await api.getChoices({
externalService,
params: { field: 'priority' },
});
expect(res).toEqual(serviceNowChoices);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,38 @@ export const serviceNowCommonFields = [
element: 'sys_updated_by',
},
];

export const serviceNowChoices = [
{
dependent_value: '',
label: '1 - Critical',
value: '1',
},
{
dependent_value: '',
label: '2 - High',
value: '2',
},
{
dependent_value: '',
label: '3 - Moderate',
value: '3',
},
{
dependent_value: '',
label: '4 - Low',
value: '4',
},
{
dependent_value: '',
label: '5 - Planning',
value: '5',
},
];

const createMock = (): jest.Mocked<ExternalService> => {
const service = {
getChoices: jest.fn().mockImplementation(() => Promise.resolve(serviceNowChoices)),
getFields: jest.fn().mockImplementation(() => Promise.resolve(serviceNowCommonFields)),
getIncident: jest.fn().mockImplementation(() =>
Promise.resolve({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as utils from '../lib/axios_utils';
import { ExternalService } from './types';
import { Logger } from '../../../../../../src/core/server';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { serviceNowCommonFields } from './mocks';
import { serviceNowCommonFields, serviceNowChoices } from './mocks';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
Expand All @@ -32,7 +32,7 @@ const table = 'incident';
describe('ServiceNow service', () => {
let service: ExternalService;

beforeAll(() => {
beforeEach(() => {
service = createExternalService(
table,
{
Expand Down Expand Up @@ -112,6 +112,28 @@ describe('ServiceNow service', () => {
});
});

test('it should call request with correct arguments when table changes', async () => {
service = createExternalService(
'sn_si_incident',
{
config: { apiUrl: 'https://dev102283.service-now.com/' },
secrets: { username: 'admin', password: 'admin' },
},
logger
);

requestMock.mockImplementation(() => ({
data: { result: { sys_id: '1', number: 'INC01' } },
}));

await service.getIncident('1');
expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
url: 'https://dev102283.service-now.com/api/now/v2/table/sn_si_incident/1',
});
});

test('it should throw an error', async () => {
requestMock.mockImplementation(() => {
throw new Error('An error has occurred');
Expand All @@ -120,6 +142,17 @@ describe('ServiceNow service', () => {
'Unable to get incident with id 1. Error: An error has occurred'
);
});

test('it should throw an error when instance is not alive', async () => {
requestMock.mockImplementation(() => ({
status: 200,
data: {},
request: { connection: { servername: 'Developer instance' } },
}));
await expect(service.getIncident('1')).rejects.toThrow(
'There is an issue with your Service Now Instance. Please check Developer instance.'
);
});
});

describe('createIncident', () => {
Expand Down Expand Up @@ -158,6 +191,37 @@ describe('ServiceNow service', () => {
});
});

test('it should call request with correct arguments when table changes', async () => {
service = createExternalService(
'sn_si_incident',
{
config: { apiUrl: 'https://dev102283.service-now.com/' },
secrets: { username: 'admin', password: 'admin' },
},
logger
);

requestMock.mockImplementation(() => ({
data: { result: { sys_id: '1', number: 'INC01', sys_created_on: '2020-03-10 12:24:20' } },
}));

const res = await service.createIncident({
incident: { short_description: 'title', description: 'desc' },
});

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
url: 'https://dev102283.service-now.com/api/now/v2/table/sn_si_incident',
method: 'post',
data: { short_description: 'title', description: 'desc' },
});

expect(res.url).toEqual(
'https://dev102283.service-now.com/nav_to.do?uri=sn_si_incident.do?sys_id=1'
);
});

test('it should throw an error', async () => {
requestMock.mockImplementation(() => {
throw new Error('An error has occurred');
Expand All @@ -171,6 +235,17 @@ describe('ServiceNow service', () => {
'[Action][ServiceNow]: Unable to create incident. Error: An error has occurred'
);
});

test('it should throw an error when instance is not alive', async () => {
requestMock.mockImplementation(() => ({
status: 200,
data: {},
request: { connection: { servername: 'Developer instance' } },
}));
await expect(service.getIncident('1')).rejects.toThrow(
'There is an issue with your Service Now Instance. Please check Developer instance.'
);
});
});

describe('updateIncident', () => {
Expand Down Expand Up @@ -210,6 +285,37 @@ describe('ServiceNow service', () => {
});
});

test('it should call request with correct arguments when table changes', async () => {
service = createExternalService(
'sn_si_incident',
{
config: { apiUrl: 'https://dev102283.service-now.com/' },
secrets: { username: 'admin', password: 'admin' },
},
logger
);

patchMock.mockImplementation(() => ({
data: { result: { sys_id: '1', number: 'INC01', sys_updated_on: '2020-03-10 12:24:20' } },
}));

const res = await service.updateIncident({
incidentId: '1',
incident: { short_description: 'title', description: 'desc' },
});

expect(patchMock).toHaveBeenCalledWith({
axios,
logger,
url: 'https://dev102283.service-now.com/api/now/v2/table/sn_si_incident/1',
data: { short_description: 'title', description: 'desc' },
});

expect(res.url).toEqual(
'https://dev102283.service-now.com/nav_to.do?uri=sn_si_incident.do?sys_id=1'
);
});

test('it should throw an error', async () => {
patchMock.mockImplementation(() => {
throw new Error('An error has occurred');
Expand All @@ -224,6 +330,7 @@ describe('ServiceNow service', () => {
'[Action][ServiceNow]: Unable to update incident with id 1. Error: An error has occurred'
);
});

test('it creates the comment correctly', async () => {
patchMock.mockImplementation(() => ({
data: { result: { sys_id: '11', number: 'INC011', sys_updated_on: '2020-03-10 12:24:20' } },
Expand All @@ -241,6 +348,17 @@ describe('ServiceNow service', () => {
url: 'https://dev102283.service-now.com/nav_to.do?uri=incident.do?sys_id=11',
});
});

test('it should throw an error when instance is not alive', async () => {
requestMock.mockImplementation(() => ({
status: 200,
data: {},
request: { connection: { servername: 'Developer instance' } },
}));
await expect(service.getIncident('1')).rejects.toThrow(
'There is an issue with your Service Now Instance. Please check Developer instance.'
);
});
});

describe('getFields', () => {
Expand All @@ -254,9 +372,10 @@ describe('ServiceNow service', () => {
axios,
logger,
url:
'https://dev102283.service-now.com/api/now/v2/table/sys_dictionary?sysparm_query=name=task^internal_type=string&active=true&array=false&read_only=false&sysparm_fields=max_length,element,column_label,mandatory',
'https://dev102283.service-now.com/api/now/v2/table/sys_dictionary?sysparm_query=name=task^ORname=incident^internal_type=string&active=true&array=false&read_only=false&sysparm_fields=max_length,element,column_label,mandatory',
});
});

test('it returns common fields correctly', async () => {
requestMock.mockImplementation(() => ({
data: { result: serviceNowCommonFields },
Expand All @@ -265,6 +384,29 @@ describe('ServiceNow service', () => {
expect(res).toEqual(serviceNowCommonFields);
});

test('it should call request with correct arguments when table changes', async () => {
service = createExternalService(
'sn_si_incident',
{
config: { apiUrl: 'https://dev102283.service-now.com/' },
secrets: { username: 'admin', password: 'admin' },
},
logger
);

requestMock.mockImplementation(() => ({
data: { result: serviceNowCommonFields },
}));
await service.getFields();

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
url:
'https://dev102283.service-now.com/api/now/v2/table/sys_dictionary?sysparm_query=name=task^ORname=sn_si_incident^internal_type=string&active=true&array=false&read_only=false&sysparm_fields=max_length,element,column_label,mandatory',
});
});

test('it should throw an error', async () => {
requestMock.mockImplementation(() => {
throw new Error('An error has occurred');
Expand All @@ -273,5 +415,84 @@ describe('ServiceNow service', () => {
'[Action][ServiceNow]: Unable to get fields. Error: An error has occurred'
);
});

test('it should throw an error when instance is not alive', async () => {
requestMock.mockImplementation(() => ({
status: 200,
data: {},
request: { connection: { servername: 'Developer instance' } },
}));
await expect(service.getIncident('1')).rejects.toThrow(
'There is an issue with your Service Now Instance. Please check Developer instance.'
);
});
});

describe('getChoices', () => {
test('it should call request with correct arguments', async () => {
requestMock.mockImplementation(() => ({
data: { result: serviceNowChoices },
}));
await service.getChoices('priority');

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
url:
'https://dev102283.service-now.com/api/now/v2/table/sys_choice?sysparm_query=name=task^ORname=incident^element=priority&sysparm_fields=label,value,dependent_value',
});
});

test('it returns common fields correctly', async () => {
requestMock.mockImplementation(() => ({
data: { result: serviceNowChoices },
}));
const res = await service.getChoices('priority');
expect(res).toEqual(serviceNowChoices);
});

test('it should call request with correct arguments when table changes', async () => {
service = createExternalService(
'sn_si_incident',
{
config: { apiUrl: 'https://dev102283.service-now.com/' },
secrets: { username: 'admin', password: 'admin' },
},
logger
);

requestMock.mockImplementation(() => ({
data: { result: serviceNowChoices },
}));

await service.getChoices('priority');

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
url:
'https://dev102283.service-now.com/api/now/v2/table/sys_choice?sysparm_query=name=task^ORname=sn_si_incident^element=priority&sysparm_fields=label,value,dependent_value',
});
});

test('it should throw an error', async () => {
requestMock.mockImplementation(() => {
throw new Error('An error has occurred');
});
await expect(service.getChoices('priority')).rejects.toThrow(
'[Action][ServiceNow]: Unable to get choices. Error: An error has occurred'
);
});

test('it should throw an error when instance is not alive', async () => {
requestMock.mockImplementation(() => ({
status: 200,
data: {},
request: { connection: { servername: 'Developer instance' } },
}));
await expect(service.getIncident('1')).rejects.toThrow(
'There is an issue with your Service Now Instance. Please check Developer instance.'
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const createExternalService = (
const checkInstance = (res: AxiosResponse) => {
if (res.status === 200 && res.data.result == null) {
throw new Error(
`There is an issue with your Service Now Instance. Please check ${res.request.connection.servername}`
`There is an issue with your Service Now Instance. Please check ${
res.request?.connection?.servername ?? ''
}.`
);
}
};
Expand Down

0 comments on commit 30c2f22

Please sign in to comment.