-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(userToken): accepting provided userToken (#64)
* feat(userToken): accepting provided userToken * fix(eventName): remove unecessary eventName for Search functions * fix(userToken): set userToken as optional * fix(uuid): prettier
- Loading branch information
Showing
15 changed files
with
153 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,65 @@ | ||
import { userToken } from "../_cookieUtils"; | ||
jest.mock("../utils/index", () => ({ | ||
createUUID: jest.fn(() => "mock-uuid") | ||
import AlgoliaInsights from "../insights"; | ||
import { createUUID } from "../utils/uuid"; | ||
|
||
jest.mock("../utils/uuid", () => ({ | ||
createUUID: jest.fn() | ||
})); | ||
|
||
const credentials = { | ||
apiKey: "test", | ||
applicationID: "test", | ||
cookieDuration: 10 * 24 * 3600 * 1000 // 10 days | ||
}; | ||
|
||
describe("cookieUtils", () => { | ||
// FIXME document.cookie should be fully mocked | ||
// getCookie and setCookie should be put in utils and mocked in this test | ||
describe("userToken", () => { | ||
it("should create a cookie with a UUID", () => { | ||
delete document.cookie; | ||
userToken(null, 1000); | ||
expect(document.cookie).toBe("_ALGOLIA=mock-uuid"); | ||
beforeEach(() => { | ||
AlgoliaInsights.init(credentials); | ||
createUUID.mockReset(); | ||
createUUID | ||
.mockReturnValueOnce("mock-uuid-1") | ||
.mockReturnValueOnce("mock-uuid-2") | ||
.mockReturnValue("mock-uuid-2+"); | ||
// clear cookies | ||
document.cookie = "_ALGOLIA=;expires=Thu, 01-Jan-1970 00:00:01 GMT;"; | ||
}); | ||
describe("setUserToken", () => { | ||
describe("ANONYMOUS_USER_TOKEN", () => { | ||
it("should create a cookie with a UUID", () => { | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
}); | ||
it("should reuse previously created UUID", () => { | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
}); | ||
it("should not reuse UUID from an expired cookie", () => { | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
// set cookie as expired | ||
document.cookie = "_ALGOLIA=;expires=Thu, 01-Jan-1970 00:00:01 GMT;"; | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-2"); | ||
}); | ||
}); | ||
describe("provided userToken", () => { | ||
it("should not create a cookie with provided userToken", () => { | ||
AlgoliaInsights.setUserToken("007"); | ||
expect(document.cookie).toBe(""); | ||
}); | ||
it("create a anonymous cookie when switching from provided userToken to anonymous", () => { | ||
AlgoliaInsights.setUserToken("007"); | ||
expect(document.cookie).toBe(""); | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
}); | ||
it("should preserve the cookie with same uuid when userToken provided after anonymous", () => { | ||
AlgoliaInsights.setUserToken(AlgoliaInsights.ANONYMOUS_USER_TOKEN); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
AlgoliaInsights.setUserToken("007"); | ||
expect(document.cookie).toBe("_ALGOLIA=anonymous-mock-uuid-1"); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.