Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: unit tests for polling #1353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

brandenrodgers
Copy link
Contributor

Description and Context

Adding tests for lib/polling. I also made some changes to make the polling function more generic and easier to test. I'll add more context inline.

Screenshots

TODO

Who to Notify

accountId: number,
taskId: number | string
) => HubSpotPromise<T>;
type PollingCallback<T extends GenericPollingResponse> =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the signature of the callback so we could simplify the arguments in poll. This makes the polling function more generic so it can be more easily leveraged in other places.

FAILURE: 'FAILURE',
} as const;

const DEFAULT_POLLING_STATUS_LOOKUP = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lookup will enable this polling func to be used for other apis, even if their status enums are slightly different from the default ones here. The only requirement in the polling util now is that the callback response needs to include a status field.

import { POLLING_DELAY, POLLING_STATUS } from './constants';
import { DEFAULT_POLLING_DELAY } from './constants';

export const DEFAULT_POLLING_STATES = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this here because it's only relevant to this file

const pollPromise = poll(mockCallback);

// Fast-forward through two polling intervals
jest.advanceTimersByTime(DEFAULT_POLLING_DELAY * 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor came up with the idea to use advanceTimersByTime, but it's a cool way to test utils that leverage setTimeout. This gives us control over each execution of the setTimeout in poll(). So we can iterate over the mock responses one at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant