-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor wait function and util testing
- Loading branch information
1 parent
8a222ba
commit 82ef499
Showing
3 changed files
with
120 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { validSearchQueryParamKeys } from "src/types/search/searchResponseTypes"; | ||
import { | ||
setNewRelicCustomAttribute, | ||
unsetAllNewRelicQueryAttributes, | ||
waitForNewRelic, | ||
} from "src/utils/analyticsUtil"; | ||
|
||
jest.useFakeTimers(); | ||
const mockSetCustomAttribute = jest.fn(); | ||
|
||
describe("waitForNewRelic", () => { | ||
afterEach(() => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = undefined; | ||
jest.resetAllMocks(); | ||
}); | ||
it("returns true if new relic is found", async () => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = {}; | ||
const result = await waitForNewRelic(); | ||
expect(result).toEqual(true); | ||
}); | ||
it("when the timeout is reached returns false", async () => { | ||
// eslint-disable-next-line | ||
const resolution = new Promise<boolean>((resolve) => | ||
waitForNewRelic().then((result) => { | ||
resolve(result); | ||
}), | ||
); | ||
await jest.runAllTimersAsync(); | ||
|
||
const result = await resolution; | ||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe("setNewRelicCustomAttribute", () => { | ||
afterEach(() => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = undefined; | ||
jest.resetAllMocks(); | ||
}); | ||
it("does not attempt to call new relic if new relic does not exist", () => { | ||
setNewRelicCustomAttribute("key", "value"); | ||
expect(mockSetCustomAttribute).not.toHaveBeenCalled(); | ||
}); | ||
it("calls new relic with passed values, with key properly prefixed", () => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = { | ||
setCustomAttribute: mockSetCustomAttribute, | ||
}; | ||
|
||
setNewRelicCustomAttribute("key", "value"); | ||
expect(mockSetCustomAttribute).toHaveBeenCalledWith( | ||
"search_param_key", | ||
"value", | ||
); | ||
}); | ||
}); | ||
|
||
describe("unsetAllNewRelicQueryAttributes", () => { | ||
afterEach(() => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = undefined; | ||
jest.resetAllMocks(); | ||
}); | ||
it("does not attempt to call new relic if new relic does not exist", () => { | ||
unsetAllNewRelicQueryAttributes("key"); | ||
expect(mockSetCustomAttribute).not.toHaveBeenCalled(); | ||
}); | ||
it("calls new relic with passed values, with key properly prefixed", () => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
window.newrelic = { | ||
setCustomAttribute: mockSetCustomAttribute, | ||
}; | ||
|
||
unsetAllNewRelicQueryAttributes(); | ||
expect(mockSetCustomAttribute).toHaveBeenCalledTimes( | ||
validSearchQueryParamKeys.length, | ||
); | ||
validSearchQueryParamKeys.forEach((key) => { | ||
expect(mockSetCustomAttribute).toHaveBeenCalledWith( | ||
`search_param_${key}`, | ||
"", | ||
); | ||
}); | ||
}); | ||
}); |