111 lines
3.9 KiB
JavaScript
111 lines
3.9 KiB
JavaScript
const assert = require('node:assert/strict');
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const test = require('node:test');
|
||
|
||
const ProfilePages = require('../public/js/profile-pages.js');
|
||
const RegionPages = require('../public/js/region-pages.js');
|
||
const UploadPages = require('../public/js/upload-pages.js');
|
||
|
||
test('profile view reads only the documented profile fields', () => {
|
||
assert.deepEqual(ProfilePages.buildProfileView({
|
||
nickName: '小林',
|
||
phone: '13800000000',
|
||
birthday: '1990-01-01',
|
||
provinceCode: '11',
|
||
userName: '不应读取',
|
||
mobile: '不应读取'
|
||
}), {
|
||
displayName: '小林',
|
||
avatarText: '小',
|
||
phone: '13800000000',
|
||
sex: '待填写',
|
||
birthday: '1990-01-01',
|
||
regionText: '加载中...'
|
||
});
|
||
|
||
assert.deepEqual(ProfilePages.buildProfileView({
|
||
userName: '不应读取',
|
||
phonenumber: '不应读取',
|
||
mobile: '不应读取'
|
||
}), {
|
||
displayName: '未设置昵称',
|
||
avatarText: '家',
|
||
phone: '未绑定',
|
||
sex: '待填写',
|
||
birthday: '待填写',
|
||
regionText: '待填写'
|
||
});
|
||
});
|
||
|
||
test('profile loading failure has an explicit non-demo view', () => {
|
||
assert.deepEqual(ProfilePages.buildUnavailableProfileView(), {
|
||
displayName: '账户资料暂时无法读取',
|
||
avatarText: '!',
|
||
phone: '暂无数据',
|
||
birthday: '暂无数据',
|
||
regionText: '暂无数据'
|
||
});
|
||
});
|
||
|
||
test('profile update emits only the six fields defined by ProfileUpdateBody', () => {
|
||
assert.deepEqual(ProfilePages.buildProfileUpdateBody({
|
||
nickName: '小林',
|
||
realName: '林某',
|
||
avatar: '',
|
||
sex: '0',
|
||
birthday: '1990-01-01',
|
||
email: 'lin@example.com',
|
||
avatarOssId: 'legacy-avatar',
|
||
provinceCode: '11'
|
||
}), {
|
||
nickName: '小林',
|
||
realName: '林某',
|
||
sex: '0',
|
||
birthday: '1990-01-01',
|
||
email: 'lin@example.com'
|
||
});
|
||
assert.deepEqual(ProfilePages.buildProfileUpdateBody({ avatar: '123' }), { avatar: '123' });
|
||
assert.deepEqual(
|
||
ProfilePages.buildProfileUpdateBody({ avatar: '2060000000000000001' }),
|
||
{ avatar: '2060000000000000001' }
|
||
);
|
||
assert.equal(UploadPages.normalizeUploadResult({ ossId: '2060000000000000000' }).ossId, '2060000000000000000');
|
||
});
|
||
|
||
test('region selector accepts only documented region fields and levels', () => {
|
||
assert.deepEqual(RegionPages.normalizeList({ data: [{ regionCode: '11' }] }), []);
|
||
assert.equal(RegionPages.getRegionCode({ value: '11' }), '');
|
||
assert.equal(RegionPages.getRegionName({ label: '北京市' }), '未命名地区');
|
||
assert.equal(RegionPages.getRegionLevel({ regionCode: '110101' }), 0);
|
||
|
||
assert.deepEqual(RegionPages.buildRegionSelection([
|
||
{ regionCode: '11', regionName: '北京市', regionLevel: 1 },
|
||
{ regionCode: '1101', regionName: '市辖区', regionLevel: 2 },
|
||
{ regionCode: '110101', regionName: '东城区', regionLevel: 3 }
|
||
]), {
|
||
provinceCode: '11',
|
||
cityCode: '1101',
|
||
districtCode: '110101'
|
||
});
|
||
});
|
||
|
||
test('profile data page exposes every editable ProfileUpdateBody field without a manual OSS ID input', () => {
|
||
const source = fs.readFileSync(path.join(__dirname, '..', 'profile-data.html'), 'utf8');
|
||
|
||
assert.match(source, /data-profile-form/);
|
||
assert.match(source, /name="nickName"/);
|
||
assert.match(source, /name="realName"/);
|
||
assert.match(source, /name="avatar"/);
|
||
assert.match(source, /name="sex"[\s\S]*<option value="2">未知<\/option>/);
|
||
assert.match(source, /name="birthday"/);
|
||
assert.match(source, /name="email"/);
|
||
assert.doesNotMatch(source, /name="avatarOssId"/);
|
||
assert.match(source, /name="avatar"[\s\S]*type="hidden"|type="hidden"[\s\S]*name="avatar"/);
|
||
assert.doesNotMatch(source, /data-region-profile-form/);
|
||
assert.doesNotMatch(source, /data-region-profile-status/);
|
||
assert.match(source, /data-region-search-form/);
|
||
assert.match(source, /src="public\/js\/upload-pages\.js"/);
|
||
assert.match(source, /src="public\/js\/region-pages\.js"/);
|
||
});
|