-
Notifications
You must be signed in to change notification settings - Fork 497
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
chore(test): use real search-insights to test the insights middleware #4712
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fa7700f
chore(test): use real search-insights to test the insights middleware
eunjae-lee 7058574
fix yarn.lock
eunjae-lee d0e0fe5
embed the implementation of getFunctionalInterface and processQueue
eunjae-lee 70cde71
import directly from the source
eunjae-lee fb04d58
fix jest config
eunjae-lee 384262d
Merge branch 'master' into fix/insights-test
ca5c8a7
avoid type error
eunjae-lee 6d1be8f
chore: update search-insights with the latest temp version
eunjae-lee 8db35c9
Merge branch 'master' into fix/insights-test
eunjae-lee b6d9269
update tsconfig.v3.json
eunjae-lee 2ad3cc0
update search-insights
eunjae-lee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,19 +3,15 @@ import algoliasearchHelper from 'algoliasearch-helper'; | |
import { createInsightsMiddleware } from '../'; | ||
import { createInstantSearch } from '../../../test/mock/createInstantSearch'; | ||
import { | ||
createAlgoliaAnalytics, | ||
createInsightsClient, | ||
createInsights, | ||
createInsightsUmdVersion, | ||
AlgoliaAnalytics, | ||
ANONYMOUS_TOKEN, | ||
} from '../../../test/mock/createInsightsClient'; | ||
import { warning } from '../../lib/utils'; | ||
import { SearchClient } from '../../types'; | ||
|
||
describe('insights', () => { | ||
const createTestEnvironment = () => { | ||
const analytics = createAlgoliaAnalytics(); | ||
const insightsClient = jest.fn(createInsightsClient(analytics)); | ||
const { analytics, insightsClient } = createInsights(); | ||
const instantSearchInstance = createInstantSearch({ | ||
client: algoliasearch('myAppId', 'myApiKey'), | ||
}); | ||
|
@@ -37,11 +33,13 @@ describe('insights', () => { | |
}; | ||
}; | ||
|
||
const createUmdTestEnvironment = (algoliaAnalytics?: AlgoliaAnalytics) => { | ||
const createUmdTestEnvironment = () => { | ||
const { | ||
analytics, | ||
insightsClient, | ||
libraryLoadedAndProcessQueue, | ||
} = createInsightsUmdVersion(algoliaAnalytics); | ||
} = createInsightsUmdVersion(); | ||
|
||
const instantSearchInstance = createInstantSearch({ | ||
client: algoliasearch('myAppId', 'myApiKey'), | ||
}); | ||
|
@@ -54,6 +52,7 @@ describe('insights', () => { | |
getHelper: () => helper, | ||
}; | ||
return { | ||
analytics, | ||
insightsClient, | ||
libraryLoadedAndProcessQueue, | ||
instantSearchInstance, | ||
|
@@ -105,12 +104,12 @@ describe('insights', () => { | |
}); | ||
|
||
it('does not throw when an event is sent right after the creation in UMD', () => { | ||
const algoliaAnalytics = createAlgoliaAnalytics(); | ||
const { | ||
analytics, | ||
insightsClient, | ||
libraryLoadedAndProcessQueue, | ||
instantSearchInstance, | ||
} = createUmdTestEnvironment(algoliaAnalytics); | ||
} = createUmdTestEnvironment(); | ||
|
||
const middleware = createInsightsMiddleware({ | ||
insightsClient, | ||
|
@@ -128,7 +127,7 @@ describe('insights', () => { | |
}, | ||
widgetType: 'ais.hits', | ||
}); | ||
expect(algoliaAnalytics.viewedObjectIDs).toHaveBeenCalledTimes(0); | ||
expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(0); | ||
|
||
// But, the library hasn't been loaded yet, so the event stays in the queue. | ||
expect(insightsClient.queue[insightsClient.queue.length - 1]).toEqual([ | ||
|
@@ -138,8 +137,8 @@ describe('insights', () => { | |
|
||
// When the library is loaded later, it consumes the queue and sends the event. | ||
libraryLoadedAndProcessQueue(); | ||
expect(algoliaAnalytics.viewedObjectIDs).toHaveBeenCalledTimes(1); | ||
expect(algoliaAnalytics.viewedObjectIDs).toHaveBeenCalledWith({ | ||
expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(1); | ||
expect(analytics.viewedObjectIDs).toHaveBeenCalledWith({ | ||
eventName: 'Hits Viewed', | ||
index: '', | ||
objectIDs: ['1', '2'], | ||
|
@@ -247,7 +246,7 @@ describe('insights', () => { | |
insightsClient, | ||
})({ instantSearchInstance }); | ||
middleware.subscribe(); | ||
expect(getUserToken()).toEqual(ANONYMOUS_TOKEN); | ||
expect(getUserToken()).toEqual(expect.stringMatching(/^anonymous-/)); | ||
}); | ||
|
||
it('applies userToken which was set before init', () => { | ||
|
@@ -315,7 +314,7 @@ describe('insights', () => { | |
expect(getUserToken()).toEqual('token-from-queue'); | ||
}); | ||
|
||
it('ignores userToken set before init', () => { | ||
it('does not override userToken set before init with anonymous token', () => { | ||
const { | ||
insightsClient, | ||
instantSearchInstance, | ||
|
@@ -331,7 +330,7 @@ describe('insights', () => { | |
insightsClient, | ||
})({ instantSearchInstance }); | ||
middleware.subscribe(); | ||
expect(getUserToken()).toEqual(ANONYMOUS_TOKEN); | ||
expect(getUserToken()).toEqual('token-from-queue-before-init'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change in this test case is technically a breaking change, but the previous behavior was wrong. If user calls |
||
}); | ||
}); | ||
|
@@ -352,14 +351,18 @@ describe('insights', () => { | |
instantSearchInstance.sendEventToInsights({ | ||
insightsMethod: 'viewedObjectIDs', | ||
widgetType: 'ais.customWidget', | ||
eventType: 'click', | ||
eventType: 'view', | ||
payload: { | ||
hello: 'world', | ||
index: 'my-index', | ||
eventName: 'My Hits Viewed', | ||
objectIDs: ['obj1'], | ||
}, | ||
}); | ||
expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(1); | ||
expect(analytics.viewedObjectIDs).toHaveBeenCalledWith({ | ||
hello: 'world', | ||
index: 'my-index', | ||
eventName: 'My Hits Viewed', | ||
objectIDs: ['obj1'], | ||
}); | ||
}); | ||
|
||
|
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,89 +1,55 @@ | ||
export const ANONYMOUS_TOKEN = 'anonymous-user-id-1'; | ||
import AlgoliaAnalytics from 'search-insights/lib/insights'; | ||
import { processQueue } from 'search-insights/lib/_processQueue'; | ||
import { getFunctionalInterface } from 'search-insights/lib/_getFunctionalInterface'; | ||
|
||
export type AlgoliaAnalytics = { | ||
setUserToken(userToken: string): void; | ||
init({ appId, apiKey }): void; | ||
getUserToken( | ||
options: any, | ||
callback: (error: any, userToken: string) => void | ||
): void; | ||
onUserTokenChange( | ||
callback: (value: string) => void, | ||
options?: { immediate?: boolean } | ||
): void; | ||
viewedObjectIDs(...args: any[]): void; | ||
}; | ||
|
||
export function createAlgoliaAnalytics(): AlgoliaAnalytics { | ||
let values: any = {}; | ||
const setValues = obj => { | ||
values = { | ||
...values, | ||
...obj, | ||
}; | ||
}; | ||
let userTokenCallback; | ||
const setUserToken = userToken => { | ||
setValues({ _userToken: userToken }); | ||
if (userTokenCallback) { | ||
userTokenCallback(userToken); | ||
} | ||
}; | ||
const init = ({ appId, apiKey }) => { | ||
setValues({ _hasCredentials: true, _appId: appId, _apiKey: apiKey }); | ||
setUserToken(ANONYMOUS_TOKEN); | ||
}; | ||
const getUserToken = (_options, callback) => | ||
callback(null, values._userToken); | ||
const onUserTokenChange = (callback, { immediate = false } = {}) => { | ||
userTokenCallback = callback; | ||
if (immediate) { | ||
callback(values._userToken); | ||
} | ||
}; | ||
const viewedObjectIDs = jest.fn(); | ||
export function createInsights() { | ||
const analytics = mockSendingEvents( | ||
new AlgoliaAnalytics({ | ||
requestFn: jest.fn(), | ||
}) | ||
); | ||
const insightsClient = jest.fn(getFunctionalInterface(analytics)); | ||
|
||
return { | ||
setUserToken, | ||
init, | ||
getUserToken, | ||
onUserTokenChange, | ||
viewedObjectIDs, | ||
analytics, | ||
insightsClient, | ||
}; | ||
} | ||
|
||
export function createInsightsClient(instance = createAlgoliaAnalytics()) { | ||
return (methodName, ...args) => { | ||
if (!instance[methodName]) { | ||
throw new Error(`${methodName} doesn't exist in this mocked instance`); | ||
} | ||
instance[methodName](...args); | ||
}; | ||
} | ||
|
||
export function createInsightsUmdVersion( | ||
algoliaAnalytics = createAlgoliaAnalytics() | ||
) { | ||
export function createInsightsUmdVersion() { | ||
const globalObject: any = {}; | ||
globalObject.AlgoliaAnalyticsObject = 'aa'; | ||
globalObject.aa = (...args) => { | ||
globalObject.aa.queue = globalObject.aa.queue || []; | ||
globalObject.aa.queue.push(args); | ||
}; | ||
const analytics = mockSendingEvents( | ||
new AlgoliaAnalytics({ | ||
requestFn: jest.fn(), | ||
}) | ||
); | ||
|
||
return { | ||
analytics, | ||
insightsClient: globalObject.aa, | ||
libraryLoadedAndProcessQueue: () => { | ||
const _aa = createInsightsClient(algoliaAnalytics); | ||
const queue = globalObject.aa.queue; | ||
queue.forEach(([methodName, ...args]) => { | ||
_aa(methodName, ...args); | ||
}); | ||
queue.push = ([methodName, ...args]) => { | ||
_aa(methodName, ...args); | ||
}; | ||
return { | ||
algoliaAnalytics, | ||
}; | ||
processQueue.call(analytics, globalObject); | ||
}, | ||
}; | ||
} | ||
|
||
function mockSendingEvents(analytics: AlgoliaAnalytics) { | ||
analytics.viewedFilters = jest.fn(analytics.viewedFilters); | ||
analytics.viewedObjectIDs = jest.fn(analytics.viewedObjectIDs); | ||
analytics.clickedFilters = jest.fn(analytics.clickedFilters); | ||
analytics.clickedObjectIDs = jest.fn(analytics.clickedObjectIDs); | ||
analytics.clickedObjectIDsAfterSearch = jest.fn( | ||
analytics.clickedObjectIDsAfterSearch | ||
); | ||
analytics.convertedFilters = jest.fn(analytics.convertedFilters); | ||
analytics.convertedObjectIDs = jest.fn(analytics.convertedObjectIDs); | ||
analytics.convertedObjectIDsAfterSearch = jest.fn( | ||
analytics.convertedObjectIDsAfterSearch | ||
); | ||
return analytics; | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm pending this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated 2ad3cc0