Skip to content

Instantly share code, notes, and snippets.

@mgeraci
Last active October 14, 2020 20:51
Show Gist options
  • Select an option

  • Save mgeraci/44d0b36490428ae3aa95180c91dfa24d to your computer and use it in GitHub Desktop.

Select an option

Save mgeraci/44d0b36490428ae3aa95180c91dfa24d to your computer and use it in GitHub Desktop.
A fake api client for the OkCupid web takehome

Let's add a network request to the app to save the completed essay. This gist provides a little API client that you should copy into your project and mocks network requests. Here's the deal:

  • There should be a button next to "start over" on the essay review screen labeled "save".
  • It should pass the text of that reviewed essay, including any user edits, to the endpoint.
  • You should not be able to save a blank essay.
  • To contrive a test case where the backend rejects the essay, the endpoint will reject any essay containing the phrases "love to laugh", "work hard, play hard", or "partner in crime".
  • Upon saving and getting an error, the user should see the error message returned by the server.
  • Upon successfully saving, the user should see a confirmation message (e.g., "Your essay has been saved!").

The API client in this gist supports one endpoint, essay, which accepts the following params:

{
  text: "the text of your essay"
}

It responds with a Promise. If successful, it will resolve with the object { success: true }, and if it fails, it will reject with a JavaScript Error.

export default {
post(url, data) {
if (!url || url === '') {
return Promise.reject(new Error('OkcApi was called without a url'));
}
if (url !== 'essay' && url !== '/essay') {
return Promise.reject(new Error(`OkcApi does not support the route '${url}'`));
}
let text = '';
if (data && data.text) {
text = data.text;
}
text = text.toLowerCase();
if (
text.indexOf('love to laugh') >= 0
|| text.indexOf('work hard, play hard') >= 0
|| text.indexOf('partner in crime') >= 0
) {
return Promise.reject(new Error('Your essay had an invalid phrase. Try being less basic!'));
}
return Promise.resolve({
success: true,
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment