Skip to content

Commit df81b82

Browse files
authored
Create carousel-href.spec.js
1 parent f4e259a commit df81b82

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Node built-in test (no mocha) – Carousel data-api
2+
const test = require('node:test');
3+
const assert = require('node:assert/strict');
4+
5+
// 设 EXPECT_FAST=1 时启用“<1000ms”断言;未修复时默认只打印时间
6+
const EXPECT_FAST = process.env.EXPECT_FAST === '1';
7+
8+
test('Carousel data-api href sanitize timing', () => {
9+
// 极简 jQuery stub:按事件名记录 handler
10+
const handlers = Object.create(null);
11+
function wrap(raw) {
12+
return {
13+
on(eventName, selectorOrHandler, maybeHandler) {
14+
const h = typeof maybeHandler === 'function' ? maybeHandler
15+
: (typeof selectorOrHandler === 'function' ? selectorOrHandler : null);
16+
if (eventName && h) handlers[eventName] = h;
17+
return this;
18+
},
19+
find() { return { hasClass: () => false }; },
20+
data() { return {}; },
21+
attr(name) { return raw && name === 'href' ? raw._href : null; }
22+
};
23+
}
24+
function $(x) { return wrap(x); }
25+
$.fn = { jquery: '3.4.1' };
26+
$.extend = Object.assign;
27+
global.window = global;
28+
global.document = {};
29+
global.jQuery = global.$ = $;
30+
31+
// 载入“本地(未修复/修复后)”的 bootstrap.js
32+
require('../dist/js/bootstrap.js');
33+
34+
const click = handlers['click.bs.carousel.data-api'] || handlers['click.bs.carousel'];
35+
assert.equal(typeof click, 'function', 'failed to capture carousel handler');
36+
37+
const N = 100000;
38+
const cases = [
39+
{ name: 'nul', s: '\u0000'.repeat(N) + '\u0000' },
40+
{ name: 'digits\\n@', s: '1'.repeat(N) + '\n@' },
41+
//{ name: '=#...@\\r', s: '=' + '#'.repeat(N) + '@\r' },
42+
//{ name: '=#*@\\r', s: '=#'.repeat(N) + '@\r' },
43+
];
44+
45+
for (const { name, s } of cases) {
46+
const t0 = Date.now();
47+
try { click.call({ _href: s }, { preventDefault(){} }); } catch {}
48+
const ms = Date.now() - t0;
49+
console.log(`[carousel] ${name.padEnd(9)} len=${s.length} -> ${ms} ms`);
50+
if (EXPECT_FAST) assert.ok(ms < 2000, `too slow: ${ms}ms`);
51+
}
52+
});

0 commit comments

Comments
 (0)