Skip to content

Commit

Permalink
chore: add test for useIsOAuth2Enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisyahlen committed Nov 21, 2024
1 parent 244ce2f commit f964642
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/hooks/__tests__/useIsOAuth2Enabled.spec.ts
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);
});
});

0 comments on commit f964642

Please sign in to comment.