Skip to content

Commit

Permalink
Update mock to include error case
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunalpal216 committed Nov 9, 2024
1 parent 267f2d0 commit c0700be
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
38 changes: 36 additions & 2 deletions scripts/__mocks__/@pdfme/generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import { generate } from './generator';
import type { Template } from '@pdfme/common';

describe('Testing mock generate util', () => {
test('should return a Promise', async () => {
const result = generate({ template: {}, inputs: [] });
const result = generate({
template: { schemas: [] } as Template,
inputs: [],
});
expect(result).toBeInstanceOf(Promise);
});

test('should resolve to a Uint8Array', async () => {
const result = generate({ template: {}, inputs: [] });
const result = generate({
template: { schemas: [] } as Template,
inputs: [],
});
expect(result).toBeInstanceOf(Uint8Array);
});

it('should throw an error when template is empty', async () => {
const emptyTemplate = { schemas: [] } as Template;
const validInputs = [{ field1: 'value1' }];

await expect(
generate({ template: emptyTemplate, inputs: validInputs }),
).rejects.toThrow('Template or inputs cannot be empty.');
});

it('should throw an error when inputs are empty', async () => {
const validTemplate = { schemas: [{ name: 'field1' }] } as Template;
const emptyInputs: Record<string, string>[] = [];

await expect(
generate({ template: validTemplate, inputs: emptyInputs }),
).rejects.toThrow('Template or inputs cannot be empty.');
});

it('should throw an error when both template and inputs are empty', async () => {
const emptyTemplate = { schemas: [] } as Template;
const emptyInputs: Record<string, string>[] = [];

await expect(
generate({ template: emptyTemplate, inputs: emptyInputs }),
).rejects.toThrow('Template or inputs cannot be empty.');
});
});
3 changes: 3 additions & 0 deletions scripts/__mocks__/@pdfme/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const generate = async ({
template: Template;
inputs: Record<string, string>[];
}): Promise<Uint8Array> => {
if (template.schemas.length === 0 || inputs.length === 0) {
throw new Error('Template or inputs cannot be empty.');
}
// Generate mock PDF-like header bytes
const pdfHeader = [0x25, 0x50, 0x44, 0x46]; // %PDF
// Add some random content based on input size
Expand Down

0 comments on commit c0700be

Please sign in to comment.