-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(shared): Improve test coverage
- Loading branch information
Showing
7 changed files
with
120 additions
and
1 deletion.
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,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
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