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 0941a74 commit b6e25c2
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
59 changes: 58 additions & 1 deletion packages/shared/src/__tests__/browser.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
58 changes: 57 additions & 1 deletion packages/shared/src/__tests__/url.test.ts
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', () => {
Expand Down Expand Up @@ -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',
);
});
});

0 comments on commit b6e25c2

Please sign in to comment.