-
Notifications
You must be signed in to change notification settings - Fork 284
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(shared): Improve test coverage #1925
Merged
LekoArts
merged 3 commits into
main
from
lekoarts/sdk-848-improve-test-coverage-of-clerkshared
Oct 23, 2023
Merged
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/shared": patch | ||
--- | ||
|
||
Improve internal test coverage and fix small bug inside `callWithRetry` |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { callWithRetry } from '../callWithRetry'; | ||
|
||
describe('callWithRetry', () => { | ||
test('should return the result of the function if it succeeds', async () => { | ||
const fn = jest.fn().mockResolvedValue('result'); | ||
const result = await callWithRetry(fn); | ||
expect(result).toBe('result'); | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should retry the function if it fails', async () => { | ||
const fn = jest.fn().mockRejectedValueOnce(new Error('error')).mockResolvedValueOnce('result'); | ||
const result = await callWithRetry(fn, 1, 2); | ||
expect(result).toBe('result'); | ||
expect(fn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test('should throw an error if the function fails too many times', async () => { | ||
const fn = jest.fn().mockRejectedValue(new Error('error')); | ||
await expect(callWithRetry(fn, 1, 2)).rejects.toThrow('error'); | ||
expect(fn).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { addClerkPrefix, parseSearchParams, stripScheme } from '../url'; | ||
import { addClerkPrefix, getClerkJsMajorVersionOrTag, getScriptUrl, parseSearchParams, stripScheme } from '../url'; | ||
|
||
describe('parseSearchParams(queryString)', () => { | ||
it('parses query string and returns a URLSearchParams object', () => { | ||
|
@@ -56,3 +56,59 @@ describe('addClerkPrefix(str)', () => { | |
expect(addClerkPrefix(urlInput)).toBe(urlOutput); | ||
}); | ||
}); | ||
|
||
describe('getClerkJsMajorVersionOrTag', () => { | ||
const stagingFrontendApi = 'foobar.lclstage.dev'; | ||
|
||
it('returns staging if pkgVersion is not provided and frontendApi is staging', () => { | ||
expect(getClerkJsMajorVersionOrTag(stagingFrontendApi)).toBe('staging'); | ||
}); | ||
|
||
it('returns latest if pkgVersion is not provided and frontendApi is not staging', () => { | ||
expect(getClerkJsMajorVersionOrTag('foobar.dev')).toBe('latest'); | ||
}); | ||
|
||
it('returns next if pkgVersion contains next', () => { | ||
expect(getClerkJsMajorVersionOrTag('foobar.dev', '1.2.3-next.4')).toBe('next'); | ||
}); | ||
|
||
it('returns the major version if pkgVersion is provided', () => { | ||
expect(getClerkJsMajorVersionOrTag('foobar.dev', '1.2.3')).toBe('1'); | ||
}); | ||
|
||
it('returns latest if pkgVersion is empty string', () => { | ||
expect(getClerkJsMajorVersionOrTag('foobar.dev', '')).toBe('latest'); | ||
}); | ||
}); | ||
|
||
describe('getScriptUrl', () => { | ||
const frontendApi = 'https://foobar.dev'; | ||
|
||
it('returns URL using the clerkJSVersion if provided', () => { | ||
expect(getScriptUrl(frontendApi, { clerkJSVersion: '1.2.3' })).toBe( | ||
'https://foobar.dev/npm/@clerk/[email protected]/dist/clerk.browser.js', | ||
); | ||
}); | ||
|
||
it('returns URL using the latest version if clerkJSVersion & pkgVersion is not provided + frontendApi is not staging', () => { | ||
expect(getScriptUrl(frontendApi, {})).toBe('https://foobar.dev/npm/@clerk/clerk-js@latest/dist/clerk.browser.js'); | ||
}); | ||
|
||
it('returns URL using the major version if only pkgVersion is provided', () => { | ||
expect(getScriptUrl(frontendApi, { pkgVersion: '1.2.3' })).toBe( | ||
'https://foobar.dev/npm/@clerk/clerk-js@1/dist/clerk.browser.js', | ||
); | ||
}); | ||
|
||
it('returns URL using the major version if only pkgVersion contains next', () => { | ||
expect(getScriptUrl(frontendApi, { pkgVersion: '1.2.3-next.4' })).toBe( | ||
'https://foobar.dev/npm/@clerk/clerk-js@next/dist/clerk.browser.js', | ||
); | ||
}); | ||
|
||
it('returns URL using the staging tag if frontendApi is staging', () => { | ||
expect(getScriptUrl('https://foobar.lclstage.dev', {})).toBe( | ||
'https://foobar.lclstage.dev/npm/@clerk/clerk-js@staging/dist/clerk.browser.js', | ||
); | ||
}); | ||
}); |
This file contains 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
22 changes: 22 additions & 0 deletions
22
packages/shared/src/utils/__tests__/createDeferredPromise.test.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createDeferredPromise } from '../createDeferredPromise'; | ||
|
||
describe('createDeferredPromise', () => { | ||
test('resolves with correct value', async () => { | ||
const { promise, resolve } = createDeferredPromise(); | ||
const expectedValue = 'hello world'; | ||
resolve(expectedValue); | ||
const result = await promise; | ||
expect(result).toBe(expectedValue); | ||
}); | ||
|
||
test('rejects with correct error', async () => { | ||
const { promise, reject } = createDeferredPromise(); | ||
const expectedError = new Error('something went wrong'); | ||
reject(expectedError); | ||
try { | ||
await promise; | ||
} catch (error) { | ||
expect(error).toBe(expectedError); | ||
} | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { isStaging } from '../instance'; | ||
|
||
describe('isStaging', () => { | ||
it.each([ | ||
['clerk', false], | ||
['clerk.com', false], | ||
['whatever.com', false], | ||
['clerk.abcef', false], | ||
['clerk.abcef.12345', false], | ||
['clerk.abcef.12345.lcl', false], | ||
['clerk.abcef.12345.lcl.dev', false], | ||
['clerk.abcef.12345.stg.dev', false], | ||
['clerk.abcef.12345.lclstage.dev', true], | ||
['clerk.abcef.12345.stgstage.dev', true], | ||
['clerk.abcef.12345.clerkstage.dev', true], | ||
['clerk.abcef.12345.accountsstage.dev', true], | ||
])('validates the frontendApi format', (str, expected) => { | ||
expect(isStaging(str)).toBe(expected); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/shared/src/utils/__tests__/runWithExponentialBackOff.test.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { runWithExponentialBackOff } from '../runWithExponentialBackOff'; | ||
|
||
describe('runWithExponentialBackOff', () => { | ||
test('resolves with the result of the callback', async () => { | ||
const result = await runWithExponentialBackOff(() => Promise.resolve('success')); | ||
expect(result).toBe('success'); | ||
}); | ||
|
||
test('retries the callback until it succeeds', async () => { | ||
let attempts = 0; | ||
const result = await runWithExponentialBackOff(() => { | ||
attempts++; | ||
if (attempts < 3) { | ||
throw new Error('failed'); | ||
} | ||
return Promise.resolve('success'); | ||
}); | ||
expect(result).toBe('success'); | ||
expect(attempts).toBe(3); | ||
}); | ||
}); |
This file contains 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
Oops, something went wrong.
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.
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.
The tests actually uncovered this small bug here