-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): use real search-insights to test the insights middleware (…
- Loading branch information
Eunjae Lee
committed
Apr 26, 2021
1 parent
05d05a9
commit 47063e9
Showing
7 changed files
with
81 additions
and
92 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
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