Skip to content

Commit

Permalink
feat: Integrate API with page
Browse files Browse the repository at this point in the history
  • Loading branch information
chadbrokaw committed Dec 18, 2024
1 parent ed823a8 commit dae6f30
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 247 deletions.
99 changes: 99 additions & 0 deletions app/javascript/__tests__/__util__/accountUtils.tsx
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"
},
})
}
Loading

0 comments on commit dae6f30

Please sign in to comment.