-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
10 changed files
with
301 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { redirectWithFallback, errorResponseWithFallback } from '../src/utils.js'; | ||
|
||
describe('utils', () => { | ||
afterEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
describe('redirectWithFallback', () => { | ||
it('uses NextResponse.redirect when available', () => { | ||
const redirectUrl = 'https://example.com'; | ||
const mockRedirect = jest.fn().mockReturnValue('redirected'); | ||
const originalRedirect = NextResponse.redirect; | ||
|
||
NextResponse.redirect = mockRedirect; | ||
|
||
const result = redirectWithFallback(redirectUrl); | ||
|
||
expect(mockRedirect).toHaveBeenCalledWith(redirectUrl); | ||
expect(result).toBe('redirected'); | ||
|
||
NextResponse.redirect = originalRedirect; | ||
}); | ||
|
||
it('falls back to standard Response when NextResponse exists but redirect is undefined', async () => { | ||
const redirectUrl = 'https://example.com'; | ||
|
||
jest.resetModules(); | ||
|
||
jest.mock('next/server', () => ({ | ||
NextResponse: { | ||
// exists but has no redirect method | ||
}, | ||
})); | ||
|
||
const { redirectWithFallback } = await import('../src/utils.js'); | ||
|
||
const result = redirectWithFallback(redirectUrl); | ||
|
||
expect(result).toBeInstanceOf(Response); | ||
expect(result.status).toBe(307); | ||
expect(result.headers.get('Location')).toBe(redirectUrl); | ||
}); | ||
|
||
it('falls back to standard Response when NextResponse is undefined', async () => { | ||
const redirectUrl = 'https://example.com'; | ||
|
||
jest.resetModules(); | ||
|
||
// Mock with undefined NextResponse | ||
jest.mock('next/server', () => ({ | ||
NextResponse: undefined, | ||
})); | ||
|
||
const { redirectWithFallback } = await import('../src/utils.js'); | ||
|
||
const result = redirectWithFallback(redirectUrl); | ||
|
||
expect(result).toBeInstanceOf(Response); | ||
expect(result.status).toBe(307); | ||
expect(result.headers.get('Location')).toBe(redirectUrl); | ||
}); | ||
}); | ||
|
||
describe('errorResponseWithFallback', () => { | ||
const errorBody = { | ||
error: { | ||
message: 'Test error', | ||
description: 'Test description', | ||
}, | ||
}; | ||
|
||
it('uses NextResponse.json when available', () => { | ||
const mockJson = jest.fn().mockReturnValue('error json response'); | ||
NextResponse.json = mockJson; | ||
|
||
const result = errorResponseWithFallback(errorBody); | ||
|
||
expect(mockJson).toHaveBeenCalledWith(errorBody, { status: 500 }); | ||
expect(result).toBe('error json response'); | ||
}); | ||
|
||
it('falls back to standard Response when NextResponse is not available', () => { | ||
const originalJson = NextResponse.json; | ||
|
||
// @ts-expect-error - This is to test the fallback | ||
delete NextResponse.json; | ||
|
||
const result = errorResponseWithFallback(errorBody); | ||
|
||
expect(result).toBeInstanceOf(Response); | ||
expect(result.status).toBe(500); | ||
expect(result.headers.get('Content-Type')).toBe('application/json'); | ||
|
||
NextResponse.json = originalJson; | ||
}); | ||
|
||
it('falls back to standard Response when NextResponse exists but json is undefined', async () => { | ||
jest.resetModules(); | ||
|
||
jest.mock('next/server', () => ({ | ||
NextResponse: { | ||
// exists but has no json method | ||
}, | ||
})); | ||
|
||
const { errorResponseWithFallback } = await import('../src/utils.js'); | ||
|
||
const result = errorResponseWithFallback(errorBody); | ||
|
||
expect(result).toBeInstanceOf(Response); | ||
expect(result.status).toBe(500); | ||
expect(result.headers.get('Content-Type')).toBe('application/json'); | ||
}); | ||
|
||
it('falls back to standard Response when NextResponse is undefined', async () => { | ||
jest.resetModules(); | ||
|
||
jest.mock('next/server', () => ({ | ||
NextResponse: undefined, | ||
})); | ||
|
||
const { errorResponseWithFallback } = await import('../src/utils.js'); | ||
|
||
const result = errorResponseWithFallback(errorBody); | ||
|
||
expect(result).toBeInstanceOf(Response); | ||
expect(result.status).toBe(500); | ||
expect(result.headers.get('Content-Type')).toBe('application/json'); | ||
}); | ||
}); | ||
}); |
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,66 @@ | ||
import { WorkOS } from '@workos-inc/node'; | ||
import { workos, VERSION } from '../src/workos.js'; | ||
|
||
describe('workos', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('initializes WorkOS with the correct configuration', () => { | ||
// Extracting the config to avoid a circular dependency error | ||
const workosConfig = { | ||
apiHostname: workos.options.apiHostname, | ||
https: workos.options.https, | ||
port: workos.options.port, | ||
appInfo: workos.options.appInfo, | ||
}; | ||
|
||
expect(workosConfig).toEqual({ | ||
apiHostname: undefined, | ||
https: true, | ||
port: undefined, | ||
appInfo: { | ||
name: 'authkit/nextjs', | ||
version: VERSION, | ||
}, | ||
}); | ||
}); | ||
|
||
it('exports a WorkOS instance', () => { | ||
expect(workos).toBeInstanceOf(WorkOS); | ||
}); | ||
|
||
describe('with custom environment variables', () => { | ||
const originalEnv = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it('uses custom API hostname when provided', async () => { | ||
process.env.WORKOS_API_HOSTNAME = 'custom.workos.com'; | ||
const { workos: customWorkos } = await import('../src/workos.js'); | ||
|
||
expect(customWorkos.options.apiHostname).toEqual('custom.workos.com'); | ||
}); | ||
|
||
it('uses custom HTTPS setting when provided', async () => { | ||
process.env.WORKOS_API_HTTPS = 'false'; | ||
const { workos: customWorkos } = await import('../src/workos.js'); | ||
|
||
expect(customWorkos.options.https).toEqual(false); | ||
}); | ||
|
||
it('uses custom port when provided', async () => { | ||
process.env.WORKOS_API_PORT = '8080'; | ||
const { workos: customWorkos } = await import('../src/workos.js'); | ||
|
||
expect(customWorkos.options.port).toEqual(8080); | ||
}); | ||
}); | ||
}); |
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.