-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
117 changed files
with
888 additions
and
986 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
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
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
79 changes: 79 additions & 0 deletions
79
packages/fullstory-react/src/components/fullstory.test.tsx
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,79 @@ | ||
/// <reference types="jest" /> | ||
import { FullStory } from '@fullstory/browser'; | ||
import { render } from '@testing-library/react'; | ||
import { MockFullstory } from '../index.js'; | ||
|
||
const ONCE = 1; | ||
const TEST_FULLSTORY = Object.assign(jest.fn(), FullStory); | ||
const TEST_INIT = jest.fn(); | ||
const TEST_IS_INITIALIZED = jest.fn(); | ||
|
||
describe('Fullstory', (): void => { | ||
beforeEach((): void => { | ||
TEST_IS_INITIALIZED.mockReturnValue(false); | ||
TEST_INIT.mockImplementation((): void => { | ||
TEST_IS_INITIALIZED.mockReturnValue(true); | ||
}); | ||
}); | ||
|
||
it('should init', (): void => { | ||
render( | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId="test-org-id" | ||
/> | ||
); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
expect(TEST_INIT).toHaveBeenLastCalledWith({ | ||
orgId: 'test-org-id', | ||
}); | ||
}); | ||
|
||
it('should not init on re-render', (): void => { | ||
const { rerender } = render( | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId="test-org-id" | ||
/> | ||
); | ||
|
||
// Re-render | ||
rerender( | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId="test-org-id" | ||
/> | ||
); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
}); | ||
|
||
it('should not init twice', (): void => { | ||
render( | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId="test-org-id" | ||
/> | ||
); | ||
|
||
render( | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId="test-org-id" | ||
/> | ||
); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
}); | ||
}); |
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,36 @@ | ||
'use client'; | ||
|
||
import type { SnippetOptions } from '@fullstory/browser'; | ||
import { useEffect, type PropsWithChildren, type ReactElement } from 'react'; | ||
import useShallowMemo from 'use-shallow-memo'; | ||
import FullstoryContext from '../contexts/fullstory.js'; | ||
import useFullStoryBrowser from '../hooks/use-fullstory-browser.js'; | ||
|
||
export default function Fullstory({ | ||
children, | ||
...snippetOptions | ||
}: PropsWithChildren<SnippetOptions>): ReactElement { | ||
// Contexts | ||
const { FullStory, init, isInitialized } = useFullStoryBrowser(); | ||
|
||
// States | ||
const memoizedSnippetOptions: SnippetOptions = useShallowMemo(snippetOptions); | ||
|
||
// Effects | ||
useEffect((): VoidFunction | undefined => { | ||
if (isInitialized()) { | ||
return; | ||
} | ||
|
||
init(memoizedSnippetOptions); | ||
return (): void => { | ||
FullStory('shutdown'); | ||
}; | ||
}, [FullStory, init, isInitialized, memoizedSnippetOptions]); | ||
|
||
return ( | ||
<FullstoryContext.Provider value={FullStory}> | ||
{children} | ||
</FullstoryContext.Provider> | ||
); | ||
} |
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,6 @@ | ||
'use client'; | ||
|
||
import { type FSApi } from "@fullstory/snippet"; | ||
import { createContext } from "react"; | ||
|
||
export default createContext<FSApi | null>(null); |
66 changes: 19 additions & 47 deletions
66
packages/fullstory-react/src/hooks/use-fullstory.test.tsx
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 |
---|---|---|
@@ -1,74 +1,46 @@ | ||
/// <reference types="jest" /> | ||
import { FullStory } from '@fullstory/browser'; | ||
import { renderHook } from '@testing-library/react'; | ||
import type { PropsWithChildren, ReactElement } from 'react'; | ||
import { MockFullStory, useFullStory } from '../index.js'; | ||
import MockFullstory from '../components/mock-fullstory.js'; | ||
import useFullstory from './use-fullstory.js'; | ||
|
||
const ONCE = 1; | ||
const TEST_FULLSTORY = Object.assign(jest.fn(), FullStory); | ||
const TEST_INIT = jest.fn(); | ||
const TEST_IS_INITIALIZED = jest.fn(); | ||
|
||
function TestWrapper({ children }: Readonly<PropsWithChildren>): ReactElement { | ||
return ( | ||
<MockFullStory init={TEST_INIT} isInitialized={TEST_IS_INITIALIZED}> | ||
<MockFullstory | ||
FullStory={TEST_FULLSTORY} | ||
init={TEST_INIT} | ||
isInitialized={TEST_IS_INITIALIZED} | ||
orgId='test-org-id' | ||
> | ||
{children} | ||
</MockFullStory> | ||
</MockFullstory> | ||
); | ||
} | ||
|
||
describe('useFullStory', (): void => { | ||
describe('useFullstory', (): void => { | ||
beforeEach((): void => { | ||
TEST_IS_INITIALIZED.mockReturnValue(false); | ||
TEST_INIT.mockImplementation((): void => { | ||
TEST_IS_INITIALIZED.mockReturnValue(true); | ||
}); | ||
}); | ||
|
||
it('should init', (): void => { | ||
renderHook(useFullStory, { | ||
wrapper: TestWrapper, | ||
initialProps: { | ||
orgId: 'test-org-id', | ||
}, | ||
}); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
expect(TEST_INIT).toHaveBeenLastCalledWith({ | ||
orgId: 'test-org-id', | ||
}); | ||
}); | ||
|
||
it('should not init on re-render', (): void => { | ||
const { rerender } = renderHook(useFullStory, { | ||
wrapper: TestWrapper, | ||
initialProps: { | ||
orgId: 'test-org-id-1', | ||
}, | ||
}); | ||
|
||
// Re-render | ||
rerender({ | ||
orgId: 'test-org-id-2', | ||
}); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
it('should throw when Fullstory is not provided', (): void => { | ||
expect((): void => { | ||
renderHook(useFullstory); | ||
}).toThrow('Expected the Fullstory context to be provided.'); | ||
}); | ||
|
||
// If two components try to mount simultaneously, only respect the first one. | ||
it('should not init twice', (): void => { | ||
renderHook(useFullStory, { | ||
wrapper: TestWrapper, | ||
initialProps: { | ||
orgId: 'test-org-id-1', | ||
}, | ||
}); | ||
|
||
renderHook(useFullStory, { | ||
it('should provide a Fullstory API', (): void => { | ||
const { result } = renderHook(useFullstory, { | ||
wrapper: TestWrapper, | ||
initialProps: { | ||
orgId: 'test-org-id-2', | ||
}, | ||
}); | ||
|
||
expect(TEST_INIT).toHaveBeenCalledTimes(ONCE); | ||
expect(result).not.toBe(null); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,27 +1,16 @@ | ||
'use client'; | ||
|
||
import type { SnippetOptions } from '@fullstory/browser'; | ||
import { useEffect } from 'react'; | ||
import useShallowMemo from 'use-shallow-memo'; | ||
import useFullStoryBrowser from './use-fullstory-browser.js'; | ||
import type { FSApi } from '@fullstory/snippet'; | ||
import { useContext } from 'react'; | ||
import Fullstory from '../contexts/fullstory.js'; | ||
|
||
export default function useFullStory( | ||
snippetOptions: Readonly<SnippetOptions>, | ||
): FSApi { | ||
export default function useFullstory(): FSApi { | ||
// Contexts | ||
const { FullStory, init, isInitialized } = useFullStoryBrowser(); | ||
const fullstory: FSApi | null = useContext(Fullstory); | ||
|
||
// States | ||
const memoizedSnippetOptions: SnippetOptions = useShallowMemo(snippetOptions); | ||
if (fullstory === null) { | ||
throw new Error('Expected the Fullstory context to be provided.'); | ||
} | ||
|
||
// Effects | ||
useEffect((): void => { | ||
if (isInitialized()) { | ||
return; | ||
} | ||
init(memoizedSnippetOptions); | ||
}, [init, isInitialized, memoizedSnippetOptions]); | ||
|
||
return FullStory; | ||
return fullstory; | ||
} |
Oops, something went wrong.