generated from MeasureAuthoringTool/madie-frontend-template
-
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.
MAt-6744 added check user can delete function
- Loading branch information
1 parent
33b2e06
commit 7953392
Showing
4 changed files
with
95 additions
and
1 deletion.
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,75 @@ | ||
import * as React from "react"; | ||
import useOktaTokens from "../hooks/useOktaTokens"; | ||
import useCheckUserCanDelete from "./useCheckCanDelete"; | ||
|
||
const JANE_DOE = "Jane doe"; | ||
const JOHN_DOE = "john doe"; | ||
|
||
jest.mock("../hooks/useOktaTokens", () => | ||
jest.fn(() => ({ | ||
getUserName: jest.fn(), | ||
})) | ||
); | ||
|
||
describe("Check user canEdit", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
(useOktaTokens as jest.Mock).mockImplementation(() => ({ | ||
getUserName: () => JANE_DOE, | ||
})); | ||
}); | ||
|
||
it("should return true when user name and createdBy are the same", () => { | ||
const canEdit = useCheckUserCanDelete(JANE_DOE, true); | ||
expect(canEdit).toBeTruthy(); | ||
}); | ||
|
||
it("should return true when user name and createdBy are the same", () => { | ||
const canEdit = useCheckUserCanDelete(JANE_DOE, true); | ||
expect(canEdit).toBeTruthy(); | ||
}); | ||
|
||
it("should return false when user name and createdBy are same but it is not draft", () => { | ||
const canEdit = useCheckUserCanDelete(JANE_DOE, false); | ||
expect(canEdit).not.toBeTruthy(); | ||
}); | ||
|
||
// it("should return undefined when user name and createdBy are not the same", () => { | ||
// const canEdit = useCheckUserCanEdit("anotherU$er", true); | ||
// expect(canEdit).not.toBeTruthy(); | ||
// }); | ||
|
||
// it("should return true when measure is shared with the same user", () => { | ||
// const canEdit = useCheckUserCanEdit( | ||
// "anotherU$er", // nosec | ||
// [{ userId: JANE_DOE, roles: ["SHARED_WITH"] }], | ||
// true | ||
// ); | ||
// expect(canEdit).toBeTruthy(); | ||
// }); | ||
|
||
// it("should return true when measure is shared with the same user with no version status supplied", () => { | ||
// const canEdit = useCheckUserCanEdit("anotherU$er", [ | ||
// { userId: JANE_DOE, roles: ["SHARED_WITH"] }, | ||
// ]); | ||
// expect(canEdit).toBeTruthy(); | ||
// }); | ||
|
||
// it("should return undefined when measure is shared with a different user", () => { | ||
// const canEdit = useCheckUserCanEdit( | ||
// "anotherU$er", | ||
// [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], | ||
// true | ||
// ); | ||
// expect(canEdit).not.toBeTruthy(); | ||
// }); | ||
|
||
// it("should return false when measure is versioned greater than 0", () => { | ||
// const canEdit = useCheckUserCanEdit( | ||
// "anotherU$er", | ||
// [{ userId: JOHN_DOE, roles: ["SHARED_WITH"] }], | ||
// false | ||
// ); | ||
// expect(canEdit).not.toBeTruthy(); | ||
// }); | ||
}); |
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,17 @@ | ||
import useOktaTokens from "../hooks/useOktaTokens"; | ||
|
||
const useCheckUserCanDelete = ( | ||
createdBy: string, | ||
draft: boolean = true | ||
): boolean => { | ||
const { getUserName } = useOktaTokens(); | ||
const userName = getUserName(); | ||
// versioned measures/libraries can never be deleted . | ||
if (!draft) { | ||
return false; | ||
} | ||
|
||
return createdBy?.toLowerCase() === userName?.toLowerCase(); | ||
}; | ||
|
||
export default useCheckUserCanDelete; |
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