Skip to content
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
184 changes: 184 additions & 0 deletions src/core/hooks/useAppStateListener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { renderHook, act } from '@testing-library/react-native';
import { AppState } from 'react-native';

import { useAppStateListener } from './useAppStateListener';

describe('useAppStateListener', () => {
let mockListener: { remove: jest.Mock };
let addEventListenerSpy: jest.SpyInstance;
let currentStateGetter: jest.SpyInstance;

beforeEach(() => {
jest.clearAllMocks();
mockListener = { remove: jest.fn() };

// Spy on AppState methods
addEventListenerSpy = jest.spyOn(AppState, 'addEventListener').mockReturnValue(mockListener);

// Mock the currentState property
Object.defineProperty(AppState, 'currentState', {
get: jest.fn(() => 'active'),
configurable: true,
});
currentStateGetter = jest.spyOn(AppState, 'currentState', 'get');
});

afterEach(() => {
addEventListenerSpy.mockRestore();
currentStateGetter.mockRestore();
});

it('should return initial app state', () => {
// GIVEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());

// THEN it should return the current app state
expect(result.current).toBe('active');
});

it('should set up AppState listener on mount', () => {
// WHEN the hook is rendered
renderHook(() => useAppStateListener());

// THEN AppState.addEventListener should be called with correct parameters
expect(addEventListenerSpy).toHaveBeenCalledWith(
'change',
expect.any(Function)
);
});

it('should update state when app state changes to background', () => {
// GIVEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());

// Get the listener callback
const listenerCallback = addEventListenerSpy.mock.calls[0][1];

// WHEN app state changes to background
act(() => {
listenerCallback('background');
});

// THEN the hook should return the new state
expect(result.current).toBe('background');
});

it('should update state when app state changes to inactive', () => {
// GIVEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());

// Get the listener callback
const listenerCallback = addEventListenerSpy.mock.calls[0][1];

// WHEN app state changes to inactive
act(() => {
listenerCallback('inactive');
});

// THEN the hook should return the new state
expect(result.current).toBe('inactive');
});

it('should update state when app state changes back to active', () => {
// GIVEN the hook is rendered and state is initially background
const { result } = renderHook(() => useAppStateListener());
const listenerCallback = addEventListenerSpy.mock.calls[0][1];

act(() => {
listenerCallback('background');
});
expect(result.current).toBe('background');

// WHEN app state changes back to active
act(() => {
listenerCallback('active');
});

// THEN the hook should return active
expect(result.current).toBe('active');
});

it('should handle multiple state changes', () => {
// GIVEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());
const listenerCallback = addEventListenerSpy.mock.calls[0][1];

// WHEN multiple state changes occur
act(() => {
listenerCallback('background');
});
expect(result.current).toBe('background');

act(() => {
listenerCallback('inactive');
});
expect(result.current).toBe('inactive');

act(() => {
listenerCallback('active');
});
expect(result.current).toBe('active');
});

it('should clean up listener on unmount', () => {
// GIVEN the hook is rendered
const { unmount } = renderHook(() => useAppStateListener());

// WHEN the component unmounts
unmount();

// THEN the listener should be removed
expect(mockListener.remove).toHaveBeenCalledTimes(1);
});

it('should handle initial state with different AppState.currentState', () => {
// GIVEN AppState.currentState is set to background
currentStateGetter.mockReturnValue('background');

// WHEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());

// THEN it should return the background state
expect(result.current).toBe('background');
});

it('should handle initial state with inactive AppState.currentState', () => {
// GIVEN AppState.currentState is set to inactive
currentStateGetter.mockReturnValue('inactive');

// WHEN the hook is rendered
const { result } = renderHook(() => useAppStateListener());

// THEN it should return the inactive state
expect(result.current).toBe('inactive');
});

it('should not call addEventListener multiple times on re-renders', () => {
// GIVEN the hook is rendered
const { rerender } = renderHook(() => useAppStateListener());

// WHEN the component re-renders
rerender(() => useAppStateListener());
rerender(() => useAppStateListener());
Comment on lines +161 to +162
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rerender function expects no parameters, but arrow functions are being passed. This should be rerender() to trigger re-renders of the same hook.

Copilot uses AI. Check for mistakes.

// THEN addEventListener should only be called once
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
});

it('should maintain state consistency across re-renders', () => {
// GIVEN the hook is rendered and state changes
const { result, rerender } = renderHook(() => useAppStateListener());
const listenerCallback = addEventListenerSpy.mock.calls[0][1];

act(() => {
listenerCallback('background');
});
expect(result.current).toBe('background');

// WHEN the component re-renders
rerender(() => useAppStateListener());
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rerender function expects no parameters, but an arrow function is being passed. This should be rerender() to trigger a re-render of the same hook.

Suggested change
rerender(() => useAppStateListener());
rerender();

Copilot uses AI. Check for mistakes.

// THEN the state should remain consistent
expect(result.current).toBe('background');
});
});
Empty file removed src/hooks/index.ts
Empty file.
Loading