All files / src/MockEnvironment MockEnvironmentBuilder.ts

22.86% Statements 16/70
0% Branches 0/18
8.33% Functions 1/12
22.39% Lines 15/67
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 1622x               2x                                     2x                                 2x                                             2x         2x         2x         2x         2x         2x         2x             2x         2x                                                                   2x                                 2x  
import {
  mockSearchInterface,
  mockSearchEndpoint,
  mockQueryController,
  mockComponent,
  mockUsageAnalytics
} from "./MockComponents";
 
import {
  ModalBox,
  IComponentBindings,
  IQueryResult,
  SearchInterface,
  QueryStateModel,
  IAnalyticsClient,
  $$,
  ComponentStateModel,
  ComponentOptionsModel,
  OS_NAME,
  Component,
  BaseComponent,
  NoopAnalyticsClient,
  AnalyticsEndpoint,
  SearchEndpoint,
  QueryController,
  QueryBuilder
} from "coveo-search-ui";
import * as FakeResults from "../Fake";
 
export interface IMockEnvironment extends IComponentBindings {
  root: HTMLElement;
  element: HTMLElement;
  result: IQueryResult;
  searchEndpoint: SearchEndpoint;
  searchInterface: SearchInterface;
  queryController: QueryController;
  queryStateModel: QueryStateModel;
  usageAnalytics: IAnalyticsClient;
}
 
export interface IMockEnvironmentWithData<T> extends IMockEnvironment {
  data: T;
}
 
export class MockEnvironmentBuilder {
  public root: HTMLElement = $$("div").el;
  public element: HTMLElement = $$("div").el;
  public result: IQueryResult = undefined;
  public searchEndpoint = mockSearchEndpoint();
  public searchInterface = mockSearchInterface();
  public queryController = mockQueryController();
  public queryStateModel = mockComponent<QueryStateModel>(
    QueryStateModel,
    QueryStateModel.ID
  );
  public componentStateModel = mockComponent<ComponentStateModel>(
    ComponentStateModel,
    ComponentStateModel.ID
  );
  public usageAnalytics = mockUsageAnalytics();
  public componentOptionsModel = mockComponent<ComponentOptionsModel>(
    ComponentOptionsModel,
    ComponentOptionsModel.ID
  );
  public os: OS_NAME;
  private built = false;
 
  public withRoot(root: HTMLElement): this {
    this.root = root ? root : this.root;
    return this;
  }
 
  public withElement(element: HTMLElement): this {
    this.element = element ? element : this.element;
    return this;
  }
 
  public withLiveQueryStateModel(): this {
    this.queryStateModel = new QueryStateModel(this.root);
    return this;
  }
 
  public withQueryStateModel(model: QueryStateModel): this {
    this.queryStateModel = model;
    return this;
  }
 
  public withCollaborativeRating(): this {
    this.searchInterface.options.enableCollaborativeRating = true;
    return this;
  }
 
  public withOs(os: OS_NAME): this {
    this.os = os;
    return this;
  }
 
  public withResult(
    result: IQueryResult = FakeResults.createFakeResult()
  ): this {
    this.result = result;
    return this;
  }
 
  public withEndpoint(endpoint: SearchEndpoint = mockSearchEndpoint()): this {
    this.searchEndpoint = endpoint;
    return this;
  }
 
  public build(): IMockEnvironment {
    if (this.built) {
      return this.getBindings();
    }
    if (this.element.parentNode === undefined) {
      this.root.appendChild(this.element);
    }
 
    Component.bindComponentToElement(this.root, this.searchInterface);
    Component.bindComponentToElement(this.root, this.queryController);
    Component.bindComponentToElement(this.root, this.queryStateModel);
    Component.bindComponentToElement(this.root, this.componentStateModel);
    Component.bindComponentToElement(this.root, this.componentOptionsModel);
 
    this.searchInterface.queryController = this.queryController;
    this.searchInterface.queryStateModel = this.queryStateModel;
    this.searchInterface.componentStateModel = this.componentStateModel;
    this.searchInterface.componentOptionsModel = this.componentOptionsModel;
 
    if (!this.searchEndpoint) {
      this.searchEndpoint = mockSearchEndpoint();
    }
 
    this.queryController.getEndpoint = () => {
      return this.searchEndpoint;
    };
 
    if (this.result) {
      Component.bindResultToElement(this.element, this.result);
    }
    this.built = true;
    return this.getBindings();
  }
 
  public getBindings(): IMockEnvironment {
    if (!this.built) {
      return this.build();
    }
    return {
      root: this.root,
      element: this.element,
      result: this.result,
      searchEndpoint: this.searchEndpoint,
      searchInterface: this.searchInterface,
      queryController: this.queryController,
      queryStateModel: this.queryStateModel,
      usageAnalytics: this.usageAnalytics,
      componentStateModel: this.componentStateModel,
      componentOptionsModel: this.componentOptionsModel
    };
  }
}