Skip to content

Commit

Permalink
Merge pull request #36161 from VickyStash/ts-migration/errorUtilsTest
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate 'ErrorUtilsTest.js' test to TypeScript
  • Loading branch information
srikarparsi authored Feb 12, 2024
2 parents 5aee9a3 + f342e3b commit f7e6dbf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/libs/ErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function getErrorsWithTranslationData(errors: Localize.MaybePhraseKey | Errors):
* @param errors - An object containing current errors in the form
* @param message - Message to assign to the inputID errors
*/
function addErrorMessage<TKey extends TranslationPaths>(errors: Errors, inputID?: string, message?: TKey | Localize.MaybePhraseKey) {
function addErrorMessage<TKey extends TranslationPaths>(errors: Errors, inputID?: string | null, message?: TKey | Localize.MaybePhraseKey) {
if (!message || !inputID) {
return;
}
Expand Down
19 changes: 10 additions & 9 deletions tests/unit/ErrorUtilsTest.js → tests/unit/ErrorUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import * as ErrorUtils from '../../src/libs/ErrorUtils';
import * as ErrorUtils 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 = {};
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 = {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 = {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 = {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 = {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 = {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 = {};
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');
Expand All @@ -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: 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');

Expand Down

0 comments on commit f7e6dbf

Please sign in to comment.