-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
101 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as hubspotUtil from './hubspot' | ||
import * as sleepUtil from './sleep' | ||
|
||
const { getHubspotHiddenFields, waitForHubspotCookie } = hubspotUtil | ||
|
||
let cookieValue = '' | ||
Object.defineProperty(document, 'cookie', { | ||
get: () => cookieValue, | ||
}) | ||
|
||
describe('#getHubspotHiddenFields', () => { | ||
const title = 'my page title' | ||
const href = 'http://my-url' | ||
const utk = 'foobar' | ||
|
||
Object.defineProperty(window, 'location', { value: { href } }) | ||
Object.defineProperty(document, 'title', { value: title }) | ||
|
||
it('should return correct hidden fields', () => { | ||
cookieValue = `foo=bar; hubspotutk=${utk}; bar=foo;` | ||
expect(getHubspotHiddenFields()).toEqual({ | ||
hubspot_page_name: title, | ||
hubspot_page_url: href, | ||
hubspot_utk: utk, | ||
}) | ||
}) | ||
|
||
it('should return undefined utk value', () => { | ||
cookieValue = 'notahubspotutk=bar;' | ||
expect(getHubspotHiddenFields()).toEqual({ | ||
hubspot_page_name: title, | ||
hubspot_page_url: href, | ||
hubspot_utk: undefined, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#waitForHubspotCookie', () => { | ||
const sleepSpy = jest.spyOn(sleepUtil, 'sleep') | ||
const cookieSpy = jest.spyOn(hubspotUtil, 'getHubspotCookieValue') | ||
|
||
beforeEach(() => { | ||
sleepSpy.mockReset() | ||
cookieSpy.mockReset() | ||
}) | ||
|
||
it('should wait until the cookie is set', async () => { | ||
sleepSpy.mockResolvedValue() | ||
cookieSpy | ||
.mockReturnValueOnce(undefined) | ||
.mockReturnValueOnce(undefined) | ||
.mockReturnValueOnce(undefined) | ||
.mockReturnValueOnce('utk') | ||
|
||
await waitForHubspotCookie() | ||
expect(sleepSpy).toHaveBeenCalledTimes(3) | ||
expect(cookieSpy).toHaveBeenCalledTimes(4) | ||
}) | ||
|
||
it('should retry maximum of 10 times before continuing without no cookie', async () => { | ||
sleepSpy.mockResolvedValue() | ||
cookieSpy.mockReturnValue(undefined) | ||
|
||
await waitForHubspotCookie() | ||
|
||
expect(sleepSpy).toHaveBeenCalledTimes(10) | ||
expect(cookieSpy).toHaveBeenCalledTimes(11) | ||
}) | ||
}) |
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,12 @@ | ||
import { sleep } from './sleep' | ||
|
||
jest.useFakeTimers() | ||
jest.spyOn(global, 'setTimeout') | ||
describe('#sleep', () => { | ||
it('should wait for 5 seconds', () => { | ||
sleep(5000) | ||
|
||
expect(setTimeout).toHaveBeenCalledTimes(1) | ||
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 5000) | ||
}) | ||
}) |
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,3 @@ | ||
export const sleep = (ms: number): Promise<void> => { | ||
return new Promise((resolve) => setTimeout(resolve, ms)) | ||
} |