-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2192 from AntaresSimulatorTeam/feature/add-utils-…
…tests test(ui-utils): add tests for validation utils & refactor imports
- Loading branch information
Showing
28 changed files
with
523 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -659,3 +659,5 @@ export interface TaskView { | |
type: TaskType; | ||
status: string; | ||
} | ||
|
||
export type ValidationReturn = string | true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import { validateArray } from "./array"; | ||
|
||
vi.mock("i18next", () => ({ | ||
t: vi.fn((key) => key), | ||
})); | ||
|
||
describe("validateArray", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test("should return true for a non-empty array with no duplicates", () => { | ||
expect(validateArray([1, 2, 3])).toBe(true); | ||
}); | ||
|
||
test("should return an error message for an empty array when not allowed", () => { | ||
expect(validateArray([])).toBe("form.field.required"); | ||
}); | ||
|
||
test("should return true for an empty array when allowed", () => { | ||
expect(validateArray([], { allowEmpty: true })).toBe(true); | ||
}); | ||
|
||
test("should return an error message for an array with duplicates when not allowed", () => { | ||
expect(validateArray([1, 2, 2, 3])).toBe("form.field.duplicateNotAllowed"); | ||
}); | ||
|
||
test("should return true for an array with duplicates when allowed", () => { | ||
expect(validateArray([1, 2, 2, 3], { allowDuplicate: true })).toBe(true); | ||
}); | ||
|
||
test("should work with currying", () => { | ||
const validator = validateArray({ allowDuplicate: false }); | ||
expect(validator([1, 2, 3])).toBe(true); | ||
expect(validator([1, 1, 2, 3])).toBe("form.field.duplicateNotAllowed"); | ||
}); | ||
|
||
test("should work with different types of array elements", () => { | ||
expect(validateArray(["a", "b", "c"])).toBe(true); | ||
expect(validateArray([{ id: 1 }, { id: 2 }])).toBe(true); | ||
}); | ||
|
||
test("should handle both options simultaneously", () => { | ||
expect(validateArray([], { allowEmpty: true, allowDuplicate: false })).toBe( | ||
true, | ||
); | ||
expect( | ||
validateArray([1, 1], { allowEmpty: true, allowDuplicate: true }), | ||
).toBe(true); | ||
expect( | ||
validateArray([1, 1], { allowEmpty: true, allowDuplicate: false }), | ||
).toBe("form.field.duplicateNotAllowed"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import { ValidationReturn } from "@/common/types"; | ||
import { t } from "i18next"; | ||
import * as R from "ramda"; | ||
|
||
interface ArrayValidationOptions { | ||
allowEmpty?: boolean; | ||
allowDuplicate?: boolean; | ||
} | ||
|
||
/** | ||
* Validates an array against specified criteria. | ||
* This function checks for duplicate values in the array. | ||
* | ||
* @example | ||
* validateArray([1, 2, 3], { allowDuplicate: false }); // true | ||
* validateArray([1, 1, 2, 3], { allowDuplicate: false }); // Error message | ||
* | ||
* | ||
* @example <caption>With currying.</caption> | ||
* const fn = validateArray({ allowDuplicate: false }); | ||
* fn([1, 2, 3]); // true | ||
* fn([1, 1, 2, 3]); // Error message | ||
* | ||
* @param value - The array to validate. | ||
* @param [options] - Configuration options for validation. | ||
* @param [options.allowEmpty=false] - Sets whether empty array is allowed or not. | ||
* @param [options.allowDuplicate=false] - Sets whether duplicate values are allowed or not. | ||
* @returns True if validation is successful, or a localized error message if it fails. | ||
*/ | ||
export function validateArray<T>( | ||
value: T[], | ||
options?: ArrayValidationOptions, | ||
): ValidationReturn; | ||
|
||
export function validateArray<T>( | ||
options?: ArrayValidationOptions, | ||
): (value: T[]) => ValidationReturn; | ||
|
||
export function validateArray<T>( | ||
valueOrOpts?: T[] | ArrayValidationOptions, | ||
options: ArrayValidationOptions = {}, | ||
): ValidationReturn | ((value: T[]) => ValidationReturn) { | ||
if (!Array.isArray(valueOrOpts)) { | ||
return (v: T[]) => validateArray(v, valueOrOpts); | ||
} | ||
|
||
const value = valueOrOpts; | ||
const { allowEmpty, allowDuplicate } = options; | ||
|
||
if (!value.length && !allowEmpty) { | ||
return t("form.field.required"); | ||
} | ||
|
||
if (!allowDuplicate && R.uniq(value).length !== value.length) { | ||
return t("form.field.duplicateNotAllowed"); | ||
} | ||
|
||
return true; | ||
} |
Oops, something went wrong.