|
1 | 1 | import * as ErrorUtils from '@src/libs/ErrorUtils'; |
| 2 | +import DateUtils from '@src/libs/DateUtils'; |
2 | 3 | import type {Errors} from '@src/types/onyx/OnyxCommon'; |
3 | 4 |
|
| 5 | +// Mock DateUtils |
| 6 | +jest.mock('@src/libs/DateUtils'); |
| 7 | + |
4 | 8 | describe('ErrorUtils', () => { |
5 | 9 | test('should add a new error message for a given inputID', () => { |
6 | 10 | const errors: Errors = {}; |
@@ -62,4 +66,88 @@ describe('ErrorUtils', () => { |
62 | 66 | username: 'Username cannot be empty\nUsername must be at least 6 characters long\nUsername must contain at least one letter\nUsername must not contain special characters', |
63 | 67 | }); |
64 | 68 | }); |
| 69 | + |
| 70 | + describe('getMicroSecondTranslationErrorWithTranslationKey', () => { |
| 71 | + beforeEach(() => { |
| 72 | + jest.clearAllMocks(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('should create an error object with microsecond timestamp and translation key', () => { |
| 76 | + const mockMicroseconds = 1234567890123; |
| 77 | + (DateUtils.getMicroseconds as jest.Mock).mockReturnValue(mockMicroseconds); |
| 78 | + |
| 79 | + const result = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.incorrectLoginOrPassword'); |
| 80 | + |
| 81 | + expect(result).toEqual({ |
| 82 | + [mockMicroseconds]: {translationKey: 'passwordForm.error.incorrectLoginOrPassword'}, |
| 83 | + }); |
| 84 | + expect(DateUtils.getMicroseconds).toHaveBeenCalledTimes(1); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should use provided errorKey instead of generating microsecond timestamp', () => { |
| 88 | + const customErrorKey = 9876543210; |
| 89 | + const result = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.fallback', customErrorKey); |
| 90 | + |
| 91 | + expect(result).toEqual({ |
| 92 | + [customErrorKey]: {translationKey: 'passwordForm.error.fallback'}, |
| 93 | + }); |
| 94 | + expect(DateUtils.getMicroseconds).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should handle different translation keys', () => { |
| 98 | + const mockMicroseconds = 1111111111111; |
| 99 | + (DateUtils.getMicroseconds as jest.Mock).mockReturnValue(mockMicroseconds); |
| 100 | + |
| 101 | + const result = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.incorrectLoginOrPassword'); |
| 102 | + |
| 103 | + expect(result).toEqual({ |
| 104 | + [mockMicroseconds]: {translationKey: 'passwordForm.error.incorrectLoginOrPassword'}, |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('isTranslationKeyError', () => { |
| 110 | + test('should return false for string messages', () => { |
| 111 | + expect(ErrorUtils.isTranslationKeyError('This is a string error')).toBe(false); |
| 112 | + expect(ErrorUtils.isTranslationKeyError('')).toBe(false); |
| 113 | + }); |
| 114 | + |
| 115 | + test('should return false for array messages', () => { |
| 116 | + expect(ErrorUtils.isTranslationKeyError(['error1', 'error2'])).toBe(false); |
| 117 | + expect(ErrorUtils.isTranslationKeyError([])).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + test('should return false for empty objects', () => { |
| 121 | + expect(ErrorUtils.isTranslationKeyError({})).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + test('should return false for objects with multiple keys', () => { |
| 125 | + expect(ErrorUtils.isTranslationKeyError({ |
| 126 | + translationKey: 'passwordForm.error.fallback', |
| 127 | + extraKey: 'extra', |
| 128 | + })).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + test('should return false for objects without translationKey property', () => { |
| 132 | + expect(ErrorUtils.isTranslationKeyError({error: 'generic error'})).toBe(false); |
| 133 | + expect(ErrorUtils.isTranslationKeyError({message: 'error message'})).toBe(false); |
| 134 | + }); |
| 135 | + |
| 136 | + test('should return true for valid TranslationKeyError objects', () => { |
| 137 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'passwordForm.error.fallback'})).toBe(true); |
| 138 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'passwordForm.error.incorrectLoginOrPassword'})).toBe(true); |
| 139 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'session.offlineMessageRetry'})).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + test('should return false for null and undefined', () => { |
| 143 | + expect(ErrorUtils.isTranslationKeyError(null)).toBe(false); |
| 144 | + expect(ErrorUtils.isTranslationKeyError(undefined)).toBe(false); |
| 145 | + }); |
| 146 | + |
| 147 | + test('should return false for primitive types', () => { |
| 148 | + expect(ErrorUtils.isTranslationKeyError(123)).toBe(false); |
| 149 | + expect(ErrorUtils.isTranslationKeyError(true)).toBe(false); |
| 150 | + expect(ErrorUtils.isTranslationKeyError(false)).toBe(false); |
| 151 | + }); |
| 152 | + }); |
65 | 153 | }); |
0 commit comments