File size: 1,234 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * @jest-environment jsdom
 */

import { render, screen } from '@testing-library/react';
import DocService from 'calypso/devdocs/service';
import SingleDocClass from '../doc';

jest.mock( 'calypso/devdocs/service', () => ( {
	fetch: jest.fn(),
} ) );

const defaultProps = {
	term: 'hello',
	path: '/example',
	sectionId: '',
};

describe( 'SingleDoc', () => {
	describe( 'render test', () => {
		test( 'should render html with marked text', () => {
			const text = 'something hello';
			DocService.fetch.mockImplementationOnce( ( path, cb ) =>
				cb( null, `<div><p>${ text }</p></div>` )
			);

			render( <SingleDocClass { ...defaultProps } /> );

			const mark = screen.getByText( 'hello' );
			expect( mark.tagName ).toBe( 'MARK' );

			const doc = screen.getByText( 'something' );
			expect( doc ).toHaveTextContent( text );
		} );

		test( 'should render Error component', () => {
			DocService.fetch.mockImplementationOnce( ( path, cb ) =>
				cb( 'Error invoking /devdocs/content: File does not exist" is displayed' )
			);
			render( <SingleDocClass { ...defaultProps } /> );

			const error = screen.queryByText( /sorry, we can't find that page right now/i );
			expect( error ).toBeInTheDocument();
		} );
	} );
} );