-
Notifications
You must be signed in to change notification settings - Fork 18
feat: update useAnalyticsPageView parameter #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
greatSumini
merged 8 commits into
EveryAnalytics:main
from
greatSumini:enhance/171/enhance-use-analytics-page-view
Sep 1, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d7ae9e
enhance: update useAnalyticsPageView to be called only once
greatSumini 8808dae
enhance: update useAnalyticsPageView to accept callback
greatSumini 74d43d0
test: add useAnalyticsPageView test
greatSumini a3c8a18
test: remove waiting time in useAnalyticsPageView tests
greatSumini 88e2297
chore: fix typo
greatSumini 9948e5f
test: add waitForAsync for remove duplicates
greatSumini 66d5b8f
test: mute logs
greatSumini b6a40bc
Merge branch 'main' of https://github.com/EveryAnalytics/react-analyt…
greatSumini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,69 @@ | ||
| import React from 'react'; | ||
| import * as faker from 'faker'; | ||
|
|
||
| import * as contextModule from '../../src/contexts/useAnalyticsContext'; | ||
| import {useAnalyticsPageView} from '../../src/hooks/useAnalyticsPageView'; | ||
|
|
||
| describe('useAnalyticsPageView', () => { | ||
| const setUp = () => { | ||
| const params = {value: faker.lorem.word()}; | ||
| const callback = () => params; | ||
| const asyncCallback = async () => params; | ||
|
|
||
| const onPageView = jest.fn(); | ||
|
|
||
| const useEffectSpy = jest.spyOn(React, 'useEffect').mockImplementationOnce(cb => cb()); | ||
| const useContextSpy = jest.spyOn(contextModule, 'useAnalyticsContext').mockImplementationOnce( | ||
| () => | ||
| ({ | ||
| onPageView, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| } as any), | ||
| ); | ||
| jest.spyOn(console, 'info').mockImplementation(() => null); | ||
|
|
||
| return { | ||
| params, | ||
| callback, | ||
| asyncCallback, | ||
| onPageView, | ||
| useEffectSpy, | ||
| useContextSpy, | ||
| }; | ||
| }; | ||
|
|
||
| const waitForAsync = () => new Promise(resolve => setTimeout(resolve, 0)); | ||
|
|
||
| test('should call analytics.onPageView with params', async () => { | ||
| const {params, onPageView, useContextSpy, useEffectSpy} = setUp(); | ||
|
|
||
| useAnalyticsPageView(params); | ||
| await waitForAsync(); | ||
|
|
||
| expect(useContextSpy).toHaveBeenCalled(); | ||
| expect(useEffectSpy).toHaveBeenCalled(); | ||
| expect(onPageView).toHaveBeenCalledWith(params); | ||
| }); | ||
|
|
||
| test('should call analytics.onPageView with callback', async () => { | ||
| const {params, callback, onPageView, useContextSpy, useEffectSpy} = setUp(); | ||
|
|
||
| useAnalyticsPageView(callback); | ||
| await waitForAsync(); | ||
|
|
||
| expect(useContextSpy).toHaveBeenCalled(); | ||
| expect(useEffectSpy).toHaveBeenCalled(); | ||
| expect(onPageView).toHaveBeenCalledWith(params); | ||
| }); | ||
|
|
||
| test('should call analytics.onPageView with asyncCallback', async () => { | ||
| const {params, asyncCallback, onPageView, useContextSpy, useEffectSpy} = setUp(); | ||
|
|
||
| useAnalyticsPageView(asyncCallback); | ||
| await waitForAsync(); | ||
|
|
||
| expect(useContextSpy).toHaveBeenCalled(); | ||
| expect(useEffectSpy).toHaveBeenCalled(); | ||
| expect(onPageView).toHaveBeenCalledWith(params); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.