示例工程 21-web-components
21-web-components/02-slots-styles/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { groupBySlot, normalizeSlotName, run } from './main.js';
test('slot 名字规范化:null/undefined 视为默认插槽', () => {
assert.equal(normalizeSlotName(null), '');
assert.equal(normalizeSlotName(undefined), '');
assert.equal(normalizeSlotName('title'), 'title');
});
test('按插槽分组', () => {
assert.deepEqual(
groupBySlot([
{ slot: 'title', label: 'a' },
{ slot: null, label: 'b' },
{ slot: 'title', label: 'c' },
]),
{ title: ['a', 'c'], '': ['b'] },
);
});
test('空列表返回空对象', () => {
assert.deepEqual(groupBySlot([]), {});
});
test('Node 里调用 run 给出环境错误', () => {
assert.throws(() => run(() => {}), /需要浏览器环境/);
});