All files / src/MockEnvironment MockComponents.ts

47.76% Statements 32/67
50% Branches 5/10
20.83% Functions 5/24
47.76% Lines 32/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 1332x                                               2x   39x   26x 26x 260x   26x     2x 13x 13x       13x                               13x 13x 13x 13x     2x   13x   13x 13x 13x     2x 13x 13x 13x 13x 13x     2x 13x                       2x                   2x                               2x           2x              
import {
  BaseComponent,
  SearchInterface,
  QueryController,
  SearchEndpoint,
  QueryBuilder,
  IAnalyticsClient,
  NoopAnalyticsClient,
  AnalyticsEndpoint
} from "coveo-search-ui";
 
export type ConstructorFunc<T> = any;
 
export interface IResponsiveComponentsMock {
  windoh: Window;
  setSmallScreenWidth(width: number): void;
  setMediumScreenWidth(width: number): void;
  getSmallScreenWidth(): number;
  getMediumScreenWidth(): number;
  isSmallScreenWidth(): boolean;
  isMediumScreenWidth(): boolean;
  isLargeScreenWidth(): boolean;
}
 
export function mock<T>(
  contructorFunc: ConstructorFunc<T>,
  name: string = "mock"
): T {
  const keys = [];
  for (const key in contructorFunc.prototype) {
    keys.push(key);
  }
  return keys.length > 0 ? jasmine.createSpyObj(name, keys) : {};
}
 
export function mockWindow(): Window {
  const mockWindow = mock<any>(Window.prototype);
  mockWindow.location = <Location>{
    href: "",
    hash: ""
  };
  mockWindow.location.replace = (newHref: string) => {
    newHref = newHref || "";
 
    mockWindow.location.href = newHref;
 
    // 'http://www.coveo.com/#foo' => 'foo'
    // 'http://www.coveo.com/#' => ''
    // 'http://www.coveo.com/' => ''
    mockWindow.location.hash = newHref.substring(newHref.indexOf("#") + 1);
 
    // 'foo' => '#foo'
    // '' => ''
    if (mockWindow.location.hash !== "") {
      mockWindow.location.hash = "#" + mockWindow.location.hash;
    }
  };
  mockWindow.addEventListener = jasmine.createSpy("addEventListener");
  mockWindow.removeEventListener = jasmine.createSpy("removeEventListener");
  mockWindow.dispatchEvent = jasmine.createSpy("dispatchEvent");
  return <Window>mockWindow;
}
 
export function mockComponent<T extends BaseComponent>(
  constructorFunc: ConstructorFunc<T>,
  Iname: string = "mock"
): T {
  const m = mock<T>(constructorFunc, name);
  m.type = name;
  return m;
}
 
export function mockSearchInterface(): SearchInterface {
  const m = mockComponent<SearchInterface>(SearchInterface, SearchInterface.ID);
  m.options = {};
  m.options.originalOptionsObject = {};
  m.responsiveComponents = mockResponsiveComponents();
  return m;
}
 
export function mockResponsiveComponents(): IResponsiveComponentsMock {
  return {
    windoh: mockWindow(),
    getMediumScreenWidth: () => 0,
    getSmallScreenWidth: () => 0,
    setMediumScreenWidth: () => 0,
    setSmallScreenWidth: () => 0,
    isSmallScreenWidth: () => false,
    isMediumScreenWidth: () => false,
    isLargeScreenWidth: () => true
  };
}
 
export function mockQueryController(): QueryController {
  const m = mockComponent<QueryController>(QueryController, QueryController.ID);
  const spy = <any>m;
  spy.options = {};
  spy.options.resultsPerPage = 10;
  spy.fetchMore.and.returnValue(new Promise((resolve, reject) => { }));
  spy.getLastQuery.and.returnValue(new QueryBuilder().build());
  return m;
}
 
export function mockSearchEndpoint(): SearchEndpoint {
  const m = mock<any>(SearchEndpoint, "SearchEndpoint");
  m.listFields.and.returnValue(new Promise((resolve, reject) => { }));
  m.listFieldValues.and.returnValue(new Promise((resolve, reject) => { }));
  m.search.and.returnValue(new Promise((resolve, reject) => { }));
  m.getQuerySuggest.and.returnValue(new Promise((resolve, reject) => { }));
  m.extensions.and.returnValue(new Promise((resolve, reject) => { }));
  m.getViewAsDatastreamUri.and.returnValue("http://datastream.uri");
  m.options = {
    queryStringArguments: {
      organizationId: "foobar"
    }
  };
  return m;
}
 
export function mockUsageAnalytics(): IAnalyticsClient {
  const m = mock<any>(NoopAnalyticsClient, "AnalyticsClient");
  m.getTopQueries.and.returnValue(new Promise((resolve, reject) => { }));
  return m;
}
 
export function mockAnalyticsEndpoint(): AnalyticsEndpoint {
  const m = mock<any>(AnalyticsEndpoint, "AnalyticsEndpoint");
  // Spy return Promise instead of void in order to chain Promises
  m.sendCustomEvent.and.returnValue(Promise.resolve(null));
  m.sendDocumentViewEvent.and.returnValue(Promise.resolve(null));
  return m;
}