Skip to content

Commit f95a20d

Browse files
test: add deprecateRedirectUri utility tests to meet 100% coverage
1 parent d094e14 commit f95a20d

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

__tests__/auth-reducer.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('reducer', () => {
1919
it('should initialise when not authenticated', async () => {
2020
const payload = {
2121
isAuthenticated: false,
22+
user: undefined,
2223
};
2324
expect(
2425
reducer(initialAuthState, { type: 'INITIALISED', ...payload })

__tests__/utils.test.tsx

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
import { hasAuthParams, loginError, tokenError } from '../src/utils';
1+
import { hasAuthParams, loginError, tokenError, deprecateRedirectUri } from '../src/utils';
22
import { OAuthError } from '../src/errors';
33

4+
// Define interfaces for testing deprecateRedirectUri function
5+
interface TestOptionsWithRedirectUri {
6+
redirectUri?: string;
7+
authorizationParams?: {
8+
redirect_uri?: string;
9+
scope?: string;
10+
};
11+
}
12+
13+
interface TestOptionsWithAuthorizationParams {
14+
authorizationParams: {
15+
redirectUri?: string;
16+
redirect_uri?: string;
17+
scope?: string;
18+
};
19+
}
20+
21+
interface TestOptionsWithBothRedirectUri {
22+
redirectUri?: string;
23+
authorizationParams: {
24+
scope: string;
25+
redirect_uri?: string;
26+
};
27+
}
28+
429
describe('utils hasAuthParams', () => {
530
it('should not recognise only the code param', async () => {
631
['?code=1', '?foo=1&code=2', '?code=1&foo=2'].forEach((search) =>
@@ -62,3 +87,75 @@ describe('utils error', () => {
6287
}).toThrowError('Login failed');
6388
});
6489
});
90+
91+
describe('utils deprecateRedirectUri', () => {
92+
let consoleSpy: jest.SpyInstance;
93+
94+
beforeEach(() => {
95+
consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
96+
});
97+
98+
afterEach(() => {
99+
consoleSpy.mockRestore();
100+
});
101+
102+
it('should handle options with redirectUri', () => {
103+
const options: TestOptionsWithRedirectUri = {
104+
redirectUri: 'https://example.com/callback',
105+
};
106+
107+
deprecateRedirectUri(options);
108+
109+
expect(consoleSpy).toHaveBeenCalledWith(
110+
'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version'
111+
);
112+
expect(options.authorizationParams?.redirect_uri).toBe('https://example.com/callback');
113+
expect(options.redirectUri).toBeUndefined();
114+
});
115+
116+
it('should handle options with authorizationParams.redirectUri', () => {
117+
const options: TestOptionsWithAuthorizationParams = {
118+
authorizationParams: {
119+
redirectUri: 'https://example.com/callback',
120+
},
121+
};
122+
123+
deprecateRedirectUri(options);
124+
125+
expect(consoleSpy).toHaveBeenCalledWith(
126+
'Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version'
127+
);
128+
expect(options.authorizationParams.redirect_uri).toBe('https://example.com/callback');
129+
expect(options.authorizationParams.redirectUri).toBeUndefined();
130+
});
131+
132+
it('should handle options with both redirectUri and existing authorizationParams', () => {
133+
const options: TestOptionsWithBothRedirectUri = {
134+
redirectUri: 'https://example.com/callback',
135+
authorizationParams: {
136+
scope: 'openid profile',
137+
},
138+
};
139+
140+
deprecateRedirectUri(options);
141+
142+
expect(options.authorizationParams.redirect_uri).toBe('https://example.com/callback');
143+
expect(options.authorizationParams.scope).toBe('openid profile');
144+
expect(options.redirectUri).toBeUndefined();
145+
});
146+
147+
it('should handle undefined options', () => {
148+
deprecateRedirectUri(undefined);
149+
expect(consoleSpy).not.toHaveBeenCalled();
150+
});
151+
152+
it('should handle options without redirectUri properties', () => {
153+
const options = {
154+
domain: 'example.auth0.com',
155+
clientId: 'client-id',
156+
};
157+
158+
deprecateRedirectUri(options);
159+
expect(consoleSpy).not.toHaveBeenCalled();
160+
});
161+
});

src/auth0-provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUs
198198
const user = await client.getUser();
199199
dispatch({ type: 'LOGIN_POPUP_COMPLETE', user });
200200
},
201-
[client]
201+
[client, handleError]
202202
);
203203

204204
const logout = useCallback(

0 commit comments

Comments
 (0)