All files / src/Fake FakeGroupBy.ts

53.33% Statements 24/45
30.56% Branches 11/36
57.14% Functions 4/7
53.33% Lines 24/45
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 1423x   3x           5x 4x 3x   2x 2x 4x                   2x           3x   1x 1x 1x   1x 1x 4x                 1x           3x                           3x                                                                                 3x           8x   8x                 3x               5x                
import { Assert, IGroupByValue, IGroupByResult } from "coveo-search-ui";
 
export function createFakeGroupByResult(
  field: string,
  token: string,
  count: number,
  includeComputedValues?: boolean
): IGroupByResult {
  Assert.isNonEmptyString(field);
  Assert.isNonEmptyString(token);
  Assert.isLargerOrEqualsThan(0, count);
 
  const groupByValues: IGroupByValue[] = [];
  for (var i = 0; i < count; ++i) {
    groupByValues.push(
      createFakeGroupByValue(
        token + i.toString(),
        i + 1,
        100 + i,
        includeComputedValues ? 1000 + i : undefined
      )
    );
  }
 
  return {
    field: field,
    values: groupByValues
  };
}
 
export function createFakeRangeGroupByResult(
  field: string,
  Istart: number = 1,
  Iend: number = 100,
  Isteps: number = 25
): IGroupByResult {
  const groupByValues: IGroupByValue[] = [];
  for (let i = start; i <= end; i += steps) {
    groupByValues.push(
      createFakeGroupByRangeValue(
        i,
        i + (steps - 1),
        "foobar" + i.toString(),
        i
      )
    );
  }
  return {
    field: field,
    values: groupByValues
  };
}
 
export function createFakeHierarchicalValue(
  token: string,
  currentLevel: number,
  delimitingCharacter: string = "|"
): string {
  let value = `level:${currentLevel.toString()}--value:${token}`;
  if (currentLevel !== 0) {
    for (let i = currentLevel - 1; i >= 0; i--) {
      value = `level:${i.toString()}--value:${token}${delimitingCharacter}${value}`;
    }
  }
  return value;
}
 
export function createFakeHierarchicalGroupByResult(
  field: string,
  token: string,
  numberOfLevel: number = 2,
  countByLevel: number = 3,
  delimitingCharacter: string = "|",
  includeComputedValues: boolean = false,
  weirdCasing: boolean = true
): IGroupByResult {
  const groupByValues: IGroupByValue[] = [];
  // i == level
  for (var i = 0; i < numberOfLevel; ++i) {
    // j == values on current level
    for (var j = 0; j < countByLevel; j++) {
      let currentValue = createFakeHierarchicalValue(
        `${token}${j.toString()}`,
        i
      );
      if (weirdCasing) {
        currentValue = _.map(
          currentValue.split(delimitingCharacter),
          (value, k) =>
            (i + j + k) % 2 === 0 ? value.toLowerCase() : value.toUpperCase()
        ).join(delimitingCharacter);
      }
      const currentGroupByValue = createFakeGroupByValue(
        currentValue,
        j + 1,
        100,
        includeComputedValues ? 1000 : undefined
      );
      groupByValues.push(currentGroupByValue);
    }
  }
 
  return {
    field: field,
    values: groupByValues
  };
}
 
export function createFakeGroupByValue(
  token: string,
  count: number,
  score?: number,
  computedValue?: number
): IGroupByValue {
  Assert.isNonEmptyString(token);
 
  return {
    value: token,
    lookupValue: token,
    numberOfResults: count,
    score: score || count * 2,
    computedFieldResults: computedValue ? [computedValue] : undefined
  };
}
 
export function createFakeGroupByRangeValue(
  from: number,
  to: number,
  token: string,
  count: number,
  score?: number,
  computedValue?: number
): IGroupByValue {
  return {
    value: from + ".." + to,
    lookupValue: token,
    numberOfResults: count,
    score: score || count * 2,
    computedFieldResults: computedValue ? [computedValue] : undefined
  };
}