mattermost-mobile/app/products/calls/calls_manager.test.ts
Claudio Costa 44bce6c319
[MM-59773] Add unit tests for Calls (#8480)
* Unit tests for app/products/calls/observers

* Unit tests for app/products/calls/actions

* Unit tests for app/products/calls/client

* Unit tests for app/products/calls/connection

* Unit tests for app/products/calls/connection/websocket_event_handlers.ts

* Unit tests for app/products/calls/connection/connection.ts

* Unit tests for app/products/calls/state/actions.ts

* Unit tests for app/products/calls/utils.ts

* Unit tests for app/products/calls/alerts.ts

* Unit tests for app/products/calls/calls_manager.ts

* Unit tests for app/products/calls/hooks.ts

* Factor out force logout error

* Reduce use of 'as jest.Mock'

* Add test case

* Use constants

* Better naming

* Fix types after merge

* Fix test
2025-01-21 12:02:03 -06:00

51 lines
1.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {AppState, Platform} from 'react-native';
import {callsOnAppStateChange} from '@calls/state';
import {CallsManager} from './calls_manager';
jest.mock('@calls/state', () => ({
callsOnAppStateChange: jest.fn(),
}));
describe('CallsManager', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(AppState, 'addEventListener');
});
afterEach(() => {
// Reset platform to its original value
Platform.OS = 'ios';
});
it('initializes AppState listeners correctly for iOS', () => {
Platform.OS = 'ios';
CallsManager.initialize();
expect(AppState.addEventListener).toHaveBeenCalledWith('change', callsOnAppStateChange);
expect(AppState.addEventListener).toHaveBeenCalledTimes(1);
});
it('initializes AppState listeners correctly for Android', () => {
Platform.OS = 'android';
CallsManager.initialize();
expect(AppState.addEventListener).toHaveBeenCalledWith('blur', expect.any(Function));
expect(AppState.addEventListener).toHaveBeenCalledWith('focus', expect.any(Function));
expect(AppState.addEventListener).toHaveBeenCalledTimes(2);
// Test that the blur callback calls callsOnAppStateChange with 'inactive'
const blurCallback = jest.mocked(AppState.addEventListener).mock.calls[0][1];
blurCallback('inactive');
expect(callsOnAppStateChange).toHaveBeenCalledWith('inactive');
// Test that the focus callback calls callsOnAppStateChange with 'active'
const focusCallback = jest.mocked(AppState.addEventListener).mock.calls[1][1];
focusCallback('active');
expect(callsOnAppStateChange).toHaveBeenCalledWith('active');
});
});