Files
jackwener__opencli/clis/jd/commands.test.js
jakevin 65979f26c2 refactor(test): extract shared page mock, remove dead test (#1321)
- Add clis/test-utils.js with standard createPageMock utility
- Migrate 11 test files to use shared utility (removes ~300 lines of duplication)
- Delete extension/src/cdp.test.ts dead skip test (chrome.scripting.executeScript removed from source)
- Remove clis/pixiv/test-utils.js (superseded by shared utility)
2026-05-05 01:39:22 +08:00

42 lines
1.6 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import './search.js';
import './detail.js';
import './reviews.js';
import './cart.js';
import './add-cart.js';
import { createPageMock } from '../test-utils.js';
describe('jd command registration', () => {
it('registers all jd shopping commands', () => {
for (const name of ['search', 'detail', 'reviews', 'cart', 'add-cart']) {
expect(getRegistry().get(`jd/${name}`)).toBeDefined();
}
});
});
describe('jd command safety', () => {
it('rejects invalid numeric sku before evaluating page scripts', async () => {
const page = createPageMock();
const detail = getRegistry().get('jd/detail');
await expect(detail.func(page, { sku: 'abc' })).rejects.toMatchObject({
name: 'ArgumentError',
code: 'ARGUMENT',
});
expect(page.goto).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
});
it('supports dry-run for add-cart without mutating the cart', async () => {
const page = createPageMock();
const addCart = getRegistry().get('jd/add-cart');
const result = await addCart.func(page, { sku: '100291143898', 'dry-run': true });
expect(result).toEqual([
expect.objectContaining({
status: 'dry-run',
sku: '100291143898',
}),
]);
expect(page.goto).toHaveBeenCalledTimes(1);
expect(page.goto).toHaveBeenCalledWith('https://item.jd.com/100291143898.html');
expect(page.evaluate).toHaveBeenCalledTimes(1);
});
});