-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d48a606
commit c477b8d
Showing
1 changed file
with
33 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |