Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex H authored and Alex H committed Dec 16, 2024
1 parent 3463c76 commit f63f41f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/lib/svgToPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function convertSVGtoPDF(
const templateURL = credential.renderMethod?.[0].id; // might want to sort if there are more than one renderMethod

let source = '';
const data = { credential };
const data = { credential: credential, qr_code: qrCodeBase64 };

// Fetch the template content
if (templateURL) {
Expand Down
77 changes: 77 additions & 0 deletions test/svgToPdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,82 @@ describe('convertSVGtoPDF', () => {

// If response is not OK, the result should be null
expect(result).toBeNull();
});

it('should fetch the template and generate a PDF', async () => {
const modifiedCredential = {
...mockCredential,
renderMethod: [
{
id: 'https://raw.githubusercontent.com/digitalcredentials/test-files/main/html-templates/rendermethod-qrcode-test.html',
type: 'HTML',
},
],
};

const templateHtml = '<html><body>{{ qr_code }}</body></html>';
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(templateHtml),
});

// Mock Handlebars.compile to return a function that generates the final HTML with embedded QR code
const mockCompiledTemplate = jest.fn().mockReturnValue((data: { qr_code: any }) => {
return `<html><body>data:image/png;base64, ${data.qr_code}</body></html>`;
});
(Handlebars.compile as jest.Mock).mockImplementation(mockCompiledTemplate);

const mockPdfResult = { filePath: 'path/to/pdf' };
(RNHTMLtoPDF.convert as jest.Mock).mockResolvedValueOnce(mockPdfResult);

const result = await convertSVGtoPDF(modifiedCredential, publicLink, qrCodeBase64);

// Ensure fetch was called with the correct URL
expect(global.fetch).toHaveBeenCalledWith(modifiedCredential.renderMethod[0].id);

// Ensure RNHTMLtoPDF.convert was called with the correct HTML and options
expect(RNHTMLtoPDF.convert).toHaveBeenCalledWith({
html: `<html><body>data:image/png;base64, ${qrCodeBase64}</body></html>`,
fileName: 'undefined Credential',
base64: false,
});

// Ensure the result is the expected PDF output
expect(result).toEqual(mockPdfResult);
});

it('should embed the qrCodeBase64 correctly in the template', async () => {
const modifiedCredential = {
...mockCredential,
renderMethod: [
{
id: 'https://raw.githubusercontent.com/digitalcredentials/test-files/main/html-templates/rendermethod-qrcode-test.html',
type: 'HTML',
},
],
};

const templateHtml = '<html><body>{{ qr_code }}</body></html>';
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(templateHtml),
});

const mockCompiledTemplate = jest.fn().mockReturnValue((data: { qr_code: any }) => {
return `<html><body>data:image/png;base64, ${data.qr_code}</body></html>`;
});
(Handlebars.compile as jest.Mock).mockImplementation(mockCompiledTemplate);

const mockPdfResult = { filePath: 'path/to/pdf' };
(RNHTMLtoPDF.convert as jest.Mock).mockResolvedValueOnce(mockPdfResult);

await convertSVGtoPDF(modifiedCredential, publicLink, qrCodeBase64);

// Check that the SVG with the correct QR code embedded was passed to RNHTMLtoPDF.convert
expect(RNHTMLtoPDF.convert).toHaveBeenCalledWith({
html: `<html><body>data:image/png;base64, ${qrCodeBase64}</body></html>`,
fileName: 'undefined Credential',
base64: false,
});
});
});

0 comments on commit f63f41f

Please sign in to comment.