From ae559973eb747204be852d388588ae49274a21b9 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 8 Feb 2024 17:12:14 +0100 Subject: [PATCH 1/2] [TS migration] Migrate 'ErrorUtilsTest.js' test --- src/libs/ErrorUtils.ts | 4 +++- .../{ErrorUtilsTest.js => ErrorUtilsTest.ts} | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) rename tests/unit/{ErrorUtilsTest.js => ErrorUtilsTest.ts} (80%) diff --git a/src/libs/ErrorUtils.ts b/src/libs/ErrorUtils.ts index 6fbba1e750dc..d27f45aa624e 100644 --- a/src/libs/ErrorUtils.ts +++ b/src/libs/ErrorUtils.ts @@ -113,7 +113,7 @@ type ErrorsList = Record; * @param errors - An object containing current errors in the form * @param message - Message to assign to the inputID errors */ -function addErrorMessage(errors: ErrorsList, inputID?: string, message?: TKey | Localize.MaybePhraseKey) { +function addErrorMessage(errors: ErrorsList, inputID?: string | null, message?: TKey | Localize.MaybePhraseKey) { if (!message || !inputID) { return; } @@ -141,3 +141,5 @@ export { addErrorMessage, getLatestErrorMessageField, }; + +export type {ErrorsList}; diff --git a/tests/unit/ErrorUtilsTest.js b/tests/unit/ErrorUtilsTest.ts similarity index 80% rename from tests/unit/ErrorUtilsTest.js rename to tests/unit/ErrorUtilsTest.ts index 7a46c71a1aa1..4c8fd84b7d3b 100644 --- a/tests/unit/ErrorUtilsTest.js +++ b/tests/unit/ErrorUtilsTest.ts @@ -1,50 +1,51 @@ -import * as ErrorUtils from '../../src/libs/ErrorUtils'; +import * as ErrorUtils from '@src/libs/ErrorUtils'; +import type {ErrorsList} from '@src/libs/ErrorUtils'; describe('ErrorUtils', () => { test('should add a new error message for a given inputID', () => { - const errors = {}; + const errors: ErrorsList = {}; ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty'); expect(errors).toEqual({username: ['Username cannot be empty', {isTranslated: true}]}); }); test('should append an error message to an existing error message for a given inputID', () => { - const errors = {username: 'Username cannot be empty'}; + const errors: ErrorsList = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long'); expect(errors).toEqual({username: ['Username cannot be empty\nUsername must be at least 6 characters long', {isTranslated: true}]}); }); test('should add an error to input which does not contain any errors yet', () => { - const errors = {username: 'Username cannot be empty'}; + const errors: ErrorsList = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'password', 'Password cannot be empty'); expect(errors).toEqual({username: 'Username cannot be empty', password: ['Password cannot be empty', {isTranslated: true}]}); }); test('should not mutate the errors object when message is empty', () => { - const errors = {username: 'Username cannot be empty'}; + const errors: ErrorsList = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', ''); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should not mutate the errors object when inputID is null', () => { - const errors = {username: 'Username cannot be empty'}; + const errors: ErrorsList = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, null, 'InputID cannot be null'); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should not mutate the errors object when message is null', () => { - const errors = {username: 'Username cannot be empty'}; + const errors: ErrorsList = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', null); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should add multiple error messages for the same inputID', () => { - const errors = {}; + const errors: ErrorsList = {}; ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter'); @@ -53,7 +54,7 @@ describe('ErrorUtils', () => { }); test('should append multiple error messages to an existing error message for the same inputID', () => { - const errors = {username: 'Username cannot be empty\nUsername must be at least 6 characters long'}; + const errors: ErrorsList = {username: 'Username cannot be empty\nUsername must be at least 6 characters long'}; ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must not contain special characters'); From f342e3b6d2aad8b4be81e217890d3f1f3eb00eb8 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 9 Feb 2024 10:07:50 +0100 Subject: [PATCH 2/2] Use Errors type instead of ErrorsList --- tests/unit/ErrorUtilsTest.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit/ErrorUtilsTest.ts b/tests/unit/ErrorUtilsTest.ts index 4c8fd84b7d3b..9168c1ca12a5 100644 --- a/tests/unit/ErrorUtilsTest.ts +++ b/tests/unit/ErrorUtilsTest.ts @@ -1,51 +1,51 @@ import * as ErrorUtils from '@src/libs/ErrorUtils'; -import type {ErrorsList} from '@src/libs/ErrorUtils'; +import type {Errors} from '@src/types/onyx/OnyxCommon'; describe('ErrorUtils', () => { test('should add a new error message for a given inputID', () => { - const errors: ErrorsList = {}; + const errors: Errors = {}; ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty'); expect(errors).toEqual({username: ['Username cannot be empty', {isTranslated: true}]}); }); test('should append an error message to an existing error message for a given inputID', () => { - const errors: ErrorsList = {username: 'Username cannot be empty'}; + const errors: Errors = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long'); expect(errors).toEqual({username: ['Username cannot be empty\nUsername must be at least 6 characters long', {isTranslated: true}]}); }); test('should add an error to input which does not contain any errors yet', () => { - const errors: ErrorsList = {username: 'Username cannot be empty'}; + const errors: Errors = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'password', 'Password cannot be empty'); expect(errors).toEqual({username: 'Username cannot be empty', password: ['Password cannot be empty', {isTranslated: true}]}); }); test('should not mutate the errors object when message is empty', () => { - const errors: ErrorsList = {username: 'Username cannot be empty'}; + const errors: Errors = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', ''); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should not mutate the errors object when inputID is null', () => { - const errors: ErrorsList = {username: 'Username cannot be empty'}; + const errors: Errors = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, null, 'InputID cannot be null'); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should not mutate the errors object when message is null', () => { - const errors: ErrorsList = {username: 'Username cannot be empty'}; + const errors: Errors = {username: 'Username cannot be empty'}; ErrorUtils.addErrorMessage(errors, 'username', null); expect(errors).toEqual({username: 'Username cannot be empty'}); }); test('should add multiple error messages for the same inputID', () => { - const errors: ErrorsList = {}; + const errors: Errors = {}; ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter'); @@ -54,7 +54,7 @@ describe('ErrorUtils', () => { }); test('should append multiple error messages to an existing error message for the same inputID', () => { - const errors: ErrorsList = {username: 'Username cannot be empty\nUsername must be at least 6 characters long'}; + const errors: Errors = {username: 'Username cannot be empty\nUsername must be at least 6 characters long'}; ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter'); ErrorUtils.addErrorMessage(errors, 'username', 'Username must not contain special characters');