-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(elements): Add more unit tests (#2896)
* chore(elements): Move tests to __tests__ folder * feat(shared): Add some more URL helpers * chore(elements): Add more tests * chore(clerk-js): Do not fail publint
- Loading branch information
Showing
13 changed files
with
372 additions
and
9 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,5 @@ | ||
--- | ||
'@clerk/shared': minor | ||
--- | ||
|
||
Add `withoutTrailingSlash()`, `hasLeadingSlash()`, `withoutLeadingSlash()`, `withLeadingSlash()`, and `cleanDoubleSlashes()` to `@clerk/shared/url`. |
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
2 changes: 1 addition & 1 deletion
2
...es/sign-up/utils/fields-to-params.test.ts → .../utils/__tests__/fields-to-params.test.ts
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
2 changes: 1 addition & 1 deletion
2
...c/internals/machines/utils/assert.test.ts → ...s/machines/utils/__tests__/assert.test.ts
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
2 changes: 1 addition & 1 deletion
2
...src/internals/machines/utils/next.test.ts → ...als/machines/utils/__tests__/next.test.ts
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
2 changes: 1 addition & 1 deletion
2
...ternals/machines/utils/strategies.test.ts → ...chines/utils/__tests__/strategies.test.ts
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
66 changes: 66 additions & 0 deletions
66
packages/elements/src/react/hooks/__tests__/use-active-states.hook.test.ts
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 { act, renderHook } from '@testing-library/react'; | ||
import { createActor, createMachine } from 'xstate'; | ||
|
||
import { useActiveStates } from '../use-active-states.hook'; | ||
|
||
describe('useActiveStates', () => { | ||
const machine = createMachine({ | ||
id: 'toggle', | ||
initial: 'inactive', | ||
states: { | ||
inactive: { | ||
on: { toggle: 'active' }, | ||
}, | ||
active: { | ||
on: { toggle: 'inactive' }, | ||
}, | ||
reset: { | ||
on: { toggle: 'inactive' }, | ||
}, | ||
}, | ||
}); | ||
|
||
const actor = createActor(machine).start(); | ||
|
||
it('should return false for invalid states param', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, 1 as any)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
describe('single state', () => { | ||
it('should return true if state is active', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, 'inactive')); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should return false if state is not active', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, 'active')); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('multiple states', () => { | ||
it('should return true if any state is active', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, ['inactive', 'active'])); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should return false if no state is active', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, ['active', 'reset'])); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('should return true if valid active state switches', () => { | ||
const { result } = renderHook(() => useActiveStates(actor, ['inactive', 'active'])); | ||
|
||
expect(result.current).toBe(true); | ||
act(() => actor.send({ type: 'toggle' })); | ||
expect(result.current).toBe(true); | ||
}); | ||
}); | ||
}); |
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
30 changes: 30 additions & 0 deletions
30
packages/elements/src/react/hooks/__tests__/use-focus.hook.test.ts
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,30 @@ | ||
import { fireEvent, renderHook } from '@testing-library/react'; | ||
|
||
import { useFocus } from '../use-focus.hook'; | ||
|
||
describe('useFocus', () => { | ||
it('should set isFocused to true when input is focused', () => { | ||
const inputRef = { current: document.createElement('input') }; | ||
const { result } = renderHook(() => useFocus(inputRef)); | ||
|
||
fireEvent.focus(inputRef.current); | ||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should set isFocused to false when input is blurred', () => { | ||
const inputRef = { current: document.createElement('input') }; | ||
const { result } = renderHook(() => useFocus(inputRef)); | ||
|
||
fireEvent.focus(inputRef.current); | ||
expect(result.current).toBe(true); | ||
fireEvent.blur(inputRef.current); | ||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('should return false when inputRef is null', () => { | ||
const inputRef = { current: null }; | ||
const { result } = renderHook(() => useFocus(inputRef)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
107 changes: 107 additions & 0 deletions
107
packages/elements/src/react/router/__tests__/router.test.ts
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,107 @@ | ||
import { createClerkRouter } from '../router'; | ||
|
||
describe('createClerkRouter', () => { | ||
const mockRouter = { | ||
pathname: jest.fn(), | ||
searchParams: jest.fn(), | ||
push: jest.fn(), | ||
replace: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('creates a ClerkRouter instance with the correct base path', () => { | ||
const oneBasePath = '/app'; | ||
const twoBasePath = 'app'; | ||
const threeBasePath = 'app/'; | ||
const one = createClerkRouter(mockRouter, oneBasePath); | ||
const two = createClerkRouter(mockRouter, twoBasePath); | ||
const three = createClerkRouter(mockRouter, threeBasePath); | ||
|
||
expect(one.basePath).toBe(oneBasePath); | ||
expect(two.basePath).toBe('/app'); | ||
expect(three.basePath).toBe('/app'); | ||
}); | ||
|
||
it('matches the path correctly', () => { | ||
const path = '/dashboard'; | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.pathname.mockReturnValue('/app/dashboard'); | ||
|
||
expect(clerkRouter.match(path)).toBe(true); | ||
}); | ||
|
||
it('throws an error when no path is provided', () => { | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
expect(() => { | ||
clerkRouter.match(); | ||
}).toThrow('[clerk] router.match() requires either a path to match, or the index flag must be set to true.'); | ||
}); | ||
|
||
it('creates a child router with the correct base path', () => { | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
const childRouter = clerkRouter.child('dashboard'); | ||
|
||
expect(childRouter.basePath).toBe('/app/dashboard'); | ||
}); | ||
|
||
it('pushes the correct destination URL ', () => { | ||
const path = '/app/dashboard'; | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.searchParams.mockImplementation(() => new URLSearchParams('')); | ||
clerkRouter.push(path); | ||
|
||
expect(mockRouter.push).toHaveBeenCalledWith('/app/dashboard'); | ||
}); | ||
|
||
it('replaces the correct destination URL', () => { | ||
const path = '/app/dashboard'; | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.searchParams.mockImplementation(() => new URLSearchParams('')); | ||
clerkRouter.replace(path); | ||
|
||
expect(mockRouter.replace).toHaveBeenCalledWith('/app/dashboard'); | ||
}); | ||
|
||
it('pushes the correct destination URL with preserved query parameters', () => { | ||
const path = '/app/dashboard'; | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.searchParams.mockImplementation(() => new URLSearchParams('after_sign_in_url=foobar&foo=bar')); | ||
clerkRouter.push(path); | ||
|
||
expect(mockRouter.push).toHaveBeenCalledWith('/app/dashboard?after_sign_in_url=foobar'); | ||
}); | ||
|
||
it('replaces the correct destination URL with preserved query parameters', () => { | ||
const path = '/app/dashboard'; | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.searchParams.mockImplementation(() => new URLSearchParams('after_sign_in_url=foobar&foo=bar')); | ||
clerkRouter.replace(path); | ||
|
||
expect(mockRouter.replace).toHaveBeenCalledWith('/app/dashboard?after_sign_in_url=foobar'); | ||
}); | ||
|
||
it('returns the correct pathname', () => { | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.pathname.mockReturnValue('/app/dashboard'); | ||
|
||
expect(clerkRouter.pathname()).toBe('/app/dashboard'); | ||
}); | ||
|
||
it('returns the correct searchParams', () => { | ||
const clerkRouter = createClerkRouter(mockRouter, '/app'); | ||
|
||
mockRouter.searchParams.mockImplementation(() => new URLSearchParams('foo=bar')); | ||
|
||
expect(clerkRouter.searchParams().get('foo')).toEqual('bar'); | ||
}); | ||
}); |
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.