-
Notifications
You must be signed in to change notification settings - Fork 141
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
12 changed files
with
177 additions
and
96 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,5 @@ | ||
--- | ||
'@segment/analytics-signals': patch | ||
--- | ||
|
||
Add signals logging for events |
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
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
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
4 changes: 2 additions & 2 deletions
4
...s/src/lib/__tests__/normalize-url.test.ts → ...alize-url/__tests__/normalize-url.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
65 changes: 65 additions & 0 deletions
65
packages/signals/signals/src/lib/storage/__tests__/web-storage.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,65 @@ | ||
import { WebStorage } from '../web-storage' | ||
|
||
describe('WebStorage', () => { | ||
let webStorage: WebStorage | ||
|
||
beforeEach(() => { | ||
webStorage = new WebStorage(sessionStorage) | ||
}) | ||
afterEach(() => { | ||
sessionStorage.clear() | ||
}) | ||
describe('getItem, setItem', () => { | ||
it('should retrieve and parse a stored value from storage', () => { | ||
const key = 'testKey' | ||
const value = { foo: 'bar' } | ||
|
||
webStorage.setItem(key, value) | ||
|
||
const result = webStorage.getItem<typeof value>(key) | ||
|
||
expect(result).toEqual(value) | ||
}) | ||
|
||
it('should return undefined if the key does not exist in storage', () => { | ||
const key = 'nonexistentKey' | ||
|
||
const result = webStorage.getItem(key) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should handle JSON serializing errors gracefully when setting', () => { | ||
const key = 'testKey' | ||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation() | ||
|
||
webStorage.setItem( | ||
key, | ||
// @ts-ignore - intentational non-serializable value | ||
BigInt(1) | ||
) | ||
|
||
expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
'Storage set error', | ||
expect.any(Object), | ||
expect.any(Error) | ||
) | ||
}) | ||
|
||
it('should handle JSON parsing errors gracefully when retrieving', () => { | ||
const key = 'testKey' | ||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation() | ||
|
||
// if somehow invalid JSON is stored in the storage | ||
sessionStorage.setItem(key, 'invalid JSON') | ||
|
||
const result = webStorage.getItem(key) | ||
|
||
expect(result).toBeUndefined() | ||
expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
'Storage retrieval error', | ||
expect.any(Object), | ||
expect.any(Error) | ||
) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
export class WebStorage { | ||
private storage: Storage | ||
constructor(storage: Storage) { | ||
this.storage = storage | ||
} | ||
|
||
/** | ||
* Set a json-parsable item in storage | ||
*/ | ||
public setItem = <T extends string | number | boolean | object>( | ||
key: string, | ||
value: T | ||
): void => { | ||
try { | ||
const item = JSON.stringify(value) | ||
this.storage.setItem(key, item) | ||
} catch (e) { | ||
console.warn('Storage set error', { key, value }, e) | ||
} | ||
} | ||
|
||
/** | ||
* Get a json-parsed item from storage | ||
*/ | ||
public getItem = <T>(key: string): T | undefined => { | ||
try { | ||
const item = this.storage.getItem(key) | ||
if (item === null) { | ||
return undefined | ||
} | ||
return JSON.parse(item) as T | ||
} catch (e) { | ||
console.warn('Storage retrieval error', { key }, e) | ||
} | ||
return undefined | ||
} | ||
} |
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