Skip to content

Commit

Permalink
improve custom matcher and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Sep 9, 2023
1 parent 113d256 commit c28735a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 0 additions & 2 deletions app/routes/_auth+/auth.$provider.callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ test('if a user is not logged in, but the connection exists and they have enable
type: twoFAVerificationType,
target: userId,
redirectTo: '/',
remember: 'on',
})
searchParams.sort()
expect(response).toHaveRedirect(`/verify?${searchParams}`)
})

Expand Down
5 changes: 0 additions & 5 deletions app/routes/_auth+/verify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const codeQueryParam = 'code'
export const targetQueryParam = 'target'
export const typeQueryParam = 'type'
export const redirectToQueryParam = 'redirectTo'
export const rememberQueryParam = 'remember'
const types = ['onboarding', 'reset-password', 'change-email', '2fa'] as const
const VerificationTypeSchema = z.enum(types)
export type VerificationTypes = z.infer<typeof VerificationTypeSchema>
Expand Down Expand Up @@ -238,7 +237,6 @@ async function validateRequest(
export default function VerifyRoute() {
const data = useLoaderData<typeof loader>()
const [searchParams] = useSearchParams()
const remember = searchParams.get(rememberQueryParam)
const isPending = useIsPending()
const actionData = useActionData<typeof action>()
const type = VerificationTypeSchema.parse(searchParams.get(typeQueryParam))
Expand Down Expand Up @@ -312,9 +310,6 @@ export default function VerifyRoute() {
type: 'hidden',
})}
/>
{remember === 'on' ? (
<input name={rememberQueryParam} value="on" type="hidden" />
) : null}
<StatusButton
className="w-full"
status={isPending ? 'pending' : actionData?.status ?? 'idle'}
Expand Down
23 changes: 22 additions & 1 deletion tests/setup/custom-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,29 @@ expect.extend({
}
}

function toUrl(s?: string | null) {
s ??= ''
return s.startsWith('http')
? new URL(s)
: new URL(s, 'https://example.com')
}

function urlsMatch(u1: URL, u2: URL) {
const u1SP = new URL(u1).searchParams
u1SP.sort()
const u2SP = new URL(u2).searchParams
u2SP.sort()
return (
u1.origin === u2.origin &&
u1.pathname === u2.pathname &&
u1SP.toString() === u2SP.toString() &&
u1.hash === u2.hash
)
}

return {
pass: location === redirectTo,
pass:
location == redirectTo || urlsMatch(toUrl(location), toUrl(redirectTo)),
message: () =>
`Expected response to ${
this.isNot ? 'not ' : ''
Expand Down

0 comments on commit c28735a

Please sign in to comment.