All files / src QuickReplies.js

100% Statements 22/22
100% Branches 23/23
100% Functions 2/2
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63            8x 1x     7x 1x     6x 1x     5x 5x 5x 5x   5x         5x 3x     5x 1x     5x           4x 1x     3x 1x     2x   2x                    
import { QUICK_REPLY_TYPES, QUICK_REPLY_LIMIT } from './constants';
 
class QuickReply {
  constructor({
    title, payload = '', content_type = 'text', image_url = '',
  }) {
    if (QUICK_REPLY_TYPES.indexOf(content_type) === -1) {
      throw new Error('Invalid content type provided.');
    }
 
    if (title.length > 20) {
      throw new Error('Title cannot be longer 20 characters.');
    }
 
    if (payload && payload.length > 1000) {
      throw new Error('Payload cannot be longer 1000 characters.');
    }
 
    this.title = title;
    this.content_type = content_type;
    this.payload = payload;
    this.image_url = image_url;
 
    const quick_reply = {
      title: this.title,
      content_type: this.content_type,
    };
 
    if (this.payload && this.content_type === 'text') {
      quick_reply.payload = this.payload;
    }
 
    if (this.image_url && this.content_type === 'text') {
      quick_reply.image_url = this.image_url;
    }
 
    return quick_reply;
  }
}
 
class QuickReplies {
  constructor(quickReplies) {
    if (!Array.isArray(quickReplies)) {
      throw new Error('You must pass an array of QuickReply objects.');
    }
 
    if (quickReplies.length > QUICK_REPLY_LIMIT) {
      throw new Error(`You cannot have more than ${QUICK_REPLY_LIMIT} quick replies.`);
    }
 
    this.quickReplies = quickReplies;
 
    return {
      quick_replies: this.quickReplies,
    };
  }
}
 
export {
  QuickReply,
  QuickReplies,
};