Skip to content

Commit

Permalink
fix: removed duplicate test case and implement mock fetch to simulate…
Browse files Browse the repository at this point in the history
… the case that fetch template from url
  • Loading branch information
ducpm511 committed Jun 12, 2024
1 parent d4e31b1 commit 75c4d42
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
52 changes: 23 additions & 29 deletions packages/renderer/__tests__/providers/render-template-2024.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { RenderTemplate2024 } from '../../src/providers/render-template-2024';
import universityDegreeCredentialTemplate2024 from '../../fixtures/univerisity-degree-credential-template-2024.json';
import { jest } from '@jest/globals';
import { IRendererContext } from '@vckit/core-types';
import { url } from 'inspector';

describe('RenderTemplate2024', () => {
let renderer: RenderTemplate2024;
Expand Down Expand Up @@ -337,33 +335,6 @@ describe('RenderTemplate2024', () => {
expect(result).toEqual({"renderedTemplate": 'Error: No hash function provided to verify the template'});
});

it('should return empty string if it failed to fetch template from url', async () => {
const document = { name: 'John Doe', url: 'example.com'};
const data = {
'https://schema.org/encodingFormat': [
{
'@value': 'text/html',
},
],
'https://www.w3.org/2018/credentials#renderMethod#template': [
{
'@value':
'',
},
],
'https://www.w3.org/2018/credentials#renderMethod#url': [
{
'@value': 'example.com', // Replace with the actual URL
},
],
};
const result = await renderer.renderCredential({
data,
document,
});
expect(result.renderedTemplate).toBe('');
});

it('should return template with style added', async () => {
const document = { name: 'John Doe', url: 'example.com'};
const data = {
Expand Down Expand Up @@ -397,4 +368,27 @@ describe('RenderTemplate2024', () => {
expect(JSON.stringify(result.renderedTemplate)).toBe("\"<style>@media (min-width: 1024px) {\\n .title {\\n font-weight: bold;\\n color: #223675;\\n }\\n}</style>\"");
});

it('should fetch template from URL and render correctly', async () => {
global.fetch = jest.fn() as typeof fetch;
const mockTemplate = '<div>{{name}}</div>';
(global.fetch as jest.Mock).mockReturnValue({
ok: true,
text: async () => mockTemplate,
});

const data = {
'https://www.w3.org/2018/credentials#renderMethod#url': [{ '@value': 'https://example.com/template' }],
'https://schema.org/encodingFormat': [{ '@value': 'text/html' }],
'https://schema.org/name': [{ '@value': 'Test Name' }],
};

const result = await renderer.renderCredential({
data,
document: { name: 'Test Name' },
});

expect(result.renderedTemplate).toBe('<div>Test Name</div>');
expect(global.fetch).toHaveBeenCalledWith('https://example.com/template');
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://www.w3.org/ns/credentials/examples/v2",
"https://vckit-contexts.s3.ap-southeast-2.amazonaws.com/render-template-context-2024.json"
{
"ex": "https://www.w3.org/2018/credentials#renderMethod#",
"renderMethod": "https://www.w3.org/2018/credentials#renderMethod",
"template": "ex:template",
"url": "ex:url"
}
],
"id": "http://example.edu/credentials/3732",
"type": ["VerifiableCredential", "UniversityDegreeCredential"],
Expand Down

0 comments on commit 75c4d42

Please sign in to comment.