|
| 1 | +import DateUtils from '@src/libs/DateUtils'; |
1 | 2 | import * as ErrorUtils from '@src/libs/ErrorUtils'; |
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,161 @@ 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( |
| 126 | + ErrorUtils.isTranslationKeyError({ |
| 127 | + translationKey: 'passwordForm.error.fallback', |
| 128 | + extraKey: 'extra', |
| 129 | + }), |
| 130 | + ).toBe(false); |
| 131 | + }); |
| 132 | + |
| 133 | + test('should return false for objects without translationKey property', () => { |
| 134 | + expect(ErrorUtils.isTranslationKeyError({error: 'generic error'})).toBe(false); |
| 135 | + expect(ErrorUtils.isTranslationKeyError({message: 'error message'})).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + test('should return true for valid TranslationKeyError objects', () => { |
| 139 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'passwordForm.error.fallback'})).toBe(true); |
| 140 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'passwordForm.error.incorrectLoginOrPassword'})).toBe(true); |
| 141 | + expect(ErrorUtils.isTranslationKeyError({translationKey: 'session.offlineMessageRetry'})).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + test('should return false for null and undefined', () => { |
| 145 | + expect(ErrorUtils.isTranslationKeyError(null)).toBe(false); |
| 146 | + expect(ErrorUtils.isTranslationKeyError(undefined)).toBe(false); |
| 147 | + }); |
| 148 | + |
| 149 | + test('should return false for primitive types', () => { |
| 150 | + expect(ErrorUtils.isTranslationKeyError(123)).toBe(false); |
| 151 | + expect(ErrorUtils.isTranslationKeyError(true)).toBe(false); |
| 152 | + expect(ErrorUtils.isTranslationKeyError(false)).toBe(false); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('Integration: getMicroSecondTranslationErrorWithTranslationKey and isTranslationKeyError', () => { |
| 157 | + beforeEach(() => { |
| 158 | + jest.clearAllMocks(); |
| 159 | + }); |
| 160 | + |
| 161 | + test('should create translation error objects that are correctly identified by isTranslationKeyError', () => { |
| 162 | + const mockMicroseconds = 1234567890123; |
| 163 | + (DateUtils.getMicroseconds as jest.Mock).mockReturnValue(mockMicroseconds); |
| 164 | + |
| 165 | + const errorObject = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.incorrectLoginOrPassword'); |
| 166 | + |
| 167 | + // The error object should have one key (the timestamp) |
| 168 | + const keys = Object.keys(errorObject); |
| 169 | + expect(keys).toHaveLength(1); |
| 170 | + |
| 171 | + // The value at that key should be a valid translation key error |
| 172 | + const errorValue = errorObject[keys[0]]; |
| 173 | + expect(ErrorUtils.isTranslationKeyError(errorValue)).toBe(true); |
| 174 | + }); |
| 175 | + |
| 176 | + test('should work with custom error keys', () => { |
| 177 | + const customErrorKey = 9876543210; |
| 178 | + const errorObject = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.fallback', customErrorKey); |
| 179 | + |
| 180 | + // The error object should have the custom key |
| 181 | + expect(errorObject).toHaveProperty(customErrorKey.toString()); |
| 182 | + |
| 183 | + // The value should be a valid translation key error |
| 184 | + const errorValue = errorObject[customErrorKey]; |
| 185 | + expect(ErrorUtils.isTranslationKeyError(errorValue)).toBe(true); |
| 186 | + }); |
| 187 | + |
| 188 | + test('should create objects with multiple errors that are all valid translation key errors', () => { |
| 189 | + const mockMicroseconds1 = 1111111111111; |
| 190 | + const mockMicroseconds2 = 2222222222222; |
| 191 | + |
| 192 | + (DateUtils.getMicroseconds as jest.Mock).mockReturnValueOnce(mockMicroseconds1).mockReturnValueOnce(mockMicroseconds2); |
| 193 | + |
| 194 | + const error1 = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.incorrectLoginOrPassword'); |
| 195 | + const error2 = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('session.offlineMessageRetry'); |
| 196 | + |
| 197 | + // Combine the error objects |
| 198 | + const combinedErrors = {...error1, ...error2}; |
| 199 | + |
| 200 | + // All values should be valid translation key errors |
| 201 | + Object.values(combinedErrors).forEach((errorValue) => { |
| 202 | + expect(ErrorUtils.isTranslationKeyError(errorValue)).toBe(true); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + test('should verify the structure of created translation key errors', () => { |
| 207 | + const mockMicroseconds = 5555555555555; |
| 208 | + (DateUtils.getMicroseconds as jest.Mock).mockReturnValue(mockMicroseconds); |
| 209 | + |
| 210 | + const errorObject = ErrorUtils.getMicroSecondTranslationErrorWithTranslationKey('passwordForm.error.twoFactorAuthenticationEnabled'); |
| 211 | + const errorValue = errorObject[mockMicroseconds]; |
| 212 | + |
| 213 | + // Verify the structure matches what isTranslationKeyError expects |
| 214 | + expect(errorValue).toEqual({ |
| 215 | + translationKey: 'passwordForm.error.twoFactorAuthenticationEnabled', |
| 216 | + }); |
| 217 | + |
| 218 | + // Verify it passes all the checks in isTranslationKeyError |
| 219 | + expect(typeof errorValue).not.toBe('string'); |
| 220 | + expect(Array.isArray(errorValue)).toBe(false); |
| 221 | + expect(Object.keys(errorValue)).toHaveLength(1); |
| 222 | + expect(errorValue.translationKey).toBeDefined(); |
| 223 | + expect(ErrorUtils.isTranslationKeyError(errorValue)).toBe(true); |
| 224 | + }); |
| 225 | + }); |
65 | 226 | }); |
0 commit comments