-
Notifications
You must be signed in to change notification settings - Fork 0
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 #574 from CBIIT/CRDCDH-2118
CRDCDH-2118 Operation Dashboard – Studies filter utilizes "All" studies selected
- Loading branch information
Showing
4 changed files
with
260 additions
and
19 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
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,177 @@ | ||
import { DashboardContentOptions } from "amazon-quicksight-embedding-sdk"; | ||
import { addStudiesParameter, addDataCommonsParameter } from "./dashboardUtils"; | ||
import { Logger } from "./logger"; | ||
|
||
jest.mock("./logger", () => ({ | ||
Logger: { | ||
error: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe("addStudiesParameter", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should return an empty array if user has 'All' as the first study", () => { | ||
const user = { | ||
studies: [{ _id: "All" }, { _id: "AnotherStudy" }], | ||
} as unknown as User; | ||
|
||
const result = addStudiesParameter(user); | ||
expect(result).toEqual([]); | ||
expect(Logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should return an array with studiesParameter if user has valid studies", () => { | ||
const user = { | ||
studies: [{ _id: "StudyA" }, { _id: "StudyB" }], | ||
} as unknown as User; | ||
|
||
const result = addStudiesParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "studiesParameter", | ||
Values: ["StudyA", "StudyB"], | ||
}, | ||
]); | ||
expect(Logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should return NO-CONTENT if user has an empty studies array", () => { | ||
const user = { | ||
studies: [], | ||
} as unknown as User; | ||
|
||
const result = addStudiesParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "studiesParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
expect(Logger.error).toHaveBeenCalledWith( | ||
"Federal Lead requires studies to be set but none or invalid values were found.", | ||
[] | ||
); | ||
}); | ||
|
||
it("should return NO-CONTENT if user studies is undefined or null", () => { | ||
const user = { | ||
studies: null, | ||
} as unknown as User; | ||
|
||
const result = addStudiesParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "studiesParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should handle a null user gracefully", () => { | ||
const user = null as unknown as User; | ||
const result = addStudiesParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "studiesParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should handle an undefined user gracefully", () => { | ||
const user = undefined as unknown as User; | ||
const result = addStudiesParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "studiesParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe("addDataCommonsParameter", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should return an array with dataCommonsParameter if user has valid dataCommons", () => { | ||
const user = { | ||
dataCommons: ["CommonsA", "CommonsB"], | ||
} as unknown as User; | ||
|
||
const result = addDataCommonsParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "dataCommonsParameter", | ||
Values: ["CommonsA", "CommonsB"], | ||
}, | ||
]); | ||
expect(Logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should return NO-CONTENT if user dataCommons is an empty array", () => { | ||
const user = { | ||
dataCommons: [], | ||
} as unknown as User; | ||
|
||
const result = addDataCommonsParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "dataCommonsParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
expect(Logger.error).toHaveBeenCalledWith( | ||
"Data Commons Personnel requires dataCommons to be set but none were found.", | ||
[] | ||
); | ||
}); | ||
|
||
it("should return NO-CONTENT if user dataCommons is null or undefined", () => { | ||
const user = { | ||
dataCommons: null, | ||
} as unknown as User; | ||
|
||
const result = addDataCommonsParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "dataCommonsParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should handle a null user gracefully", () => { | ||
const user = null as unknown as User; | ||
const result = addDataCommonsParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "dataCommonsParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should handle an undefined user gracefully", () => { | ||
const user = undefined as unknown as User; | ||
const result = addDataCommonsParameter(user); | ||
expect(result).toEqual<DashboardContentOptions["parameters"]>([ | ||
{ | ||
Name: "dataCommonsParameter", | ||
Values: ["NO-CONTENT"], | ||
}, | ||
]); | ||
expect(Logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,66 @@ | ||
import { DashboardContentOptions } from "amazon-quicksight-embedding-sdk"; | ||
import { Logger } from "./logger"; | ||
|
||
/** | ||
* Constructs and returns an array of QuickSight parameter objects for a user's studies. | ||
* | ||
* - If the user's first study is `All`, the function returns an empty array (allowing QuickSight to display all data). | ||
* - If the user has a valid array of studies, it creates a `studiesParameter` whose values are the `_id` fields of each study. | ||
* - Otherwise, it logs an error and returns a parameter array with `["NO-CONTENT"]`. | ||
* | ||
* | ||
* @param {User} user - The current user | ||
* @returns {DashboardContentOptions["parameters"]} The updated dashboard parameters | ||
*/ | ||
export const addStudiesParameter = (user: User): DashboardContentOptions["parameters"] => { | ||
const params: DashboardContentOptions["parameters"] = []; | ||
const { studies } = user || {}; | ||
|
||
// If user contains the "All" study, do NOT push the "studiesParameter" | ||
if ((studies || [])?.findIndex((s) => s?._id === "All") !== -1) { | ||
return params; | ||
} | ||
|
||
// Otherwise, push a real or fallback param | ||
if (Array.isArray(studies) && studies.length > 0) { | ||
params.push({ | ||
Name: "studiesParameter", | ||
Values: studies.map((s) => s._id), | ||
}); | ||
return params; | ||
} | ||
|
||
Logger.error( | ||
"Federal Lead requires studies to be set but none or invalid values were found.", | ||
studies | ||
); | ||
params.push({ Name: "studiesParameter", Values: ["NO-CONTENT"] }); | ||
return params; | ||
}; | ||
|
||
/** | ||
* Constructs and returns an array of QuickSight parameter objects for a user's data commons. | ||
* | ||
* - If the user has a valid array of data commons, it creates a `dataCommonsParameter` whose values are the array elements. | ||
* - Otherwise, it logs an error and returns a parameter array with `["NO-CONTENT"]`. | ||
* | ||
* | ||
* @param {User} user - The current user | ||
* @returns {DashboardContentOptions["parameters"]} The updated dashboard parameters | ||
*/ | ||
export const addDataCommonsParameter = (user: User): DashboardContentOptions["parameters"] => { | ||
const params: DashboardContentOptions["parameters"] = []; | ||
const { dataCommons } = user || {}; | ||
|
||
if (Array.isArray(dataCommons) && dataCommons.length > 0) { | ||
params.push({ Name: "dataCommonsParameter", Values: dataCommons }); | ||
return params; | ||
} | ||
|
||
Logger.error( | ||
"Data Commons Personnel requires dataCommons to be set but none were found.", | ||
dataCommons | ||
); | ||
params.push({ Name: "dataCommonsParameter", Values: ["NO-CONTENT"] }); | ||
return params; | ||
}; |
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