-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for useIsOAuth2Enabled
- Loading branch information
Thisyahlen
committed
Nov 21, 2024
1 parent
244ce2f
commit f964642
Showing
1 changed file
with
88 additions
and
0 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,88 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useIsOAuth2Enabled } from '../useIsOAuth2Enabled'; | ||
import * as Constants from '../../constants/'; | ||
|
||
jest.mock('../../constants/', () => ({ | ||
getServerInfo: jest.fn(), | ||
})); | ||
|
||
describe('useIsOAuth2Enabled', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return false if OAuth2EnabledAppsInitialised is false', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({ appId: 12345 }); | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled([], false)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('should return false if OAuth2EnabledApps is empty', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({ appId: 12345 }); | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled([], true)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('should return false if appId is not in the enabled_for list', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({ appId: 98765 }); | ||
|
||
const OAuth2EnabledApps = [ | ||
{ | ||
enabled_for: [12345, 23456], | ||
}, | ||
]; | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled(OAuth2EnabledApps, true)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it('should return true if appId is in the enabled_for list', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({ appId: 12345 }); | ||
|
||
const OAuth2EnabledApps = [ | ||
{ | ||
enabled_for: [12345, 23456], | ||
}, | ||
]; | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled(OAuth2EnabledApps, true)); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should always check the most recent enabled_for list', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({ appId: 67890 }); | ||
|
||
const OAuth2EnabledApps = [ | ||
{ | ||
enabled_for: [12345], | ||
}, | ||
{ | ||
enabled_for: [67890], | ||
}, | ||
]; | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled(OAuth2EnabledApps, true)); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should handle edge case with no appId', () => { | ||
(Constants.getServerInfo as jest.Mock).mockReturnValue({}); | ||
|
||
const OAuth2EnabledApps = [ | ||
{ | ||
enabled_for: [12345], | ||
}, | ||
]; | ||
|
||
const { result } = renderHook(() => useIsOAuth2Enabled(OAuth2EnabledApps, true)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |