Skip to content

Commit

Permalink
Changing function signature to not need window
Browse files Browse the repository at this point in the history
  • Loading branch information
littlelazer committed Sep 27, 2024
1 parent 36da262 commit 6fc3414
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/network/__tests__/useExchangeCodeForToken.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AuthProvider } from 'src/utils/AuthContext'
import { asMock, currentAuthCredentials, userIsNotSignedIn, userIsSignedIn } from 'src/testHelpers'
import { faker } from '@faker-js/faker'
import { signInWithCode } from '../auth'
import { currentUrlSearchParams } from 'src/utils/currentUrlSearchParams'
import { currentUrlSearchParamsFor } from 'src/utils/currentUrlSearchParamsFor'
import { navigate } from 'gatsby'

jest.mock('src/network/auth', () => {
Expand All @@ -14,9 +14,9 @@ jest.mock('src/network/auth', () => {
}
})

jest.mock('src/utils/currentUrlSearchParams', () => {
jest.mock('src/utils/currentUrlSearchParamsFor', () => {
return {
currentUrlSearchParams: jest.fn(),
currentUrlSearchParamsFor: jest.fn(),
}
})

Expand Down Expand Up @@ -56,9 +56,9 @@ describe('useExchangeCodeForToken', () => {
ExpiresIn: faker.number.int({ min: 1000, max: 4000 }),
},
})
asMock(currentUrlSearchParams).mockReturnValue(new URLSearchParams(`code=${code}`))
asMock(currentUrlSearchParamsFor).mockReturnValue(code)
asMock(navigate).mockImplementation(async () => {
asMock(currentUrlSearchParams).mockReturnValue(new URLSearchParams())
asMock(currentUrlSearchParamsFor).mockReturnValue(null)
})
})

Expand Down Expand Up @@ -86,9 +86,7 @@ describe('useExchangeCodeForToken', () => {
kind: 'NOT_AUTHORIZED',
error: { name: 'Not authorized', message: 'not authorized' },
})
asMock(currentUrlSearchParams).mockReturnValue(
new URLSearchParams(`code=${faker.lorem.word()}`),
)
asMock(currentUrlSearchParamsFor).mockReturnValue(faker.lorem.word())
})

it('displays the error', async () => {
Expand All @@ -106,9 +104,7 @@ describe('useExchangeCodeForToken', () => {
beforeEach(() => {
asMock(navigate).mockClear()
userIsSignedIn()
asMock(currentUrlSearchParams).mockReturnValue(
new URLSearchParams(`code=${faker.lorem.word()}`),
)
asMock(currentUrlSearchParamsFor).mockReturnValue(faker.lorem.word())
})

it('does not try to sign in', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/network/useExchangeCodeForToken.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useState } from 'react'
import { useAuth } from 'src/utils/AuthContext'
import { currentUrlSearchParams } from 'src/utils/currentUrlSearchParams'
import { currentUrlSearchParamsFor } from 'src/utils/currentUrlSearchParamsFor'
import { signInWithCode } from './auth'
import { navigate } from 'gatsby'

export const useExchangeCodeForToken = () => {
const code: string | null = currentUrlSearchParams().get('code')
const code: string | null = currentUrlSearchParamsFor('code')
const [loading, setLoading] = useState(false)
const [auth, setAuth] = useAuth()
const [errorMessage, setErrorMessage] = useState('')
Expand Down
1 change: 0 additions & 1 deletion src/utils/currentUrlSearchParams.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/utils/currentUrlSearchParamsFor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const currentUrlSearchParamsFor = (key: string) => {
return typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get(key) : null
}

0 comments on commit 6fc3414

Please sign in to comment.