mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-06 10:50:13 +00:00
* 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>
69 lines
2.3 KiB
TypeScript
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');
|
|
});
|
|
});
|