-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
4 deletions.
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,6 @@ | ||
--- | ||
'@segment/analytics-next': minor | ||
'@segment/analytics-core': patch | ||
--- | ||
|
||
Add useQueryString option to InitOptions |
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
60 changes: 60 additions & 0 deletions
60
packages/browser/src/core/query-string/__tests__/useQueryString.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,60 @@ | ||
import unfetch from 'unfetch' | ||
import { AnalyticsBrowser } from '../../..' | ||
import { createSuccess } from '../../../test-helpers/factories' | ||
|
||
jest.mock('unfetch') | ||
jest | ||
.mocked(unfetch) | ||
.mockImplementation(() => createSuccess({ integrations: {} })) | ||
|
||
// @ts-ignore | ||
delete window.location | ||
// @ts-ignore | ||
window.location = new URL( | ||
'https://www.example.com?ajs_aid=873832VB&ajs_uid=xcvn7568' | ||
) | ||
|
||
describe('useQueryString configuration option', () => { | ||
it('ignores aid and uid from query string when disabled', async () => { | ||
const [analyticsAlt] = await AnalyticsBrowser.load( | ||
{ writeKey: 'abc' }, | ||
{ | ||
useQueryString: false, | ||
} | ||
) | ||
|
||
// not acknowledge the aid provided in the query params, let ajs generate one | ||
expect(analyticsAlt.user().anonymousId()).not.toBe('873832VB') | ||
expect(analyticsAlt.user().id()).toBe(null) | ||
}) | ||
|
||
it('ignores uid when it doesnt match the required pattern', async () => { | ||
const [analyticsAlt] = await AnalyticsBrowser.load( | ||
{ writeKey: 'abc' }, | ||
{ | ||
useQueryString: { | ||
uid: /[A-Z]{6}/, | ||
}, | ||
} | ||
) | ||
|
||
// no constraint was set for aid therefore accepted | ||
expect(analyticsAlt.user().anonymousId()).toBe('873832VB') | ||
expect(analyticsAlt.user().id()).toBe(null) | ||
}) | ||
|
||
it('accepts both aid and uid from query string when they match the required pattern', async () => { | ||
const [analyticsAlt] = await AnalyticsBrowser.load( | ||
{ writeKey: 'abc' }, | ||
{ | ||
useQueryString: { | ||
aid: /\d{6}[A-Z]{2}/, | ||
uid: /[a-z]{4}\d{4}/, | ||
}, | ||
} | ||
) | ||
|
||
expect(analyticsAlt.user().anonymousId()).toBe('873832VB') | ||
expect(analyticsAlt.user().id()).toBe('xcvn7568') | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Spec derived from https://github.com/jonschlinkert/is-plain-object/blob/master/test/server.js | ||
|
||
import { isPlainObject } from '../is-plain-object' | ||
|
||
describe('isPlainObject', () => { | ||
it('should return `true` if the object is created by the `Object` constructor.', function () { | ||
expect(isPlainObject(Object.create({}))).toBe(true) | ||
expect(isPlainObject(Object.create(Object.prototype))).toBe(true) | ||
expect(isPlainObject({ foo: 'bar' })).toBe(true) | ||
expect(isPlainObject({})).toBe(true) | ||
expect(isPlainObject(Object.create(null))).toBe(true) | ||
}) | ||
|
||
it('should return `false` if the object is not created by the `Object` constructor.', function () { | ||
class Foo { | ||
abc = {} | ||
} | ||
|
||
expect(isPlainObject(/foo/)).toBe(false) | ||
expect(isPlainObject(function () {})).toBe(false) | ||
expect(isPlainObject(1)).toBe(false) | ||
expect(isPlainObject(['foo', 'bar'])).toBe(false) | ||
expect(isPlainObject([])).toBe(false) | ||
expect(isPlainObject(new Foo())).toBe(false) | ||
expect(isPlainObject(null)).toBe(false) | ||
}) | ||
}) |
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,26 @@ | ||
// Code derived from https://github.com/jonschlinkert/is-plain-object/blob/master/is-plain-object.js | ||
|
||
function isObject(o: unknown): o is Object { | ||
return Object.prototype.toString.call(o) === '[object Object]' | ||
} | ||
|
||
export function isPlainObject(o: unknown): o is Record<PropertyKey, unknown> { | ||
if (isObject(o) === false) return false | ||
|
||
// If has modified constructor | ||
const ctor = (o as any).constructor | ||
if (ctor === undefined) return true | ||
|
||
// If has modified prototype | ||
const prot = ctor.prototype | ||
if (isObject(prot) === false) return false | ||
|
||
// If constructor does not have an Object-specific method | ||
// eslint-disable-next-line no-prototype-builtins | ||
if ((prot as Object).hasOwnProperty('isPrototypeOf') === false) { | ||
return false | ||
} | ||
|
||
// Most likely a plain Object | ||
return true | ||
} |