Home Manual Reference Source

src/controllers/web/chat.controller.js

import * as WebPageObjects from "../../page_objects/web/webPageObjects.js";
import BaseController from "./base.controller.js";

/** 
 * @extends BaseController
 */
class ChatController extends BaseController {
  /**
   * Adds the browser to the controller from the Base Controller constructor.
   * This then gets pushed into the various page objects
   * and is ultimately used there to make calls to the actual browser.
   * @param {args} args Args from client
   */
  constructor(args) {
    super(args);
    this.chatPage = new WebPageObjects.ChatPage(args);
  }

  /** 
   * Gets contact name from top of chat message window
   * @returns {String} Name of contact
   * @todo Fix this!
   */
  async getChatWindowHeaderName() {
    return await this.chatPage.chatWindowHeader().getText();
  }

  /** 
   * Gets messages for selected contact
   * @param {String} name - name of contact
   * @returns {Array} Array of objects of messages.
   * @example client.chatController.getChatMessagesForContact('Test User')
   * @example
   * // Returns...
   * [{
   *    from: 'Another User',
   *    timestamp: 'Friday, March 21, 2017',
   *    message: 'Hey from another user!'
   * },
   * {
   *    from: 'Another User',
   *    timestamp: 'Monday, March 24, 2017',
   *    message: 'Did you get my last message?'
   * },
   * {
   *    from: 'Another User 2',
   *    timestamp: 'Tuesday, March 25, 2017',
   *    message: 'Hey, Another User is trying to reach you...'
   * }]
   */
  async getChatMessagesForContact(name) {
    await this.chatPage.clickContact(name);
    return await this.chatPage.getChatMessages();
  }

  /** 
   * Get last message (as in the newest) for selected contact
   * @param {String} name - name of contact
   * @returns {Object} - Object with from, timestamp, and message.
   * @example client.chatController.getLastMessageForContact('Test User')
   * @example
   * // Returns...
   * {
   *    from: 'Another User',
   *    timestamp: 'Friday, March 24, 2017',
   *    message: 'Hey from another user!'
   * }
   */
  async getLastMessageForContact(name) {
    await this.chatPage.clickContact(name);
    const messages = await this.chatPage.getChatMessages();
    return messages[messages.length - 1];
  }

  /** 
   * Selects contact, then sends message to them
   * @param {String} name Name of contact
   * @param {String} message Message to send
   */
  async sendChatMessageToContact(name, message) {
    await this.chatPage.startNewChatWithContact(name);
    await this.chatPage.sendChatMessage(message);
  }
}

export default ChatController;