Skip to content

Commit

Permalink
add test for *edge* case for CustomTextArea
Browse files Browse the repository at this point in the history
  • Loading branch information
jkafader-esnet committed Nov 9, 2024
1 parent bfe65b0 commit 40877ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/components/CustomTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function validateMapJsonStr(inStr: string, currentValidationState: ValidationSta
let validationFailedMsg: null | string = null;
try {
const parsedObj = JSON.parse(inStr);
if (typeof(parsedObj) != 'object') {
if (typeof(parsedObj) !== 'object') {
throw new Error("Bad topology object");
}
if (!Array.isArray(parsedObj.edges) || !Array.isArray(parsedObj.nodes)) {
Expand All @@ -41,8 +41,9 @@ function validateMapJsonStr(inStr: string, currentValidationState: ValidationSta
for (const edge of parsedObj.edges) {
const { name, meta, coordinates } = edge;
if (
!name || typeof(name) != 'string' ||
(!!meta && typeof(meta) != 'object') ||
!name || typeof(name) !== 'string' ||
(!!meta && typeof(meta) !== 'object') ||
meta?.endpoint_identifiers !== 'object' ||
!coordinates || !Array.isArray(coordinates) ||
coordinates.some((coordinate) => {
return !Array.isArray(coordinate)
Expand All @@ -56,10 +57,10 @@ function validateMapJsonStr(inStr: string, currentValidationState: ValidationSta
for (const node of parsedObj.nodes) {
const { name, meta, coordinate } = node;
if (
!name || typeof(name) != 'string' ||
(!!meta && typeof(meta) != 'object') ||
!name || typeof(name) !== 'string' ||
(!!meta && typeof(meta) !== 'object') ||
!coordinate || !Array.isArray(coordinate) ||
coordinate.length != 2 || !Number.isFinite(coordinate[0]) ||
coordinate.length !== 2 || !Number.isFinite(coordinate[0]) ||
!Number.isFinite(coordinate[1])
) {
throw new Error("Bad node definition");
Expand Down
12 changes: 11 additions & 1 deletion test/react/CustomTextArea.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe("Component CustomTextArea esmap", () => {
expect(document.getElementsByTagName("textarea")).toBeTruthy();
});
it("should provide an error message if the value is changed to invalid JSON", async ()=>{
let errorElems = document.getElementsByClassName("validation-error")[0];
await act(async () => {
let elems = document.getElementsByTagName("textarea");
let elem = elems[0];
Expand All @@ -63,5 +62,16 @@ describe("Component CustomTextArea esmap", () => {
let error = document.getElementsByClassName("validation-error")[0];

expect(error.innerText).toBeTruthy();
});
it("should provide an error message if the user provides an incomplete edge", async()=>{
await act(async () => {
let elems = document.getElementsByTagName("textarea");
let elem = elems[0];
const badEdgeTopology = '{ "edges": [ {"name": "A--L", "coordinates": [[39, -98],[39, -99]], "meta": { } }], "nodes": [ {"name": "L", "coordinate": [39, -98], "meta": {} } ] }';
triggerInputChange(elem, badEdgeTopology);
})
let error = document.getElementsByClassName("validation-error")[0];

expect(error.innerText).toBe("Bad edge definition");
})
})

0 comments on commit 40877ac

Please sign in to comment.