mattermost-mobile-upstream/app/utils/timezone.test.ts
Scott Bishel 4a11b7c956
Some checks are pending
ci / lint-typecheck (push) Waiting to run
ci / detox-lint-typecheck (push) Waiting to run
ci / test (push) Waiting to run
FIX: Correct Android minute interval and UTC+offset date display in datetime fields (#10202)
* fix android datetime issues

* fix android date/time tests

* lint fix, review fix

* fix tests

* more tests

* another test fix

* fixing test

* fix tests

* fix tests

* fix tests

* fix tests

* remove test

* make test use android explicitly

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-10-22 15:12:27 -05:00

69 lines
2.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getTimeZone} from 'react-native-localize';
import {getDeviceTimezone, getSupportedDeviceTimezone} from './timezone';
// Mocking react-native-localize getTimeZone function
jest.mock('react-native-localize', () => ({
getTimeZone: jest.fn(),
}));
describe('getDeviceTimezone function', () => {
afterEach(() => {
jest.clearAllMocks(); // Clear all mock calls after each test
});
it('should return the device timezone', () => {
// Mock getTimeZone to return a specific timezone
const mockTimeZone = 'America/New_York';
(getTimeZone as jest.Mock).mockReturnValue(mockTimeZone);
// Call the function
const result = getDeviceTimezone();
// Expect getTimeZone to have been called once
expect(getTimeZone).toHaveBeenCalledTimes(1);
// Expect the result to be the mocked timezone
expect(result).toEqual(mockTimeZone);
});
});
describe('getSupportedDeviceTimezone', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should return the device timezone when Intl can format with it', () => {
(getTimeZone as jest.Mock).mockReturnValue('Asia/Yekaterinburg');
expect(getSupportedDeviceTimezone()).toBe('Asia/Yekaterinburg');
});
it('should return undefined when the platform reports a zone Intl rejects', () => {
(getTimeZone as jest.Mock).mockReturnValue('World/Somewhere');
expect(getSupportedDeviceTimezone()).toBeUndefined();
});
it('should re-evaluate when the device timezone changes', () => {
(getTimeZone as jest.Mock).mockReturnValue('World/Somewhere');
expect(getSupportedDeviceTimezone()).toBeUndefined();
(getTimeZone as jest.Mock).mockReturnValue('America/New_York');
expect(getSupportedDeviceTimezone()).toBe('America/New_York');
});
it('should return the last valid timezone when getTimeZone throws', () => {
(getTimeZone as jest.Mock).mockReturnValue('America/New_York');
expect(getSupportedDeviceTimezone()).toBe('America/New_York');
(getTimeZone as jest.Mock).mockImplementation(() => {
throw new Error('native call failed');
});
expect(getSupportedDeviceTimezone()).toBe('America/New_York');
});
});