Skip to content
Closed
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
98 changes: 98 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,101 @@ describe('Auth0Provider', () => {
expect(screen.queryByText('__main_user__')).not.toBeInTheDocument();
});
});

describe('Auth0Provider - useMemo dependency behavior', () => {

afterEach(() => {
jest.clearAllMocks();
window.history.pushState({}, document.title, '/');
});

interface TestComponentProps {
clientId?: string;
audience?: string;
scope?: string;
}

const TestComponent: React.FC<TestComponentProps> = ({
clientId = '__test_client_id__',
audience = '__test_audience__',
scope = 'read:profile openid'
}) => (
<Auth0Provider
domain="__test_domain__"
clientId={clientId}
authorizationParams={{
audience,
scope
}}
>
<div>test</div>
</Auth0Provider>
);

it('should recreate Auth0Client when clientId changes', async () => {
const { rerender } = render(<TestComponent />);

// Initial render
expect(Auth0Client).toHaveBeenCalledTimes(1);
expect(Auth0Client).toHaveBeenLastCalledWith(
expect.objectContaining({
clientId: '__test_client_id__',
authorizationParams: {
audience: '__test_audience__',
scope: 'read:profile openid'
}
})
);

// Change clientId - should recreate client
rerender(<TestComponent clientId="__new_client_id__" />);
expect(Auth0Client).toHaveBeenCalledTimes(2);
expect(Auth0Client).toHaveBeenLastCalledWith(
expect.objectContaining({
clientId: '__new_client_id__',
authorizationParams: {
audience: '__test_audience__',
scope: 'read:profile openid'
}
})
);
});

it('should recreate Auth0Client when audience changes', async () => {
const { rerender } = render(<TestComponent />);

expect(Auth0Client).toHaveBeenCalledTimes(1);

// Change audience - should recreate client
rerender(<TestComponent audience="__new_audience__" />);
expect(Auth0Client).toHaveBeenCalledTimes(2);
expect(Auth0Client).toHaveBeenLastCalledWith(
expect.objectContaining({
clientId: '__test_client_id__',
authorizationParams: {
audience: '__new_audience__',
scope: 'read:profile openid'
}
})
);
});

it('should recreate Auth0Client when scope changes', async () => {
const { rerender } = render(<TestComponent />);

expect(Auth0Client).toHaveBeenCalledTimes(1);

// Change scope with multiple permissions - should recreate client
rerender(<TestComponent scope="read:users write:users admin:all" />);
expect(Auth0Client).toHaveBeenCalledTimes(2);
expect(Auth0Client).toHaveBeenLastCalledWith(
expect.objectContaining({
clientId: '__test_client_id__',
authorizationParams: {
audience: '__test_audience__',
scope: 'read:users write:users admin:all'
}
})
);
});
});
12 changes: 9 additions & 3 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
useMemo,
useReducer,
useRef,
useState,
} from 'react';
import {
Auth0Client,
Expand Down Expand Up @@ -140,8 +139,15 @@
context = Auth0Context,
...clientOpts
} = opts;
const [client] = useState(
() => new Auth0Client(toAuth0ClientOptions(clientOpts))
const client = useMemo(
() => new Auth0Client(toAuth0ClientOptions(clientOpts)),
[

Check warning on line 144 in src/auth0-provider.tsx

View workflow job for this annotation

GitHub Actions / Build Package

React Hook useMemo has a missing dependency: 'clientOpts'. Either include it or remove the dependency array

Check warning on line 144 in src/auth0-provider.tsx

View workflow job for this annotation

GitHub Actions / BrowserStack Tests

React Hook useMemo has a missing dependency: 'clientOpts'. Either include it or remove the dependency array
clientOpts.domain,
clientOpts.clientId,
clientOpts.authorizationParams?.audience,
clientOpts.authorizationParams?.scope,
clientOpts.authorizationParams?.redirect_uri,
]
);
const [state, dispatch] = useReducer(reducer<TUser>, initialAuthState as AuthState<TUser>);
const didInitialise = useRef(false);
Expand Down
Loading