From b6e25c2edcb7c102dd5176d742d54f45d6668556 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Fri, 20 Oct 2023 16:23:06 +0200 Subject: [PATCH] chore(shared): Improve test coverage --- packages/shared/src/__tests__/browser.test.ts | 59 ++++++++++++++++++- packages/shared/src/__tests__/url.test.ts | 58 +++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/__tests__/browser.test.ts b/packages/shared/src/__tests__/browser.test.ts index 5a801e7d9b..1c34776789 100644 --- a/packages/shared/src/__tests__/browser.test.ts +++ b/packages/shared/src/__tests__/browser.test.ts @@ -1,9 +1,66 @@ -import { inBrowser, isValidBrowserOnline, userAgentIsRobot } from '../browser'; +import { inBrowser, isValidBrowser, isValidBrowserOnline, userAgentIsRobot } from '../browser'; describe('inBrowser()', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + it('returns true if window is defined', () => { expect(inBrowser()).toBe(true); }); + it('returns false if window is undefined', () => { + const windowSpy = jest.spyOn(global, 'window', 'get'); + // @ts-ignore - Test + windowSpy.mockReturnValue(undefined); + expect(inBrowser()).toBe(false); + }); +}); + +describe('isValidBrowser', () => { + let userAgentGetter: any; + let webdriverGetter: any; + + beforeEach(() => { + userAgentGetter = jest.spyOn(window.navigator, 'userAgent', 'get'); + webdriverGetter = jest.spyOn(window.navigator, 'webdriver', 'get'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('returns false if not in browser', () => { + const windowSpy = jest.spyOn(global, 'window', 'get'); + // @ts-ignore - Test + windowSpy.mockReturnValue(undefined); + + expect(isValidBrowser()).toBe(false); + }); + + it('returns true if in browser, navigator is not a bot, and webdriver is not enabled', () => { + userAgentGetter.mockReturnValue( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0', + ); + webdriverGetter.mockReturnValue(false); + + expect(isValidBrowser()).toBe(true); + }); + + it('returns false if navigator is a bot', () => { + userAgentGetter.mockReturnValue('msnbot-NewsBlogs/2.0b (+http://search.msn.com/msnbot.htm)'); + webdriverGetter.mockReturnValue(false); + + expect(isValidBrowser()).toBe(false); + }); + + it('returns false if webdriver is enabled', () => { + userAgentGetter.mockReturnValue( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0', + ); + webdriverGetter.mockReturnValue(true); + + expect(isValidBrowser()).toBe(false); + }); }); describe('detectUserAgentRobot', () => { diff --git a/packages/shared/src/__tests__/url.test.ts b/packages/shared/src/__tests__/url.test.ts index 6410f67c83..7a5f8ce4ef 100644 --- a/packages/shared/src/__tests__/url.test.ts +++ b/packages/shared/src/__tests__/url.test.ts @@ -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/clerk-js@1.2.3/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', + ); + }); +});