Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

2.0 KB
Raw Blame
import fetchMock from "fetch-mock";
import { match } from "ts-pattern";

import type { EmbeddingDataPicker } from "metabase/redux/store/embedding-data-picker";
import type { CollectionItem, SearchResult } from "metabase-types/api";

export function setupSearchEndpoints(
  items: (CollectionItem | SearchResult)[],
  delay?: number,
) {
  const name = "search";
  fetchMock.removeRoute(name);
  fetchMock.get(
    "path:/api/search",
    (call) => {
      const url = new URL(call.url);
      const models = url.searchParams.getAll("models");
      const limit = Number(url.searchParams.get("limit")) || 50;
      const offset = Number(url.searchParams.get("offset"));
      const table_db_id = url.searchParams.get("table_db_id") || null;
      const queryText = url.searchParams.get("q")?.toLowerCase();

      let matchedItems = items.filter(
        ({ name }) =>
          !queryText || name.toLowerCase().includes(queryText.toLowerCase()),
      );

      const availableModels = [
        ...new Set(matchedItems.map(({ model }) => model)),
      ];

      matchedItems = matchedItems.filter(
        ({ model }) => !models.length || models.includes(model),
      );
      return {
        data: matchedItems.slice(offset, offset + limit),
        total: matchedItems.length,
        models,
        available_models: availableModels,
        limit,
        offset,
        table_db_id,
      };
    },
    { name, delay },
  );
}

export function setupSearchErrorEndpoint({ status = 400 } = {}) {
  const name = "search";
  fetchMock.removeRoute(name);
  fetchMock.get("path:/api/search", { status }, { name });
}

export function setupEmbeddingDataPickerDecisionEndpoints(
  dataPicker: EmbeddingDataPicker,
) {
  const mockEntityCount = match(dataPicker)
    .with("flat", () => 10)
    .with("staged", () => 100)
    .exhaustive();

  fetchMock.get({
    name: "entity-count",
    url: "path:/api/search",
    query: {
      models: ["dataset", "table"],
      limit: 0,
    },
    response: {
      total: mockEntityCount,
    },
  });
}