@sanity-labs/client-fake-for-test 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1914,7 +1914,16 @@ const registryKey = (resource) => `${resource.type}:${resource.id}`, datasetReso
1914
1914
  function ensureEntry(registry, resource) {
1915
1915
  const key = registryKey(resource);
1916
1916
  let entry = registry.get(key);
1917
- return entry === void 0 && (entry = { resource, store: createTestStore(), history: createDocumentHistory() }, registry.set(key, entry)), entry;
1917
+ return entry === void 0 && (entry = {
1918
+ resource,
1919
+ store: createTestStore(),
1920
+ history: createDocumentHistory(),
1921
+ exists: !1
1922
+ }, registry.set(key, entry)), entry;
1923
+ }
1924
+ function seedEntry(registry, resource, documents) {
1925
+ const entry = ensureEntry(registry, resource);
1926
+ entry.store.seed(documents), entry.exists = !0;
1918
1927
  }
1919
1928
  function createTestClient(options = {}) {
1920
1929
  const defaultProjectId = options.projectId ?? DEFAULT_PROJECT_ID, defaultDataset = options.dataset ?? DEFAULT_DATASET, backend = {
@@ -1931,16 +1940,16 @@ function createTestClient(options = {}) {
1931
1940
  }, registry = backend.registry;
1932
1941
  if (rejectAmbiguousIdentity("createTestClient()", options), options.token !== void 0 && requireIdentity(backend, options.token, "createTestClient()"), options.resource !== void 0 && validateResource(options.resource), options.documents) {
1933
1942
  const root = options.resource ?? datasetResource(defaultProjectId, defaultDataset);
1934
- ensureEntry(registry, root).store.seed(options.documents);
1943
+ seedEntry(registry, root, options.documents);
1935
1944
  }
1936
1945
  if (options.datasets)
1937
1946
  for (const [key, docs] of Object.entries(options.datasets)) {
1938
1947
  const { projectId, dataset } = parseDatasetKey(key);
1939
- ensureEntry(registry, datasetResource(projectId, dataset)).store.seed(docs);
1948
+ seedEntry(registry, datasetResource(projectId, dataset), docs);
1940
1949
  }
1941
1950
  if (options.resources)
1942
1951
  for (const [key, docs] of Object.entries(options.resources))
1943
- ensureEntry(registry, parseResourceKey(key)).store.seed(docs);
1952
+ seedEntry(registry, parseResourceKey(key), docs);
1944
1953
  return makeHandle({
1945
1954
  backend,
1946
1955
  projectId: defaultProjectId,
@@ -2054,7 +2063,18 @@ function makeHandle(handleArgs) {
2054
2063
  ...accessControl.access !== void 0 ? { access: accessControl.access } : {}
2055
2064
  }
2056
2065
  ] : []
2057
- ], capabilities = capabilitiesOf(target.type), targetLabel = capabilities.label(target.id), { store, history } = ensureEntry(registry, target), concurrentEditKey = (id) => registryKey(target) + " " + id, actorId = () => {
2066
+ ], capabilities = capabilitiesOf(target.type), targetLabel = capabilities.label(target.id), targetEntry = ensureEntry(registry, target), { store, history } = targetEntry, publicStore = {
2067
+ ...store,
2068
+ seed: (documents) => {
2069
+ store.seed(documents), targetEntry.exists = !0;
2070
+ },
2071
+ add: (documents) => {
2072
+ store.add(documents), targetEntry.exists = !0;
2073
+ },
2074
+ put: (doc) => {
2075
+ store.put(doc), targetEntry.exists = !0;
2076
+ }
2077
+ }, concurrentEditKey = (id) => registryKey(target) + " " + id, actorId = () => {
2058
2078
  const access = activeAccess();
2059
2079
  if (access !== void 0)
2060
2080
  return capabilities.identity === "global" ? (access.globalUser ?? access.currentUser).id : access.currentUser.id;
@@ -2068,7 +2088,7 @@ function makeHandle(handleArgs) {
2068
2088
  beforeWrite = void 0, writeRequest = DEFAULT_WRITE;
2069
2089
  }
2070
2090
  }), commitHistory = (transactionId) => {
2071
- beforeWrite !== void 0 && history.commit({ transactionId, author: actorId(), before: beforeWrite, after: store.all() });
2091
+ beforeWrite !== void 0 && (targetEntry.exists = !0, history.commit({ transactionId, author: actorId(), before: beforeWrite, after: store.all() }));
2072
2092
  }, record = (transactionId, mutations, tag) => {
2073
2093
  const effectiveTag = effectiveRequestTag(tag);
2074
2094
  commitHistory(transactionId), backend.log.push({
@@ -2452,7 +2472,9 @@ function makeHandle(handleArgs) {
2452
2472
  actorAccess: () => requireActiveAccess().access,
2453
2473
  actorProjectId: () => token === void 0 ? void 0 : backend.identities.get(token)?.projectId,
2454
2474
  identities: members,
2455
- resources: () => [...registry.values()].map((entry) => entry.resource)
2475
+ // A dataset stands for its project, and every project exists in
2476
+ // the fake, so a dataset handle still counts as before.
2477
+ resources: () => [...registry.values()].filter((e) => e.exists || e.resource.type === "dataset").map((e) => e.resource)
2456
2478
  }
2457
2479
  })
2458
2480
  )
@@ -2483,11 +2505,11 @@ function makeHandle(handleArgs) {
2483
2505
  hasProjectId: hasProjectId || config.projectId !== void 0
2484
2506
  });
2485
2507
  },
2486
- seed: (documents) => store.seed(documents),
2487
- add: (documents) => store.add(documents),
2508
+ seed: (documents) => publicStore.seed(documents),
2509
+ add: (documents) => publicStore.add(documents),
2488
2510
  reset: () => store.reset(),
2489
2511
  snapshot: () => store.snapshot(),
2490
- store: () => store,
2512
+ store: () => publicStore,
2491
2513
  simulateConcurrentEdit: (id, count = 1) => {
2492
2514
  const editKey = concurrentEditKey(id);
2493
2515
  backend.concurrentEdits.set(editKey, (backend.concurrentEdits.get(editKey) ?? 0) + count);