Skip to content

Commit

Permalink
DEV test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Feb 16, 2024
1 parent 7da1360 commit 9456912
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,86 @@ exports[`ServiceConfig should handle cancelling service calls: cancelled request
]
`;

exports[`ServiceConfig should handle polling service call errors: location error 1`] = `
[
[
[Error: location string error],
],
]
`;

exports[`ServiceConfig should handle polling service call errors: status error 1`] = `
[
[
[Error: status error],
],
[
[Error: status error],
],
[
[Error: status error],
],
]
`;

exports[`ServiceConfig should handle polling service call errors: status error polling 1`] = `
{
"status": [
{
"count": 0,
"error": {
"data": "error",
"pollConfig": {
"__retryCount": 1,
"location": "/pollError",
"pollInterval": 1,
"status": [Function],
"validate": [Function],
},
},
"success": undefined,
},
],
}
`;

exports[`ServiceConfig should handle polling service call errors: status of a status error 1`] = `
[
[
[Error: status error],
],
]
`;

exports[`ServiceConfig should handle polling service call errors: status of a status error polling 1`] = `
{
"status": [
{
"count": 0,
"error": {
"data": "error",
"pollConfig": {
"__retryCount": 1,
"location": "/pollError",
"pollInterval": 1,
"status": [Function],
"validate": [Function],
},
},
"success": undefined,
},
],
}
`;

exports[`ServiceConfig should handle polling service call errors: validation error 1`] = `
[
[
[Error: basic validation error],
],
]
`;

exports[`ServiceConfig should handle polling service calls: basic polling validator 1`] = `
{
"output": {
Expand Down
133 changes: 133 additions & 0 deletions src/services/common/__tests__/serviceConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('ServiceConfig', () => {

afterAll(() => {
moxios.uninstall();
jest.clearAllMocks();
});

it('should have specific properties and methods', () => {
Expand Down Expand Up @@ -352,6 +353,138 @@ describe('ServiceConfig', () => {
}).toMatchSnapshot('custom location');
});

it('should handle polling service call errors', async () => {
// const consoleSpyLog = jest.spyOn(console, 'log');

Check warning on line 357 in src/services/common/__tests__/serviceConfig.test.js

View workflow job for this annotation

GitHub Actions / Integration-checks (18.x)

Expected a block comment instead of consecutive line comments

Check warning on line 357 in src/services/common/__tests__/serviceConfig.test.js

View workflow job for this annotation

GitHub Actions / Integration-checks (20.x)

Expected a block comment instead of consecutive line comments
// const consoleSpyWarn = jest.spyOn(console, 'warn');
const consoleSpyError = jest.spyOn(console, 'error');

await serviceConfig.axiosServiceCall(
{
cache: false,
url: '/test/',
poll: () => {
throw new Error('basic validation error');
}
},
{ pollInterval: 1 }
);
expect(consoleSpyError.mock.calls).toMatchSnapshot('validation error');
consoleSpyError.mockClear();

await serviceConfig.axiosServiceCall(
{
cache: false,
url: '/test/',
poll: {
validate: (response, count) => count === 1,
location: () => {
throw new Error('location string error');
}
}
},
{ pollInterval: 1 }
);

// delay to give the internal status promise time to unwrap
await new Promise(resolve => {
setTimeout(() => resolve(), 25);
});

expect(consoleSpyError.mock.calls).toMatchSnapshot('location error');
consoleSpyError.mockClear();

const statusErrorPoll = jest.fn();
await serviceConfig.axiosServiceCall(
{
cache: false,
url: '/test/',
poll: {
location: '/pollError',
validate: (response, count) => count === 5,
status: (success, err, count) => {
statusErrorPoll(success, err, count);
}
}
},
{ pollInterval: 1 }
);

// delay to give the internal status promise time to unwrap
await new Promise(resolve => {
setTimeout(() => resolve(), 50);
});

expect(statusErrorPoll).toHaveBeenCalledTimes(1);
expect({
status: statusErrorPoll.mock.calls.map(([success, err, count]) => ({
error: {
data: err.response.data,
pollConfig: err.config.poll
},
success,
count
}))
}).toMatchSnapshot('status error polling');

await serviceConfig.axiosServiceCall(
{
cache: false,
url: '/test/',
poll: {
validate: (response, count) => count === 3,
status: () => {
throw new Error('status error');
}
}
},
{ pollInterval: 1 }
);

// delay to give the internal status promise time to unwrap
await new Promise(resolve => {
setTimeout(() => resolve(), 25);
});

expect(consoleSpyError.mock.calls).toMatchSnapshot('status error');
consoleSpyError.mockClear();

const statusStatusErrorPoll = jest.fn();
await serviceConfig.axiosServiceCall(
{
cache: false,
url: '/test/',
poll: {
location: '/pollError',
validate: (response, count) => count === 5,
status: (success, err, count) => {
statusStatusErrorPoll(success, err, count);
throw new Error('status error');
}
}
},
{ pollInterval: 1 }
);

// delay to give the internal status promise time to unwrap
await new Promise(resolve => {
setTimeout(() => resolve(), 25);
});

expect(statusStatusErrorPoll).toHaveBeenCalledTimes(1);
expect({
status: statusStatusErrorPoll.mock.calls.map(([success, err, count]) => ({
error: {
data: err.response.data,
pollConfig: err.config.poll
},
success,
count
}))
}).toMatchSnapshot('status of a status error polling');
expect(consoleSpyError.mock.calls).toMatchSnapshot('status of a status error');
consoleSpyError.mockClear();
});

it('should allow passing a function and emulating a service call', async () => {
const responses = [];

Expand Down
4 changes: 3 additions & 1 deletion src/services/common/serviceConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ const axiosServiceCall = async (
pollInterval: updatedConfig.poll.pollInterval || pollInterval
};

let validated = false;
let validated;

try {
validated = await updatedPoll.validate.call(null, callbackResponse, updatedPoll.__retryCount);
} catch (err) {
console.error(err);
validated = true;
}

if (validated === true) {
Expand All @@ -274,6 +275,7 @@ const axiosServiceCall = async (
tempLocation = await tempLocation.call(null, callbackResponse, updatedPoll.__retryCount);
} catch (err) {
console.error(err);
tempLocation = updatedConfig.url;
}
}

Expand Down

0 comments on commit 9456912

Please sign in to comment.