Skip to content

Commit

Permalink
test(astro): add Clerk script loader tests
Browse files Browse the repository at this point in the history
test(astro): Test script loader version

test(astro): Add main script loader test
  • Loading branch information
wobsoriano committed Jul 10, 2024
1 parent 696b221 commit 401cc89
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions packages/shared/src/__tests__/loadClerkJsScript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { buildClerkJsScriptAttributes, clerkJsScriptUrl, loadClerkJsScript } from '../loadClerkJsScript';
import { loadScript } from '../loadScript';

jest.mock('../loadScript');

describe('loadClerkJsScript()', () => {
const mockPublishableKey = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk';

beforeEach(() => {
jest.clearAllMocks();
(loadScript as jest.Mock).mockResolvedValue(undefined);
document.querySelector = jest.fn().mockReturnValue(null);
});

test('throws error when publishableKey is missing', () => {
expect(() => loadClerkJsScript({} as any)).rejects.toThrow(
'Clerk: Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.',
);
});

test('loads script when no existing script is found', async () => {
await loadClerkJsScript({ publishableKey: mockPublishableKey });

expect(loadScript).toHaveBeenCalledWith(
expect.stringContaining('https://foo-bar-13.clerk.accounts.dev/npm/@clerk/clerk-js@5/dist/clerk.browser.js'),
expect.objectContaining({
async: true,
crossOrigin: 'anonymous',
beforeLoad: expect.any(Function),
}),
);
});

test('uses existing script when found', async () => {
const mockExistingScript = document.createElement('script');
document.querySelector = jest.fn().mockReturnValue(mockExistingScript);

const loadPromise = loadClerkJsScript({ publishableKey: mockPublishableKey });
mockExistingScript.dispatchEvent(new Event('load'));

await expect(loadPromise).resolves.toBe(mockExistingScript);
expect(loadScript).not.toHaveBeenCalled();
});

test('rejects when existing script fails to load', async () => {
const mockExistingScript = document.createElement('script');
document.querySelector = jest.fn().mockReturnValue(mockExistingScript);

const loadPromise = loadClerkJsScript({ publishableKey: mockPublishableKey });
mockExistingScript.dispatchEvent(new Event('error'));

await expect(loadPromise).rejects.toBe('Clerk: Failed to load Clerk');
expect(loadScript).not.toHaveBeenCalled();
});

test('throws error when loadScript fails', async () => {
(loadScript as jest.Mock).mockRejectedValue(new Error('Script load failed'));

await expect(loadClerkJsScript({ publishableKey: mockPublishableKey })).rejects.toThrow(
'Clerk: Failed to load Clerk',
);
});
});

describe('clerkJsScriptUrl()', () => {
const mockDevPublishableKey = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk';
const mockProdPublishableKey = 'pk_live_ZXhhbXBsZS5jbGVyay5hY2NvdW50cy5kZXYk';

test('returns clerkJSUrl when provided', () => {
const customUrl = 'https://custom.clerk.com/clerk.js';
const result = clerkJsScriptUrl({ clerkJSUrl: customUrl, publishableKey: mockDevPublishableKey });
expect(result).toBe(customUrl);
});

test('constructs URL correctly for development key', () => {
const result = clerkJsScriptUrl({ publishableKey: mockDevPublishableKey });
expect(result).toBe('https://foo-bar-13.clerk.accounts.dev/npm/@clerk/clerk-js@5/dist/clerk.browser.js');
});

test('constructs URL correctly for production key', () => {
const result = clerkJsScriptUrl({ publishableKey: mockProdPublishableKey });
expect(result).toBe('https://example.clerk.accounts.dev/npm/@clerk/clerk-js@5/dist/clerk.browser.js');
});

test('includes clerkJSVariant in URL when provided', () => {
const result = clerkJsScriptUrl({ publishableKey: mockProdPublishableKey, clerkJSVariant: 'headless' });
expect(result).toBe('https://example.clerk.accounts.dev/npm/@clerk/clerk-js@5/dist/clerk.headless.browser.js');
});

test('uses provided clerkJSVersion', () => {
const result = clerkJsScriptUrl({ publishableKey: mockDevPublishableKey, clerkJSVersion: '6' });
expect(result).toContain('/npm/@clerk/clerk-js@6/');
});
});

describe('buildClerkJsScriptAttributes()', () => {
const mockPublishableKey = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk';
const mockProxyUrl = 'https://proxy.clerk.com';
const mockDomain = 'custom.com';

test.each([
[
'all options',
{ publishableKey: mockPublishableKey, proxyUrl: mockProxyUrl, domain: mockDomain },
{
'data-clerk-publishable-key': mockPublishableKey,
'data-clerk-proxy-url': mockProxyUrl,
'data-clerk-domain': mockDomain,
},
],
[
'only publishableKey',
{ publishableKey: mockPublishableKey },
{ 'data-clerk-publishable-key': mockPublishableKey },
],
[
'publishableKey and proxyUrl',
{ publishableKey: mockPublishableKey, proxyUrl: mockProxyUrl },
{ 'data-clerk-publishable-key': mockPublishableKey, 'data-clerk-proxy-url': mockProxyUrl },
],
['no options', {}, {}],
])('returns correct attributes with %s', (_, input, expected) => {
// @ts-ignore input loses correct type because of empty object
expect(buildClerkJsScriptAttributes(input)).toEqual(expected);
});
});

0 comments on commit 401cc89

Please sign in to comment.