@gitlab/frontend-tests 0.1.0 → 0.1.3

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.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var server = require('./server-otsK-xxA.cjs');
3
+ var server = require('./server-Bv38oMzv.cjs');
4
4
  require('msw/node');
5
5
  require('msw');
6
6
  require('node:fs');
@@ -26,6 +26,7 @@ exports.getVariant = server.getVariant;
26
26
  exports.isValidGraphqlFixture = server.isValidGraphqlFixture;
27
27
  exports.lastRequestVariables = server.lastRequestVariables;
28
28
  exports.loadFixturesMap = server.loadFixturesMap;
29
+ exports.mapStaticOperationHandlers = server.mapStaticOperationHandlers;
29
30
  exports.matchFixture = server.matchFixture;
30
31
  exports.missingOperations = server.missingOperations;
31
32
  exports.normaliseGraphqlFixture = server.normaliseGraphqlFixture;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as activateVariant, b as baseMetadata, c as buildHandlers, d as captureMissingOperation, e as captureRequest, f as capturedRequests, g as clearMissingOperations, h as createFixturePaginator, i as createServer, j as defineFixtureVariants, k as defineRestFixtureVariants, l as dumpRegistry, m as expectGraphQLCalls, n as getActiveVariant, o as getVariant, p as isValidGraphqlFixture, q as lastRequestVariables, r as loadFixturesMap, s as matchFixture, t as missingOperations, u as normaliseGraphqlFixture, v as resetAllVariants, w as resetCapturedRequests, x as setFixtureData, y as setFixtureErrors, z as setFixtureItemsCount, A as setQueryVariant, B as snapshotRequests } from './server-Ds9SYBSn.js';
1
+ export { a as activateVariant, b as baseMetadata, c as buildHandlers, d as captureMissingOperation, e as captureRequest, f as capturedRequests, g as clearMissingOperations, h as createFixturePaginator, i as createServer, j as defineFixtureVariants, k as defineRestFixtureVariants, l as dumpRegistry, m as expectGraphQLCalls, n as getActiveVariant, o as getVariant, p as isValidGraphqlFixture, q as lastRequestVariables, r as loadFixturesMap, s as mapStaticOperationHandlers, t as matchFixture, u as missingOperations, v as normaliseGraphqlFixture, w as resetAllVariants, x as resetCapturedRequests, y as setFixtureData, z as setFixtureErrors, A as setFixtureItemsCount, B as setQueryVariant, C as snapshotRequests } from './server-CrJeYlNb.js';
2
2
  import 'msw/node';
3
3
  import 'msw';
4
4
  import 'node:fs';
@@ -1,5 +1,18 @@
1
1
  import { type RequestHandler } from 'msw';
2
2
  import type { FeatureHandler, RestEndpoint } from './types.cjs';
3
+ /**
4
+ * Turns a fixtures map into the static handlers a feature's `OPERATION_HANDLERS`
5
+ * starts from: one per operation, serving the active variant when a spec has set
6
+ * one and the recorded fixture otherwise. Spread the result first, so handlers
7
+ * that read request variables can override the static entry for their operation.
8
+ *
9
+ * @example
10
+ * const OPERATION_HANDLERS = {
11
+ * ...mapStaticOperationHandlers(fixtures),
12
+ * getWorkItemsFull: ({ variables }) => listResponse(variables),
13
+ * };
14
+ */
15
+ export declare function mapStaticOperationHandlers<Fixture>(fixtures: Record<string, Fixture>): Record<string, () => Fixture>;
3
16
  /**
4
17
  * Assembles the MSW handlers for a test run from the feature handlers and REST
5
18
  * endpoints a consumer registers. Every answered request is captured for
@@ -1,5 +1,18 @@
1
1
  import { type RequestHandler } from 'msw';
2
2
  import type { FeatureHandler, RestEndpoint } from './types.js';
3
+ /**
4
+ * Turns a fixtures map into the static handlers a feature's `OPERATION_HANDLERS`
5
+ * starts from: one per operation, serving the active variant when a spec has set
6
+ * one and the recorded fixture otherwise. Spread the result first, so handlers
7
+ * that read request variables can override the static entry for their operation.
8
+ *
9
+ * @example
10
+ * const OPERATION_HANDLERS = {
11
+ * ...mapStaticOperationHandlers(fixtures),
12
+ * getWorkItemsFull: ({ variables }) => listResponse(variables),
13
+ * };
14
+ */
15
+ export declare function mapStaticOperationHandlers<Fixture>(fixtures: Record<string, Fixture>): Record<string, () => Fixture>;
3
16
  /**
4
17
  * Assembles the MSW handlers for a test run from the feature handlers and REST
5
18
  * endpoints a consumer registers. Every answered request is captured for
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var server = require('./server-otsK-xxA.cjs');
3
+ var server = require('./server-Bv38oMzv.cjs');
4
4
  require('msw/node');
5
5
  require('msw');
6
6
  require('node:fs');
@@ -26,6 +26,7 @@ exports.getVariant = server.getVariant;
26
26
  exports.isValidGraphqlFixture = server.isValidGraphqlFixture;
27
27
  exports.lastRequestVariables = server.lastRequestVariables;
28
28
  exports.loadFixturesMap = server.loadFixturesMap;
29
+ exports.mapStaticOperationHandlers = server.mapStaticOperationHandlers;
29
30
  exports.matchFixture = server.matchFixture;
30
31
  exports.missingOperations = server.missingOperations;
31
32
  exports.normaliseGraphqlFixture = server.normaliseGraphqlFixture;
@@ -1,4 +1,4 @@
1
- export { a as activateVariant, b as baseMetadata, c as buildHandlers, d as captureMissingOperation, e as captureRequest, f as capturedRequests, g as clearMissingOperations, h as createFixturePaginator, i as createServer, j as defineFixtureVariants, k as defineRestFixtureVariants, l as dumpRegistry, m as expectGraphQLCalls, n as getActiveVariant, o as getVariant, p as isValidGraphqlFixture, q as lastRequestVariables, r as loadFixturesMap, s as matchFixture, t as missingOperations, u as normaliseGraphqlFixture, v as resetAllVariants, w as resetCapturedRequests, x as setFixtureData, y as setFixtureErrors, z as setFixtureItemsCount, A as setQueryVariant, B as snapshotRequests } from './server-Ds9SYBSn.js';
1
+ export { a as activateVariant, b as baseMetadata, c as buildHandlers, d as captureMissingOperation, e as captureRequest, f as capturedRequests, g as clearMissingOperations, h as createFixturePaginator, i as createServer, j as defineFixtureVariants, k as defineRestFixtureVariants, l as dumpRegistry, m as expectGraphQLCalls, n as getActiveVariant, o as getVariant, p as isValidGraphqlFixture, q as lastRequestVariables, r as loadFixturesMap, s as mapStaticOperationHandlers, t as matchFixture, u as missingOperations, v as normaliseGraphqlFixture, w as resetAllVariants, x as resetCapturedRequests, y as setFixtureData, z as setFixtureErrors, A as setFixtureItemsCount, B as setQueryVariant, C as snapshotRequests } from './server-CrJeYlNb.js';
2
2
  import 'msw/node';
3
3
  import 'msw';
4
4
  import 'node:fs';
@@ -3493,11 +3493,21 @@ const DEFAULT_VARIABLE_NAMES = {
3493
3493
  };
3494
3494
  function splitGraphqlId(id) {
3495
3495
  const match = String(id).match(/^(.*\/)(\d+)$/);
3496
- if (!match) {
3497
- throw new Error(`Cannot synthesize a new id from "${id}": expected a "prefix/number" format`);
3496
+ return match ? { prefix: match[1], number: Number(match[2]) } : null;
3497
+ }
3498
+ // Opaque ids (e.g. a finding `uuid`) have no number to count up from, so they get a suffix.
3499
+ function idSynthesizer(ids) {
3500
+ const parsed = ids.map(splitGraphqlId);
3501
+ if (parsed.some((entry) => entry === null)) {
3502
+ const templateId = ids[ids.length - 1];
3503
+ return (index) => `${templateId}-${index + 1}`;
3498
3504
  }
3499
- const [, prefix, numericPart] = match;
3500
- return { prefix, number: Number(numericPart) };
3505
+ const numeric = parsed;
3506
+ const { prefix } = numeric[numeric.length - 1];
3507
+ // Recorded lists are often sorted DESC, so count up from the highest id rather than
3508
+ // the last one, or the new ids collide with recorded nodes.
3509
+ const highestId = Math.max(...numeric.map(({ number }) => number));
3510
+ return (index) => `${prefix}${highestId + index + 1}`;
3501
3511
  }
3502
3512
  // A connection lists its items as `nodes`, as Relay-style `edges` wrapping each node, or both.
3503
3513
  const ITEM_LISTS = [
@@ -3515,14 +3525,11 @@ function resizeItems(items, itemCount, nodeOf) {
3515
3525
  return items.slice(0, itemCount);
3516
3526
  }
3517
3527
  const template = items[items.length - 1];
3518
- const { prefix } = splitGraphqlId(nodeOf(template).id);
3519
- // Recorded lists are often sorted DESC, so count up from the highest id rather than
3520
- // the last one, or the new ids collide with recorded nodes.
3521
- const highestId = Math.max(...items.map((item) => splitGraphqlId(nodeOf(item).id).number));
3528
+ const nextId = idSynthesizer(items.map((item) => nodeOf(item).id));
3522
3529
  const filled = Array.from({ length: itemCount - items.length }, (_, index) => {
3523
3530
  const clone = cloneDeep(template);
3524
3531
  const node = nodeOf(clone);
3525
- node.id = `${prefix}${highestId + index + 1}`;
3532
+ node.id = nextId(index);
3526
3533
  return clone;
3527
3534
  });
3528
3535
  return [...items, ...filled];
@@ -3922,6 +3929,24 @@ function dumpRegistry() {
3922
3929
  return out;
3923
3930
  }
3924
3931
 
3932
+ /**
3933
+ * Turns a fixtures map into the static handlers a feature's `OPERATION_HANDLERS`
3934
+ * starts from: one per operation, serving the active variant when a spec has set
3935
+ * one and the recorded fixture otherwise. Spread the result first, so handlers
3936
+ * that read request variables can override the static entry for their operation.
3937
+ *
3938
+ * @example
3939
+ * const OPERATION_HANDLERS = {
3940
+ * ...mapStaticOperationHandlers(fixtures),
3941
+ * getWorkItemsFull: ({ variables }) => listResponse(variables),
3942
+ * };
3943
+ */
3944
+ function mapStaticOperationHandlers(fixtures) {
3945
+ return Object.fromEntries(Object.entries(fixtures).map(([operationName, fixture]) => [
3946
+ operationName,
3947
+ () => getActiveVariant(operationName) ?? fixture,
3948
+ ]));
3949
+ }
3925
3950
  /**
3926
3951
  * Assembles the MSW handlers for a test run from the feature handlers and REST
3927
3952
  * endpoints a consumer registers. Every answered request is captured for
@@ -3980,6 +4005,7 @@ exports.getVariant = getVariant;
3980
4005
  exports.isValidGraphqlFixture = isValidGraphqlFixture;
3981
4006
  exports.lastRequestVariables = lastRequestVariables;
3982
4007
  exports.loadFixturesMap = loadFixturesMap;
4008
+ exports.mapStaticOperationHandlers = mapStaticOperationHandlers;
3983
4009
  exports.matchFixture = matchFixture;
3984
4010
  exports.missingOperations = missingOperations;
3985
4011
  exports.normaliseGraphqlFixture = normaliseGraphqlFixture;
@@ -3491,11 +3491,21 @@ const DEFAULT_VARIABLE_NAMES = {
3491
3491
  };
3492
3492
  function splitGraphqlId(id) {
3493
3493
  const match = String(id).match(/^(.*\/)(\d+)$/);
3494
- if (!match) {
3495
- throw new Error(`Cannot synthesize a new id from "${id}": expected a "prefix/number" format`);
3494
+ return match ? { prefix: match[1], number: Number(match[2]) } : null;
3495
+ }
3496
+ // Opaque ids (e.g. a finding `uuid`) have no number to count up from, so they get a suffix.
3497
+ function idSynthesizer(ids) {
3498
+ const parsed = ids.map(splitGraphqlId);
3499
+ if (parsed.some((entry) => entry === null)) {
3500
+ const templateId = ids[ids.length - 1];
3501
+ return (index) => `${templateId}-${index + 1}`;
3496
3502
  }
3497
- const [, prefix, numericPart] = match;
3498
- return { prefix, number: Number(numericPart) };
3503
+ const numeric = parsed;
3504
+ const { prefix } = numeric[numeric.length - 1];
3505
+ // Recorded lists are often sorted DESC, so count up from the highest id rather than
3506
+ // the last one, or the new ids collide with recorded nodes.
3507
+ const highestId = Math.max(...numeric.map(({ number }) => number));
3508
+ return (index) => `${prefix}${highestId + index + 1}`;
3499
3509
  }
3500
3510
  // A connection lists its items as `nodes`, as Relay-style `edges` wrapping each node, or both.
3501
3511
  const ITEM_LISTS = [
@@ -3513,14 +3523,11 @@ function resizeItems(items, itemCount, nodeOf) {
3513
3523
  return items.slice(0, itemCount);
3514
3524
  }
3515
3525
  const template = items[items.length - 1];
3516
- const { prefix } = splitGraphqlId(nodeOf(template).id);
3517
- // Recorded lists are often sorted DESC, so count up from the highest id rather than
3518
- // the last one, or the new ids collide with recorded nodes.
3519
- const highestId = Math.max(...items.map((item) => splitGraphqlId(nodeOf(item).id).number));
3526
+ const nextId = idSynthesizer(items.map((item) => nodeOf(item).id));
3520
3527
  const filled = Array.from({ length: itemCount - items.length }, (_, index) => {
3521
3528
  const clone = cloneDeep(template);
3522
3529
  const node = nodeOf(clone);
3523
- node.id = `${prefix}${highestId + index + 1}`;
3530
+ node.id = nextId(index);
3524
3531
  return clone;
3525
3532
  });
3526
3533
  return [...items, ...filled];
@@ -3920,6 +3927,24 @@ function dumpRegistry() {
3920
3927
  return out;
3921
3928
  }
3922
3929
 
3930
+ /**
3931
+ * Turns a fixtures map into the static handlers a feature's `OPERATION_HANDLERS`
3932
+ * starts from: one per operation, serving the active variant when a spec has set
3933
+ * one and the recorded fixture otherwise. Spread the result first, so handlers
3934
+ * that read request variables can override the static entry for their operation.
3935
+ *
3936
+ * @example
3937
+ * const OPERATION_HANDLERS = {
3938
+ * ...mapStaticOperationHandlers(fixtures),
3939
+ * getWorkItemsFull: ({ variables }) => listResponse(variables),
3940
+ * };
3941
+ */
3942
+ function mapStaticOperationHandlers(fixtures) {
3943
+ return Object.fromEntries(Object.entries(fixtures).map(([operationName, fixture]) => [
3944
+ operationName,
3945
+ () => getActiveVariant(operationName) ?? fixture,
3946
+ ]));
3947
+ }
3923
3948
  /**
3924
3949
  * Assembles the MSW handlers for a test run from the feature handlers and REST
3925
3950
  * endpoints a consumer registers. Every answered request is captured for
@@ -3960,4 +3985,4 @@ function buildHandlers(allFeatureHandlers, allRestEndpoints) {
3960
3985
  /** Builds the MSW node server for a test run from the consumer's feature handlers and REST endpoints. */
3961
3986
  const createServer = (featureHandlers, restEndpoints) => setupServer(...buildHandlers(featureHandlers, restEndpoints));
3962
3987
 
3963
- export { setQueryVariant as A, snapshotRequests as B, activateVariant as a, baseMetadata as b, buildHandlers as c, captureMissingOperation as d, captureRequest as e, capturedRequests as f, clearMissingOperations as g, createFixturePaginator as h, createServer as i, defineFixtureVariants as j, defineRestFixtureVariants as k, dumpRegistry as l, expectGraphQLCalls as m, getActiveVariant as n, getVariant as o, isValidGraphqlFixture as p, lastRequestVariables as q, loadFixturesMap as r, matchFixture as s, missingOperations as t, normaliseGraphqlFixture as u, resetAllVariants as v, resetCapturedRequests as w, setFixtureData as x, setFixtureErrors as y, setFixtureItemsCount as z };
3988
+ export { setFixtureItemsCount as A, setQueryVariant as B, snapshotRequests as C, activateVariant as a, baseMetadata as b, buildHandlers as c, captureMissingOperation as d, captureRequest as e, capturedRequests as f, clearMissingOperations as g, createFixturePaginator as h, createServer as i, defineFixtureVariants as j, defineRestFixtureVariants as k, dumpRegistry as l, expectGraphQLCalls as m, getActiveVariant as n, getVariant as o, isValidGraphqlFixture as p, lastRequestVariables as q, loadFixturesMap as r, mapStaticOperationHandlers as s, matchFixture as t, missingOperations as u, normaliseGraphqlFixture as v, resetAllVariants as w, resetCapturedRequests as x, setFixtureData as y, setFixtureErrors as z };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitlab/frontend-tests",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Reusable test infrastructure for GitLab frontend packages",
5
5
  "license": "MIT",
6
6
  "type": "module",