All files / src/Fake FakeResults.ts

18.82% Statements 16/85
12.5% Branches 7/56
25% Functions 2/8
17.86% Lines 15/84
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2192x 2x   2x 3x 6x   3x 3x 12x     3x                                     2x                                                                 13x 13x                                                                                   2x                             2x                                                                                 2x                                                                                                              
import { mockSearchInterface } from "../MockEnvironment";
import { IQueryResult, QueryUtils, IQueryResults } from "coveo-search-ui";
 
export function createFakeResults(
  Icount: number = 10,
  Etoken: string = ""
): IQueryResults {
  const results: IQueryResult[] = [];
  for (let i = 0; i < count; ++i) {
    results.push(createFakeResult(token + i.toString()));
  }
 
  return {
    searchUid: QueryUtils.createGuid(),
    pipeline: "pipeline",
    splitTestRun: "splitTestRunName",
    totalCount: count !== 0 ? count : 0,
    totalCountFiltered: count !== 0 ? count : 0,
    duration: 321,
    indexDuration: 123,
    clientDuration: 456,
    results: results,
    groupByResults: [],
    queryCorrections: [],
    _folded: undefined,
    termsToHighlight: undefined,
    phrasesToHighlight: undefined,
    triggers: []
  };
}
 
export function createFakeResultsWithChildResults(
  count: number = 10,
  numberOfChildResults: number = 5,
  totalNumberOfChildResult: number = 5
): IQueryResults {
  const results: IQueryResult[] = [];
  for (let i = 0; i < count; ++i) {
    results.push(
      createFakeResultWithChildResult(
        i.toString(),
        numberOfChildResults,
        totalNumberOfChildResult
      )
    );
  }
 
  return {
    searchUid: QueryUtils.createGuid(),
    totalCount: count !== 0 ? count + 1 : 0,
    totalCountFiltered: count !== 0 ? count + 1 : 0,
    duration: 321,
    indexDuration: 123,
    clientDuration: 456,
    results: results,
    groupByResults: [],
    queryCorrections: [],
    _folded: undefined,
    termsToHighlight: undefined,
    phrasesToHighlight: undefined,
    triggers: []
  };
}
 
export function createFakeResult(Itoken: string = "foo"): IQueryResult {
  return <IQueryResult>{
    title: "Title" + token,
    titleHighlights: [],
    uri: "http://uri." + token + ".com",
    printableUri: "http://printable.uri." + token + ".com",
    printableUriHighlights: [],
    clickUri: "http://click.uri." + token + ".com",
    uniqueId: "uniqueId" + token,
    excerpt: "excerpt" + token,
    excerptHighlights: [],
    firstSentences: "firstSentences" + token,
    firstSentencesHighlights: [],
    hasHtmlVersion: true,
    hasMobileHtmlVersion: true,
    flags: "HasThumbnail",
    summary: "summary" + token,
    summaryHighlights: [],
    rankingInfo: "",
    raw: {
      string: "string value",
      date: new Date(1980, 2, 11, 8, 30).valueOf(),
      number: 123,
      emails: "mlaporte@coveo.com;dlavoie@coveo.com",
      empty: "",
      randomNumber: Math.random(),
      urihash: QueryUtils.createGuid(),
      source: "the source",
      collection: "the collection",
      author: "o.o"
    },
    childResults: [],
    termsToHighlight: {},
    phrasesToHighlight: {},
    index: 0,
    queryUid: "the uid",
    rating: 3,
    state: {},
    isRecommendation: false,
    searchInterface: mockSearchInterface()
  };
}
 
export function createFakeResultWithChildResult(
  token: string,
  numberOfChildResult: number,
  totalNumberOfChildResult: number = 5
): IQueryResult {
  const childResults: IQueryResult[] = [];
  for (var i = 0; i < numberOfChildResult; i++) {
    childResults.push(createFakeResult(token + "-child" + i));
  }
  let ret = createFakeResult(token);
  ret.totalNumberOfChildResults = totalNumberOfChildResult;
  ret = _.extend(ret, { childResults: childResults });
  return ret;
}
 
export function createFakeResultWithAttachments(
  token: string = "test",
  numberOfAttachments: number = 3,
  attachmentType: string[] = ["xml", "pdf", "txt"],
  flags: string = "HasThumbnail",
  attachmentsFlags: string[] = ["IsAttachment", "IsAttachment", "IsAttachment"],
  withSubAttachments: boolean = false
): IQueryResult {
  const fake = createFakeResult(token);
  fake.flags = flags;
  if (withSubAttachments) {
    const subAttachments = [];
    for (var i = 0; i < numberOfAttachments; i++) {
      subAttachments.push(
        createFakeResultWithAttachments(
          "test1",
          3,
          undefined,
          undefined,
          undefined,
          false
        )
      );
    }
    fake.attachments = subAttachments;
  } else {
    fake.attachments = createFakeResults(numberOfAttachments).results;
  }
  _.each(attachmentType, (type, index, list) => {
    if (fake.attachments[index] !== undefined) {
      fake.attachments[index].raw["filetype"] = type;
    }
  });
  _.each(attachmentsFlags, (flag, index, list) => {
    if (fake.attachments[index] !== undefined) {
      fake.attachments[index].flags = flag;
    }
  });
  return fake;
}
 
export function createFakeFeedItemResult(
  token: string,
  nbLikes: number = 0,
  nbTopics: number = 0,
  hasAttachment: boolean = false
): IQueryResult {
  const result = createFakeResult(token);
  result.raw.sfparentid = "parentid";
  result.raw.sfparentname = "parentname";
  result.raw.sffeeditemid = token + "id";
  result.clickUri = "myURI/" + result.raw.sffeeditemid;
  result.raw.sfcreatedby = "createdby";
  result.raw.sfcreatedbyname = "createdby";
  result.raw.sfcreatedbyid = "createdbyid";
  result.raw.sfinsertedbyid = "createdbyid";
 
  // Generate likes
  if (nbLikes > 0) {
    result.raw.sflikecount = nbLikes;
    result.raw.sflikedby = "";
    result.raw.sflikedbyid = "";
 
    for (let i = 1; i <= nbLikes; i++) {
      result.raw.sflikedby += "LikeName" + i;
      result.raw.sflikedbyid += "LikeId" + i;
 
      if (i !== nbLikes) {
        result.raw.sflikedby += ";";
        result.raw.sflikedbyid += ";";
      }
    }
  }
 
  // Generate topics
  if (nbTopics > 0) {
    result.raw.coveochatterfeedtopics = "";
 
    for (let i = 1; i <= nbTopics; i++) {
      result.raw.coveochatterfeedtopics += "topic" + i;
 
      if (i !== nbTopics) {
        result.raw.coveochatterfeedtopics += ";";
      }
    }
  }
 
  // Generate post attachment
  if (hasAttachment) {
    result.raw.coveochatterfeedtopics = "PostAttachment";
    result.raw.sfcontentfilename = "fileName";
    result.raw.sfcontentversionid = token;
  }
 
  return result;
}