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