CodeSampleX

示例

@testing-library/user-event 14.6.1: userEvent.click

已验证示例 — npm @testing-library/user-event 14.6.1: userEvent.click. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: userEvent.setup creates an instance…

sha256:f22d624c20a49fd2f37d906fa55118f0d4d3ad5f2f8c0b3e7965e91fa54a9ff2

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-12

案例

HOW
目标
verify userEvent.click in pkg:npm/%40testing-library/user-event@14.6.1
包
符号
  • userEvent.click
创建时间
2026-09-12T02:33:51Z

契约

  1. userEvent.setup creates an instance that dispatches pointer, mouse, and click event sequence on click
  2. userEvent.click directly dispatches click event on element
  3. userEvent.click skips click event on disabled button element

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify userEvent.click in pkg:npm/%40testing-library/user-event@14.6.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40testing-library/user-event@14.6.1
Demonstrate these symbols/APIs:
  - userEvent.click

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:9ddcf859022bbe9a5c1dde8c4ed6cee5f375f1894acfc698a4dd99e7e24a1114","contract":["userEvent.setup creates an instance that dispatches pointer, mouse, and click event sequence on click","userEvent.click directly dispatches click event on element","userEvent.click skips click event on disabled button element"],"goal":"verify userEvent.click in pkg:npm/%40testing-library/user-event@14.6.1","kind":"HOW","packages":["pkg:npm/%40testing-library/user-event@14.6.1"],"schemaVersion":1,"symbols":["userEvent.click"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40testing-library/user-event@14.6.1"],"schemaVersion":1,"subject":"pkg:npm/%40testing-library/user-event@14.6.1","symbols":["userEvent.click"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "user-event-click-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "user-event-click-sample",
      "version": "1.0.0",
      "dependencies": {
        "@testing-library/user-event": "14.6.1"
      }
    },
    "node_modules/@babel/code-frame": {
      "version": "7.29.7",
      "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.7.tgz",
      "integrity": "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw==",
      "license": "MIT",
      "peer": true,
      "dependencies": {
        "@babel/helper-validator-identifier": "^7.29.7",
        "js-tokens": "^4.0.0",
        "picocolors": "^1.1.1"
      },
      "engines": {
        "node": ">=6.9.0"
      }
    },
    "node_modules/@babel/helper-validator-identifier": {
      "version": "7.29.7",
      "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz",
      "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=6.9.0"
      }
    },
    "node_modules/@babel/runtime": {
      "version": "7.29.7",
      "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.7.tgz",
      "integrity": "sha512-Nq8OhGWiZIZGV6hLHoyAKLLcJihP/xFeBMGJoUrxTX2psI8dCifzLhZISFb+VWS3wFMRDmCGw5R+dOySCqPLhw==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=6.9.0"
      }
    },
    "node_modules/@testing-library/dom": {
      "version": "10.4.1",
      "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.1.tgz",
      "integrity": "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg==",
      "license": "MIT",
      "peer": true,
      "dependencies": {
        "@babel/code-frame": "^7.10.4",
        "@babel/runtime": "^7.12.5",
        "@types/aria-query": "^5.0.1",
        "aria-query": "5.3.0",
        "dom-accessibility-api": "^0.5.9",
        "lz-string": "^1.5.0",
        "picocolors": "1.1.1",
        "pretty-format": "^27.0.2"
      },
      "engines": {
        "node": ">=18"
      }
    },
    "node_modules/@testing-library/user-event": {
      "version": "14.6.1",
      "resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-14.6.1.tgz",
      "integrity": "sha512-vq7fv0rnt+QTXgPxr5Hjc210p6YKq2kmdziLgnsZGgLJ9e6VAShx1pACLuRjd/AS/sr7phAR58OIIpf0LlmQNw==",
      "license": "MIT",
      "engines": {
        "node": ">=12",
        "npm": ">=6"
      },
      "peerDependencies": {
        "@testing-library/dom": ">=7.21.4"
      }
    },
    "node_modules/@types/aria-query": {
      "version": "5.0.4",
      "resolved": "https://registry.npmjs.org/@types/aria-query/-/aria-query-5.0.4.tgz",
      "integrity": "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==",
      "license": "MIT",
      "peer": true
    },
    "node_modules/ansi-regex": {
      "version": "5.0.1",
      "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
      "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/ansi-styles": {
      "version": "5.2.0",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-5.2.0.tgz",
      "integrity": "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    },
    "node_modules/aria-query": {
      "version": "5.3.0",
      "resolved": "https://registry.npmjs.org/aria-query/-/aria-query-5.3.0.tgz",
      "integrity": "sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A==",
      "license": "Apache-2.0",
      "peer": true,
      "dependencies": {
        "dequal": "^2.0.3"
      }
    },
    "node_modules/dequal": {
      "version": "2.0.3",
      "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz",
      "integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=6"
      }
    },
    "node_modules/dom-accessibility-api": {
      "version": "0.5.16",
      "resolved": "https://registry.npmjs.org/dom-accessibility-api/-/dom-accessibility-api-0.5.16.tgz",
      "integrity": "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg==",
      "license": "MIT",
      "peer": true
    },
    "node_modules/js-tokens": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
      "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
      "license": "MIT",
      "peer": true
    },
    "node_modules/lz-string": {
      "version": "1.5.0",
      "resolved": "https://registry.npmjs.org/lz-string/-/lz-string-1.5.0.tgz",
      "integrity": "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==",
      "license": "MIT",
      "peer": true,
      "bin": {
        "lz-string": "bin/bin.js"
      }
    },
    "node_modules/picocolors": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
      "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==",
      "license": "ISC",
      "peer": true
    },
    "node_modules/pretty-format": {
      "version": "27.5.1",
      "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-27.5.1.tgz",
      "integrity": "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ==",
      "license": "MIT",
      "peer": true,
      "dependencies": {
        "ansi-regex": "^5.0.1",
        "ansi-styles": "^5.0.0",
        "react-is": "^17.0.1"
      },
      "engines": {
        "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
      }
    },
    "node_modules/react-is": {
      "version": "17.0.2",
      "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz",
      "integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==",
      "license": "MIT",
      "peer": true
    }
  }
}
package.json
{
  "name": "user-event-click-sample",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for userEvent.click in @testing-library/user-event",
  "dependencies": {
    "@testing-library/user-event": "14.6.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify userEvent.click in pkg:npm/%40testing-library/user-event@14.6.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40testing-library/user-event@14.6.1"
  ],
  "symbols": [
    "userEvent.click"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { userEvent } from '@testing-library/user-event';

// Minimal self-contained DOM environment for Node.js clean-room execution
class MockEventTarget {
  constructor() {
    this._listeners = {};
  }
  addEventListener(type, listener, options) {
    if (!this._listeners[type]) this._listeners[type] = [];
    this._listeners[type].push(listener);
  }
  removeEventListener(type, listener, options) {
    if (!this._listeners[type]) return;
    this._listeners[type] = this._listeners[type].filter(l => l !== listener);
  }
  dispatchEvent(event) {
    Object.defineProperty(event, 'target', { value: this, configurable: true, writable: true });
    Object.defineProperty(event, 'currentTarget', { value: this, configurable: true, writable: true });
    const list = this._listeners[event.type] || [];
    for (const l of list) {
      if (typeof l === 'function') l(event);
      else if (l && typeof l.handleEvent === 'function') l.handleEvent(event);
    }
    return !event.defaultPrevented;
  }
}

class MockElement extends MockEventTarget {
  constructor(tagName, ownerDocument) {
    super();
    this.tagName = tagName.toUpperCase();
    this.nodeType = 1;
    this.ownerDocument = ownerDocument;
    this.disabled = false;
    this.children = [];
    this.parentElement = null;
    this.style = {};
  }
  focus() {}
  blur() {}
  hasAttribute(name) { return name === 'disabled' ? Boolean(this.disabled) : false; }
  getAttribute(name) { return this.hasAttribute(name) ? '' : null; }
  setAttribute(name, val) { if (name === 'disabled') this.disabled = true; }
  removeAttribute(name) { if (name === 'disabled') this.disabled = false; }
  matches(selector) { return false; }
  closest(selector) { return null; }
  querySelector(selector) { return null; }
  querySelectorAll(selector) { return []; }
  getBoundingClientRect() {
    return { top: 0, left: 0, right: 100, bottom: 30, width: 100, height: 30, x: 0, y: 0 };
  }
  contains(node) {
    return node === this;
  }
}

class MockDocument extends MockEventTarget {
  constructor() {
    super();
    this.nodeType = 9;
    this.visibilityState = 'visible';
    this.documentElement = new MockElement('HTML', this);
    this.body = new MockElement('BODY', this);
    this.documentElement.parentElement = null;
    this.body.parentElement = this.documentElement;
    this.activeElement = this.body;
    this.defaultView = null;
  }
  createElement(tag) {
    return new MockElement(tag, this);
  }
  createRange() {
    return {
      setStart: () => {},
      setEnd: () => {},
      commonAncestorContainer: this.body
    };
  }
  elementFromPoint(x, y) {
    return this.body;
  }
  hasAttribute() { return false; }
}

const mockDoc = new MockDocument();

class CustomEventClass extends globalThis.Event {
  constructor(type, init = {}) {
    super(type, init);
  }
}
class MockMouseEvent extends CustomEventClass {}
class MockPointerEvent extends CustomEventClass {}
class MockUIEvent extends CustomEventClass {}

const mockWindow = {
  document: mockDoc,
  Event: CustomEventClass,
  UIEvent: MockUIEvent,
  MouseEvent: MockMouseEvent,
  PointerEvent: MockPointerEvent,
  EventTarget: globalThis.EventTarget,
  navigator: { clipboard: {} },
  HTMLElement: MockElement,
  Element: MockElement,
  Node: { ELEMENT_NODE: 1, DOCUMENT_NODE: 9 },
  getComputedStyle: () => ({
    visibility: 'visible',
    display: 'block',
    pointerEvents: 'auto'
  })
};
mockDoc.defaultView = mockWindow;

globalThis.window = mockWindow;
globalThis.document = mockDoc;
globalThis.HTMLElement = MockElement;
globalThis.Element = MockElement;
globalThis.getComputedStyle = mockWindow.getComputedStyle;

// 1. userEvent.setup creates an instance that dispatches pointer, mouse, and click event sequence on click
{
  const user = userEvent.setup({ document: mockDoc });
  const button = mockDoc.createElement('button');
  mockDoc.body.children.push(button);
  button.parentElement = mockDoc.body;

  const events = [];
  button.addEventListener('pointerdown', e => events.push(e.type));
  button.addEventListener('mousedown', e => events.push(e.type));
  button.addEventListener('pointerup', e => events.push(e.type));
  button.addEventListener('mouseup', e => events.push(e.type));
  button.addEventListener('click', e => events.push(e.type));

  await user.click(button);
  assert.deepStrictEqual(events, ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']);
}

// 2. userEvent.click directly dispatches click event on element
{
  const button = mockDoc.createElement('button');
  mockDoc.body.children.push(button);
  button.parentElement = mockDoc.body;

  let clicked = false;
  button.addEventListener('click', () => {
    clicked = true;
  });

  await userEvent.click(button);
  assert.strictEqual(clicked, true, 'direct userEvent.click must dispatch click event');
}

// 3. userEvent.click skips click event on disabled button element
{
  const button = mockDoc.createElement('button');
  button.disabled = true;
  mockDoc.body.children.push(button);
  button.parentElement = mockDoc.body;

  let clicked = false;
  button.addEventListener('click', () => {
    clicked = true;
  });

  await userEvent.click(button);
  assert.strictEqual(clicked, false, 'userEvent.click must not dispatch click on disabled element');
}

console.log('Contract passed');

原始种子者

匿名