From c477b8d55c2db60e61f5e7ebb0b69b45e81fe323 Mon Sep 17 00:00:00 2001 From: ssandupatla <58558770+ssandupatla@users.noreply.github.com> Date: Fri, 7 Jul 2023 16:02:48 +0530 Subject: [PATCH] Update ComponentToPrint.test.js --- .../ComponentToPrint/ComponentToPrint.test.js | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/components/ComponentToPrint/ComponentToPrint.test.js b/src/components/ComponentToPrint/ComponentToPrint.test.js index 0854a249..2537a71c 100644 --- a/src/components/ComponentToPrint/ComponentToPrint.test.js +++ b/src/components/ComponentToPrint/ComponentToPrint.test.js @@ -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(() =>
); }); describe('ComponentToPrint', () => { const templateFnMock = jest.fn(); - const dataSourceMock = {}; - + const templateFn = jest.fn(() => '123456'); + const dataSource = { 'request':1 }; + const parser = new Parser(); beforeEach(() => { templateFnMock.mockClear(); }); - - it('renders without crashing', () => { - render(); + it('should handle null result from parseWithInstructions', () => { + const originalParseWithInstructions = parser.parseWithInstructions; + parser.parseWithInstructions = jest.fn(() => null); + render(); + 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(() => '123456'); - const dataSource = {}; - - const { getByTestId } = render(); - - const barcodeComponent = getByTestId('barcode-component'); - - expect(barcodeComponent).toBeInTheDocument(); + it('should handle empty template string', () => { + const emptyTemplateFn = jest.fn(() => null); + const { container } = render(); + expect(container.firstChild).toBeNull(); + }); + it('should process the barcode rule with empty string', () => { + const mockBarcodeValue = '123456789'; + const emptyString = ''; + const mockComponentStr = `
${mockBarcodeValue}${emptyString}
`; + templateFn.mockReturnValue(mockComponentStr); + render(); + const barcodeElement = screen.getAllByTestId('barcode-component')[0]; + expect(barcodeElement).toBeInTheDocument(); + }); + it('should process the barcode rule with data', () => { + const originalParseWithInstructions = parser.parseWithInstructions; + const mockParsedComponent =
Mock Component
; + parser.parseWithInstructions = jest.fn(() => mockParsedComponent); + render(); + const emptyBarcodeElement = screen.getAllByTestId('barcode-component')[1]; + expect(emptyBarcodeElement).toBeInTheDocument(); + parser.parseWithInstructions = originalParseWithInstructions; }); });