How to adapt unit tests for an existing function that needs to be wrapped in a Higher Order Function?
I have two functions. getServerProps() fetching data and isAuthenticated() checking user authentication. Both in separate modules with their own unit tests.
export function isAuthenticated(getServerProps) {
return async function (param1, param2, param3) {
const isLoggedIn = await authService()
if (!isLoggedIn) {
return { shouldRedirect: true };
}
const serverProps = await getServerProps(param1, param2, param3);
return { shouldRedirect: false, ...serverProps };
};
}
.
export async function getServerProps(param1, param2, param3) {
const data = new DataService(param1, param2, param3);
return { ...data };
}
Changing the export of the second function to isAuthenticated(getServerProps) mixes two functions in one unit. How to isolate the initial getServerProps() for the purpose of unit testing?
What's the best way to write tests in such a situation?