File size: 1,612 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
48
49
50
51
52
53
54
55
56
import page from '@automattic/calypso-router';
import { AppState } from 'calypso/types';
import { getSafeImageUrlForReader, showSelectedPost } from '../utils';

jest.mock( '@automattic/calypso-router', () => jest.fn() );

describe( 'reader utils', () => {
	const dispatch = jest.fn();
	const getState = () =>
		( {
			reader: {
				posts: {
					items: {},
				},
			},
		} ) as AppState;

	beforeEach( () => {
		jest.resetAllMocks();
	} );

	describe( '#showSelectedPost', () => {
		test( 'does not do anything if postKey argument is missing', () => {
			showSelectedPost( {} )( dispatch, getState );
			expect( page ).not.toHaveBeenCalled();
		} );

		test( 'redirects if passed a post key', () => {
			showSelectedPost( { postKey: { feedId: 1, postId: 5 } } )( dispatch, getState );
			expect( page ).toHaveBeenCalledTimes( 1 );
		} );

		test( 'redirects to a #comments URL if we passed comments argument', () => {
			showSelectedPost( { postKey: { feedId: 1, postId: 5 }, comments: true } )(
				dispatch,
				getState
			);
			expect( page as ( url: string ) => void ).toHaveBeenCalledWith(
				'/reader/feeds/1/posts/5#comments'
			); //
		} );
	} );

	describe( 'getSafeImageUrlForReader', () => {
		test( 'returns the url as is if it is from a trusted host', () => {
			const url = 'https://www.redditstatic.com/image.jpg';
			expect( getSafeImageUrlForReader( url ) ).toEqual( url );
		} );

		test( 'returns the Photon url if it is not from a trusted host', () => {
			const url = 'https://www.example.com/image.jpg';
			expect( getSafeImageUrlForReader( url ) ).not.toEqual( url );
		} );
	} );
} );