Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'ErrorUtilsTest.js' test to TypeScript #36161

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading