4

This is how one of my intercept functions looks like now:

interceptWithError() {
  nock(baseUrl)
    .get(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .put(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .post(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .delete(/.*/)
    .replyWithError(500);
}

I would like to avoid repetition, and also give it more flexibility by doing something like this:

interceptWithError(params) {
  const verb = params && params.verb;
  const stat = params && params.stat;

  return nock(baseUrl)
    .[verb]    // something like this!!!
    .replyWithError(stat)
}

Is there a way to do so???

Yalda
  • 680
  • 1
  • 18
  • 39

1 Answers1

0

This is what I came up with :)

baseNock(url) {
  return this.nock(url)
    .replyContentLength()
    .defaultReplyHeaders({ 'Content-Type': 'application/json' });
}

interceptWithError(verbCodeMap) {
  const verbs = (verbCodeMap && Object.keys(verbCodeMap))
    || ['post', 'get', 'put', 'delete'];

  return verbs.map(verb => 
    baseNock(someUrl)[verb](/.*/)
      .replyWithError((verbCodeMap && verbCodeMap[verb]) || 500));    
}
Yalda
  • 680
  • 1
  • 18
  • 39