Created
August 2, 2024 23:26
-
-
Save cjcenizal/3ea34390d081bf0a38747988d2d98aa1 to your computer and use it in GitHub Desktop.
Testability
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
// What are some things that make this code difficult to test, and how would you address them? | |
import { config } from "../constants/config"; | |
import { sendRequest } from "../services/sendRequest"; | |
import { notifications } from "../services/notifications"; | |
export async function fetchUser(userModel) { | |
const { | |
server: { | |
api: { baseUrl }, | |
}, | |
} = config; | |
const { | |
meta: { id }, | |
} = userModel; | |
const method = "GET"; | |
const url = `${baseUrl}/users/${id}`; | |
const startTime = Date.now(); | |
try { | |
return { | |
user: await sendRequest[method](url), | |
roundTripTime: Date.now() - startTime, | |
}; | |
} catch (e) { | |
if (e.statusCode === 404) { | |
notifications.showError(`User with ID ${id} does not exist`); | |
} | |
throw e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment