Skip to content

Commit

Permalink
chore(shared): Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Oct 20, 2023
1 parent 92727ee commit cd1b641
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/shared/src/__tests__/callWithRetry.test.ts
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);
});
});
30 changes: 30 additions & 0 deletions packages/shared/src/__tests__/keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
buildPublishableKey,
createDevOrStagingUrlCache,
isDevelopmentFromApiKey,
isLegacyFrontendApiKey,
isProductionFromApiKey,
isPublishableKey,
parsePublishableKey,
} from '../keys';
Expand Down Expand Up @@ -90,3 +92,31 @@ describe('isDevOrStagingUrl(url)', () => {
expect(isDevOrStagingUrl(a)).toBe(expected);
});
});

describe('isDevelopmentFromApiKey(key)', () => {
const cases: Array<[string, boolean]> = [
['sk_live_Y2xlcmsuY2xlcmsuZGV2JA==', false],
['sk_test_Y2xlcmsuY2xlcmsuZGV2JA==', true],
['live_Y2xlcmsuY2xlcmsuZGV2JA==', false],
['test_Y2xlcmsuY2xlcmsuZGV2JA==', true],
];

test.each(cases)('given %p as a publishable key string, returns %p', (publishableKeyStr, expected) => {
const result = isDevelopmentFromApiKey(publishableKeyStr);
expect(result).toEqual(expected);
});
});

describe('isProductionFromApiKey(key)', () => {
const cases: Array<[string, boolean]> = [
['sk_live_Y2xlcmsuY2xlcmsuZGV2JA==', true],
['sk_test_Y2xlcmsuY2xlcmsuZGV2JA==', false],
['live_Y2xlcmsuY2xlcmsuZGV2JA==', true],
['test_Y2xlcmsuY2xlcmsuZGV2JA==', false],
];

test.each(cases)('given %p as a publishable key string, returns %p', (publishableKeyStr, expected) => {
const result = isProductionFromApiKey(publishableKeyStr);
expect(result).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion packages/shared/src/callWithRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export async function callWithRetry<T>(
}
await wait(2 ** attempt * 100);

return callWithRetry(fn, attempt + 1);
return callWithRetry(fn, attempt + 1, maxAttempts);
}
}
22 changes: 22 additions & 0 deletions packages/shared/src/utils/__tests__/createDeferredPromise.test.ts
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);
}
});
});
20 changes: 20 additions & 0 deletions packages/shared/src/utils/__tests__/instance.test.ts
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);
});
});
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);
});
});
3 changes: 3 additions & 0 deletions packages/shared/src/utils/instance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Check if the frontendApi ends with a staging domain
*/
export function isStaging(frontendApi: string): boolean {
return (
frontendApi.endsWith('.lclstage.dev') ||
Expand Down

0 comments on commit cd1b641

Please sign in to comment.