-
Notifications
You must be signed in to change notification settings - Fork 0
Reset polling on init
#21
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,11 @@ beforeEach(() => { | |||||||||
| prefab.configs = {}; | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| afterEach(() => { | ||||||||||
| prefab.stopPolling(); | ||||||||||
| prefab.pollCount = 0; | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| describe('init', () => { | ||||||||||
| it('works when the request is successful', async () => { | ||||||||||
| const data = {values: {turbo: {double: 2.5}}}; | ||||||||||
|
|
@@ -91,7 +96,6 @@ describe('poll', () => { | |||||||||
|
|
||||||||||
| await prefab.poll({frequencyInMs}); | ||||||||||
| expect(prefab.loader.context).toStrictEqual(prefab.context); | ||||||||||
| await wait(1); | ||||||||||
|
|
||||||||||
| if (prefab.pollStatus.status !== 'running') { | ||||||||||
| throw new Error('Expected pollStatus to be running'); | ||||||||||
|
|
@@ -117,6 +121,41 @@ describe('poll', () => { | |||||||||
| await wait(frequencyInMs * 2); | ||||||||||
| expect(prefab.pollCount).toEqual(2); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('is reset on init', async () => { | ||||||||||
| jest.spyOn(globalThis, 'clearTimeout'); | ||||||||||
|
|
||||||||||
| const data = {values: {}}; | ||||||||||
| const frequencyInMs = 25; | ||||||||||
| fetchMock.mockResponse(JSON.stringify(data)); | ||||||||||
|
|
||||||||||
| const config: InitParams = { | ||||||||||
| apiKey: '1234', | ||||||||||
| context: new Context({user: {device: 'desktop'}}), | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| await prefab.init(config); | ||||||||||
|
|
||||||||||
| if (!prefab.loader) { | ||||||||||
| throw new Error('Expected loader to be set'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| await prefab.poll({frequencyInMs}); | ||||||||||
| expect(prefab.loader.context).toStrictEqual(prefab.context); | ||||||||||
|
|
||||||||||
| if (prefab.pollStatus.status !== 'running') { | ||||||||||
| throw new Error('Expected pollStatus to be running'); | ||||||||||
| } | ||||||||||
|
Comment on lines
+146
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| expect(prefab.pollCount).toEqual(0); | ||||||||||
| expect(prefab.loader.context).toStrictEqual(prefab.context); | ||||||||||
|
|
||||||||||
| const timeoutId = prefab.pollTimeoutId; | ||||||||||
|
|
||||||||||
| await prefab.init(config); | ||||||||||
| expect(prefab.pollStatus).toEqual({status: 'stopped'}); | ||||||||||
| expect(clearTimeout).toHaveBeenCalledWith(timeoutId); | ||||||||||
| expect(prefab.pollTimeoutId).toBeUndefined(); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| describe('setConfig', () => { | ||||||||||
|
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to write these as expectations instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A reasonable thing to suggest. But if I do that, I still have to guard against this being
undefinedor when I later callprefab.loader.contexttypescript complainserror TS18048: 'prefab.loader' is possibly 'undefined'.There are other ways to handle this, but IME this is the simplest way to handle it in test code.
If you have a succinct preferred approach, please do tell!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhh, good ol' typescript doesn't pick up the guards from the expectations in jest yet without writing wrapper functions per this PR: microsoft/TypeScript#32695
👍 keep on trucking.