Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript support for multiple When children without wrapping in a fragment #67

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/react/when.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Policy from '../policy'
import { usePundit } from './pundit-provider'

interface WhenProps {
children: ReactNode | null
children: ReactNode | ReactNode[] | null
can: string
policy?: Policy
user?: unknown
Expand All @@ -16,7 +16,7 @@ export default function When({
policy,
user,
record,
}: WhenProps): ReactNode | null {
}: WhenProps): ReactNode | ReactNode[] | null {
const { policy: hookPolicy } = usePundit()
const paramPolicy = policy?.copy(user, record)
const canPerformAction = paramPolicy
Expand Down
10 changes: 4 additions & 6 deletions test/react/pundit-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ describe('<PunditProvider />', () => {
render(
<PunditProvider policy={policy}>
<When can="view">
<>
<button type="button">View</button>
<When can="edit">
<button type="button">Edit</button>
</When>
</>
<button type="button">View</button>
<When can="edit">
<button type="button">Edit</button>
</When>
</When>
</PunditProvider>
)
Expand Down
11 changes: 11 additions & 0 deletions test/react/when.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ describe('<When />', () => {
expect(screen.queryByText('View')).toBeInTheDocument()
})

it('authorises multiple children within a single <When /> element', () => {
render(
<When can="view" policy={policy}>
<button type="button">View 1</button>
<button type="button">View 2</button>
</When>
)
expect(screen.queryByText('View 1')).toBeInTheDocument()
expect(screen.queryByText('View 2')).toBeInTheDocument()
})

it('does not display <When /> child when action is forbidden using "policy" param', () => {
render(
<When can="edit" policy={policy}>
Expand Down