Skip to content

Commit

Permalink
Update ComponentToPrint.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ssandupatla authored Jul 7, 2023
1 parent d48a606 commit c477b8d
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/components/ComponentToPrint/ComponentToPrint.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { Parser } from 'html-to-react';
import ComponentToPrint from './ComponentToPrint';

// Mock the react-barcode library
jest.mock('react-barcode', () => {
return jest.fn().mockImplementation(() => <div data-testid="barcode-component" />);
});

describe('ComponentToPrint', () => {
const templateFnMock = jest.fn();
const dataSourceMock = {};

const templateFn = jest.fn(() => '<barcode>123456</barcode>');
const dataSource = { 'request':1 };
const parser = new Parser();
beforeEach(() => {
templateFnMock.mockClear();
});

it('renders without crashing', () => {
render(<ComponentToPrint templateFn={templateFnMock} dataSource={dataSourceMock} />);
it('should handle null result from parseWithInstructions', () => {
const originalParseWithInstructions = parser.parseWithInstructions;
parser.parseWithInstructions = jest.fn(() => null);
render(<ComponentToPrint dataSource={dataSource} templateFn={templateFn} />);
expect(screen.queryByTestId('barcode')).not.toBeInTheDocument();
parser.parseWithInstructions = originalParseWithInstructions;
});

it('renders a Barcode component when encountering a "barcode" tag in the template', () => {
const templateFn = jest.fn(() => '<barcode>123456</barcode>');
const dataSource = {};

const { getByTestId } = render(<ComponentToPrint templateFn={templateFn} dataSource={dataSource} />);

const barcodeComponent = getByTestId('barcode-component');

expect(barcodeComponent).toBeInTheDocument();
it('should handle empty template string', () => {
const emptyTemplateFn = jest.fn(() => null);
const { container } = render(<ComponentToPrint dataSource={dataSource} templateFn={emptyTemplateFn} />);
expect(container.firstChild).toBeNull();
});
it('should process the barcode rule with empty string', () => {
const mockBarcodeValue = '123456789';
const emptyString = '';
const mockComponentStr = `<div><barcode>${mockBarcodeValue}</barcode><barcode>${emptyString}</barcode></div>`;
templateFn.mockReturnValue(mockComponentStr);
render(<ComponentToPrint dataSource={dataSource} templateFn={templateFn} />);
const barcodeElement = screen.getAllByTestId('barcode-component')[0];
expect(barcodeElement).toBeInTheDocument();
});
it('should process the barcode rule with data', () => {
const originalParseWithInstructions = parser.parseWithInstructions;
const mockParsedComponent = <div data-testid="mock-component">Mock Component</div>;
parser.parseWithInstructions = jest.fn(() => mockParsedComponent);
render(<ComponentToPrint dataSource={dataSource} templateFn={templateFn} />);
const emptyBarcodeElement = screen.getAllByTestId('barcode-component')[1];
expect(emptyBarcodeElement).toBeInTheDocument();
parser.parseWithInstructions = originalParseWithInstructions;
});
});

0 comments on commit c477b8d

Please sign in to comment.