refactor: update ctx test function

This commit is contained in:
Sheldon Guo 2025-03-18 08:15:56 +08:00
parent 89bb4c434a
commit 5cb7bc0d8f

View File

@ -24,8 +24,15 @@ describe('ctx function', () => {
const data = {
async $user({ fields, context }) {
if (context.state.userId) {
return (field) => 1;
} else return (field) => 2;
return {
getValue: () => 1,
afterApplyHelpers: ({ value }) => value,
};
}
return {
getValue: () => 2,
afterApplyHelpers: ({ value }) => value,
};
},
state: {
userId: 1,
@ -38,8 +45,11 @@ describe('ctx function', () => {
it('should handle context function without state', async () => {
const template = '{{$user.id}} - {{$user.name}}';
const data = {
$user({ fields, context }) {
return (field) => 2;
async $user({ fields, context }) {
return {
getValue: () => 2,
afterApplyHelpers: ({ value }) => value,
};
},
state: {},
};
@ -50,13 +60,16 @@ describe('ctx function', () => {
it('should handle nested context values', async () => {
const template = '{{$user.profile.email}} - {{$user.profile.address.city}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const map = {
'profile.email': 'test@example.com',
'profile.address.city': 'New York',
};
return map[field] || null;
},
afterApplyHelpers: ({ value }) => value,
};
},
};
@ -67,11 +80,17 @@ describe('ctx function', () => {
it('should handle multiple context functions', async () => {
const template = '{{$user.name}} works at {{$company.name}}';
const data = {
$user({ fields, context }) {
return (field) => 'John';
async $user({ fields, context }) {
return {
getValue: () => 'John',
afterApplyHelpers: ({ value }) => value,
};
},
$company({ fields, context }) {
return (field) => 'NocoBase';
async $company({ fields, context }) {
return {
getValue: () => 'NocoBase',
afterApplyHelpers: ({ value }) => value,
};
},
};
const result = await parser.render(template, data);
@ -81,8 +100,11 @@ describe('ctx function', () => {
it('should handle undefined context values', async () => {
const template = '{{$user.nonexistent}}';
const data = {
$user({ fields, context }) {
return (field) => undefined;
async $user({ fields, context }) {
return {
getValue: () => undefined,
afterApplyHelpers: ({ value }) => value,
};
},
};
const result = await parser.render(template, data);
@ -92,11 +114,13 @@ describe('ctx function', () => {
it('should handle context function with array values', async () => {
const template = '{{$user.roles[0]}} and {{$user.roles.1}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const data = { roles: ['admin', 'user'] };
const result = get(data, field);
return result;
return get(data, field);
},
afterApplyHelpers: ({ value }) => value,
};
},
};
@ -106,15 +130,20 @@ describe('ctx function', () => {
it('should escape array', async () => {
const template = ' {{$user.id}} - {{$user.name}} ';
const data = {
$user({ fields, context }) {
async $user({ fields, context }) {
if (context.state.userId) {
return (field) => 1;
} else return (field) => 2;
return {
getValue: () => 1,
afterApplyHelpers: ({ value }) => value,
};
}
return {
getValue: () => 2,
afterApplyHelpers: ({ value }) => value,
};
},
};
const context = {
state: {
userId: 1,
@ -123,14 +152,17 @@ describe('ctx function', () => {
const result = await parser.render(template, data, context);
expect(result).toEqual(' 1 - 1 ');
});
it('should handle context function with nested arrays', async () => {
const template = '{{$user.roles[0].name}} and {{$user.roles[1].name}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const data = { roles: [{ name: 'admin' }, { name: 'user' }] };
const result = get(data, field);
return result;
return get(data, field);
},
afterApplyHelpers: ({ value }) => value,
};
},
};
@ -141,13 +173,16 @@ describe('ctx function', () => {
it('should handle context function with deep nested objects', async () => {
const template = '{{$user.profile.address.city}} - {{$user.profile.address.zip}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const map = {
'profile.address.city': 'San Francisco',
'profile.address.zip': '94107',
};
return map[field] || null;
},
afterApplyHelpers: ({ value }) => value,
};
},
};
@ -158,20 +193,26 @@ describe('ctx function', () => {
it('should handle context function with multiple nested objects', async () => {
const template = '{{$user.profile.email}} - {{$company.info.address.city}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const map = {
'profile.email': 'test@example.com',
};
return map[field] || null;
},
afterApplyHelpers: ({ value }) => value,
};
},
$company({ fields, context }) {
return (field) => {
async $company({ fields, context }) {
return {
getValue: ({ field }) => {
const map = {
'info.address.city': 'Los Angeles',
};
return map[field] || null;
},
afterApplyHelpers: ({ value }) => value,
};
},
};
@ -182,8 +223,11 @@ describe('ctx function', () => {
it('should handle context function with boolean values', async () => {
const template = '{{$user.isActive}}';
const data = {
$user({ fields, context }) {
return (field) => true;
async $user({ fields, context }) {
return {
getValue: () => true,
afterApplyHelpers: ({ value }) => value,
};
},
};
const result = await parser.render(template, data);
@ -193,8 +237,11 @@ describe('ctx function', () => {
it('should handle context function with null values', async () => {
const template = '{{$user.profile.picture}}';
const data = {
$user({ fields, context }) {
return (field) => null;
async $user({ fields, context }) {
return {
getValue: () => null,
afterApplyHelpers: ({ value }) => value,
};
},
};
const result = await parser.render(template, data);
@ -204,8 +251,11 @@ describe('ctx function', () => {
it('should handle context function with numeric values', async () => {
const template = '{{$user.age}}';
const data = {
$user({ fields, context }) {
return (field) => 30;
async $user({ fields, context }) {
return {
getValue: () => 30,
afterApplyHelpers: ({ value }) => value,
};
},
};
const result = await parser.render(template, data);
@ -215,13 +265,16 @@ describe('ctx function', () => {
it('should handle context function with special characters in keys', async () => {
const template = '{{$user["first-name"]}} - {{$user["last-name"]}}';
const data = {
$user({ fields, context }) {
return (field) => {
async $user({ fields, context }) {
return {
getValue: ({ field }) => {
const map = {
'first-name': 'John',
'last-name': 'Doe',
};
return map[field] || null;
},
afterApplyHelpers: ({ value }) => value,
};
},
};