fix(frontend): align search and header components with reference
This commit is contained in:
1 parent
4c13ef478c
commit
542daca457
34 files changed
+803
-705
No files matched your search
@@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
const vm = require('node:vm');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
|
||||
const runtime = read('assets/js/runtime/main.js');
|
||||
|
||||
function handlerBetween(start, end, context) {
|
||||
let handler;
|
||||
vm.runInNewContext(runtime.slice(runtime.indexOf(start), runtime.indexOf(end)), {
|
||||
_win: { bd: { on(event, selector, callback) { handler = callback; } } },
|
||||
...context,
|
||||
});
|
||||
assert.equal(typeof handler, 'function');
|
||||
return handler;
|
||||
}
|
||||
|
||||
test('search type changes reset only the active form placeholder', () => {
|
||||
const field = { length: 1, attr: () => '开启精彩搜索', text(value) { this.value = value; } };
|
||||
const control = {};
|
||||
const handler = handlerBetween('//搜索功能-恢复默认占位符', '//搜索功能->删除多余的input', {
|
||||
jQuery(target) {
|
||||
assert.equal(target, control, 'must not select all search boxes');
|
||||
return { closest(selector) {
|
||||
assert.equal(selector, 'form');
|
||||
return { find(selector) { assert.equal(selector, '.scale-placeholder'); return field; } };
|
||||
} };
|
||||
},
|
||||
});
|
||||
handler.call(control);
|
||||
assert.equal(field.value, '开启精彩搜索');
|
||||
});
|
||||
|
||||
test('clearing history expires the saved cookie at its original WordPress path and domain', () => {
|
||||
const control = {};
|
||||
const history = {};
|
||||
const cookies = [];
|
||||
let removed = false;
|
||||
let confirmed = true;
|
||||
const jQuery = (target) => {
|
||||
if (target === control) return { attr(name) {
|
||||
return { 'data-cookie-path': '/blog/', 'data-cookie-domain': '.example.test' }[name];
|
||||
} };
|
||||
if (target === '.history-search') return { slideUp(duration, complete) {
|
||||
assert.equal(duration, 300);
|
||||
complete.call(history);
|
||||
} };
|
||||
assert.equal(target, history);
|
||||
return { remove() { removed = true; } };
|
||||
};
|
||||
jQuery.cookie = (name, value, options) => cookies.push({ name, value, ...options });
|
||||
const handler = handlerBetween('//清空搜索关键词', '//用户签到', {
|
||||
jQuery, confirm: () => confirmed, aksrt__: (key) => key,
|
||||
});
|
||||
handler.call(control);
|
||||
assert.deepEqual(cookies, [{ name: 'history_search', value: null, path: '/blog/', domain: '.example.test' }]);
|
||||
assert.equal(removed, true);
|
||||
confirmed = false;
|
||||
handler.call(control);
|
||||
assert.equal(cookies.length, 1, 'cancelling must preserve history');
|
||||
});
|
||||
|
||||
test('removed search markup has no orphaned JavaScript owner', () => {
|
||||
assert.doesNotMatch(read('assets/js/core/article.js'), /initSearchClear|search-clear-btn|search-page-input|search-category-filter|aksrt_search_type|aksrt_category/);
|
||||
assert.doesNotMatch(runtime, /eval\('\/' \+ val|search-tab \.item-heading/);
|
||||
assert.doesNotMatch(runtime, /css\/dplayer\.min\.css/);
|
||||
});
|
||||
|
||||
test('search submissions replace results with encoded filters and navigate normally when cleared', () => {
|
||||
const handlers = {};
|
||||
const form = {};
|
||||
let data = { s: '主题 & "搜索"', type: 'post', trem: '7' };
|
||||
let linkMarkup = '';
|
||||
let clicks = 0;
|
||||
const link = { click() { clicks++; return this; }, remove() {} };
|
||||
const pager = { length: 1, append(node) { assert.equal(node, link); } };
|
||||
const jQuery = (target) => {
|
||||
if (target === '.search-page-ajaxpager') return pager;
|
||||
if (target === form) return { serializeObject: () => data, attr: () => 'https://example.test/blog/' };
|
||||
linkMarkup = target;
|
||||
return link;
|
||||
};
|
||||
vm.runInNewContext(runtime.slice(runtime.indexOf('//搜索页面关键词ajax'), runtime.indexOf('function nav_folding()')), {
|
||||
_win: { bd: { on(event, selector, handler) { handlers[event] = handler; } } },
|
||||
jQuery, URL, URLSearchParams,
|
||||
});
|
||||
assert.equal(handlers.submit.call(form), false);
|
||||
const url = new URL(linkMarkup.match(/href="([^"]+)"/)[1]);
|
||||
assert.equal(url.searchParams.get('s'), data.s);
|
||||
assert.equal(url.searchParams.get('type'), 'post');
|
||||
assert.equal(url.searchParams.get('trem'), '7');
|
||||
assert.equal(clicks, 1);
|
||||
data = { s: ' ', type: 'post' };
|
||||
assert.equal(handlers.submit.call(form), undefined);
|
||||
assert.equal(clicks, 1, 'the empty page has no AJAX pager and must use normal form navigation');
|
||||
});
|
||||
Reference in new issue
Block a user