Skip to content

Commit

Permalink
✨ Add sdk-utils waitForPercyIdle helper (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman authored Mar 17, 2022
1 parent d1668f0 commit 1c85c52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/sdk-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import logger from '@percy/logger';
import percy from './percy-info';
import request from './request';
import isPercyEnabled from './percy-enabled';
import waitForPercyIdle from './percy-idle';
import fetchPercyDOM from './percy-dom';
import postSnapshot from './post-snapshot';

Expand All @@ -13,6 +14,7 @@ export {
percy,
request,
isPercyEnabled,
waitForPercyIdle,
fetchPercyDOM,
postSnapshot
};
13 changes: 13 additions & 0 deletions packages/sdk-utils/src/percy-idle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import request from './request';

const RETRY_ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT'];

export async function waitForPercyIdle() {
try {
return !!(await request('/percy/idle'));
} catch (e) {
return RETRY_ERROR_CODES.includes(e.code) && waitForPercyIdle();
}
}

export default waitForPercyIdle;
21 changes: 21 additions & 0 deletions packages/sdk-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ describe('SDK Utils', () => {
});
});

describe('waitForPercyIdle()', () => {
let { waitForPercyIdle } = utils;

it('gets idle state from the CLI API idle endpoint', async () => {
await expectAsync(waitForPercyIdle()).toBeResolvedTo(true);
await expectAsync(helpers.getRequests()).toBeResolvedTo([['/percy/idle']]);
});

it('polls the CLI API idle endpoint on timeout', async () => {
spyOn(utils.request, 'fetch').and.callFake((...args) => {
return utils.request.fetch.calls.count() > 2
? utils.request.fetch.and.originalFn(...args)
// eslint-disable-next-line prefer-promise-reject-errors
: Promise.reject({ code: 'ETIMEDOUT' });
});

await expectAsync(waitForPercyIdle()).toBeResolvedTo(true);
expect(utils.request.fetch).toHaveBeenCalledTimes(3);
});
});

describe('fetchPercyDOM()', () => {
let { fetchPercyDOM } = utils;

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-utils/test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ function context() {
{ success: true, config: { snapshot: { widths: [1280] } } })],
'/percy/config': ({ body }) => [200, 'application/json', (
{ success: true, config: body })],
'/percy/snapshot': () => [200, 'application/json', { success: true }]
'/percy/snapshot': () => [200, 'application/json', { success: true }],
'/percy/idle': () => [200, 'application/json', { success: true }]
}, 5338);

ctx.server.route((req, res, next) => {
Expand Down

0 comments on commit 1c85c52

Please sign in to comment.