-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed823a8
commit dae6f30
Showing
6 changed files
with
304 additions
and
247 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from "react" | ||
import UserContext, { ContextProps } from "../../authentication/context/UserContext" | ||
import { User } from "../../authentication/user" | ||
|
||
export const mockProfileStub: User = { | ||
uid: "abc123", | ||
email: "[email protected]", | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
DOB: "1999-01-01", | ||
firstName: "FirstName", | ||
lastName: "LastName", | ||
middleName: "MiddleName", | ||
} | ||
|
||
export const setupUserContext = ({ | ||
loggedIn, | ||
setUpMockLocation = true, | ||
saveProfileMock = jest.fn(), | ||
mockProfile = mockProfileStub, | ||
}: { | ||
loggedIn: boolean | ||
setUpMockLocation?: boolean | ||
saveProfileMock?: jest.Mock | ||
mockProfile?: ContextProps["profile"] | ||
}) => { | ||
const originalUseContext = React.useContext | ||
const originalLocation = window.location | ||
const mockContextValue: ContextProps = { | ||
profile: loggedIn ? mockProfile : undefined, | ||
signIn: jest.fn(), | ||
signOut: jest.fn(), | ||
timeOut: jest.fn(), | ||
saveProfile: saveProfileMock || jest.fn(), | ||
loading: false, | ||
initialStateLoaded: true, | ||
} | ||
|
||
jest.spyOn(React, "useContext").mockImplementation((context) => { | ||
if (context === UserContext) { | ||
return mockContextValue | ||
} | ||
return originalUseContext(context) | ||
}) | ||
|
||
if (!loggedIn && setUpMockLocation) { | ||
// Allows for a redirect to the Sign In page | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (window as any)?.location | ||
;(window as Window).location = { | ||
...originalLocation, | ||
href: "http://dahlia.com", | ||
assign: jest.fn(), | ||
replace: jest.fn(), | ||
reload: jest.fn(), | ||
toString: jest.fn(), | ||
} | ||
} | ||
|
||
return originalUseContext | ||
} | ||
|
||
export const setupLocationAndRouteMock = () => { | ||
const originalLocation = window.location | ||
|
||
const customLocation = { | ||
...originalLocation, | ||
href: "http://dahlia.com", | ||
assign: jest.fn(), | ||
replace: jest.fn(), | ||
reload: jest.fn(), | ||
toString: jest.fn(), | ||
} | ||
|
||
Object.defineProperty(window, "location", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: customLocation, | ||
}) | ||
|
||
// Redefine the href setter to resolve relative URLs | ||
Object.defineProperty(window.location, "href", { | ||
configurable: true, | ||
enumerable: true, | ||
set: function (href: string) { | ||
const base = "http://dahlia.com" | ||
try { | ||
const newUrl = new URL(href, base) | ||
this._href = newUrl.href | ||
} catch { | ||
this._href = href | ||
} | ||
}, | ||
get: function () { | ||
return this._href || "http://dahlia.com" | ||
}, | ||
}) | ||
} |
Oops, something went wrong.