File size: 3,869 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * @jest-environment jsdom
 */

import { render, screen, waitFor } from '@testing-library/react';
import { SiteThumbnail } from '..';
import { fetchAwaitRedirect } from '../fetch-await-redirect';

const MSHOTS_URL = 'https://fakeUrl';
const IMG_ALT = 'test image';

jest.mock( '../fetch-await-redirect' );

describe( 'SiteThumbnail', () => {
	beforeEach( () => {
		jest.useFakeTimers();
		jest.useFakeTimers();
		// Mocking the fetch function to return response.redirect = false by default
		// and to return response.redirect = true when the url is the same as the mshots url
		// This is to simulate the mshots image being loaded
		fetchAwaitRedirect.mockImplementation( async () => {
			return {
				isError: false,
				isRedirect: false,
			};
		} );
	} );
	test( 'has an image that points to mshot', async () => {
		render( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } /> );
		await waitFor( () => {
			expect( screen.queryByAltText( IMG_ALT ) ).not.toBeNull();
			expect( screen.getByAltText( IMG_ALT ).getAttribute( 'src' ) ).toMatch(
				'https://s0.wp.com/mshots/v1/' + encodeURIComponent( MSHOTS_URL )
			);
		} );
	} );

	test( 'shows fallback when mshot returns 429', async () => {
		fetchAwaitRedirect.mockImplementation( async () => {
			return {
				isError: true,
				isRedirect: false,
			};
		} );
		render( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } /> );
		await waitFor( () => expect( screen.queryByAltText( IMG_ALT ) ).toBeNull() );
	} );

	test( 'should show the dimension width for the default sizes', async () => {
		const dimension = {
			width: 100,
			height: 100,
		};
		render( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } { ...dimension } /> );
		await waitFor( () =>
			expect( screen.getByAltText( IMG_ALT ).getAttribute( 'sizes' ) ).toEqual(
				`${ dimension.width }px`
			)
		);
	} );

	test( 'should use the sizesAttr prop as sizes attr', async () => {
		const sizesAttr = '(max-width: 400px) 100vw, 400px';
		render( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } sizesAttr={ sizesAttr } /> );
		await waitFor( () =>
			expect( screen.getByAltText( IMG_ALT ).getAttribute( 'sizes' ) ).toEqual( sizesAttr )
		);
	} );

	// eslint-disable-next-line jest/no-disabled-tests
	test.skip( 'should generate responsive size alternatives 2x and 3x srcset', async () => {
		const dimension = { width: 200, height: 100 };
		render( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } { ...dimension } /> );
		await waitFor( () => {
			const srcset = screen.getByAltText( IMG_ALT ).getAttribute( 'srcset' );
			expect( srcset ).toMatch( ` ${ dimension.width * 2 }w` );
			expect( srcset ).toMatch( ` ${ dimension.width * 3 }w` );
		} );
	} );

	// eslint-disable-next-line jest/no-disabled-tests
	test.skip( 'should add dimensionsSrcSet array prop to srcset string attribute', async () => {
		const alternativeDimensions = [
			{
				width: 777,
				height: 777,
			},
			{
				width: 888,
				height: 888,
			},
		];
		render(
			<SiteThumbnail
				mShotsUrl={ MSHOTS_URL }
				alt={ IMG_ALT }
				dimensionsSrcset={ alternativeDimensions }
			/>
		);
		await waitFor( () => {
			const srcset = screen.getByAltText( IMG_ALT ).getAttribute( 'srcset' );
			alternativeDimensions.forEach( ( dimension ) => {
				expect( srcset ).toMatch( ` ${ dimension.width }w` );
			} );
		} );
	} );

	test( 'empty URL will show no image cause an error', async () => {
		const { rerender } = render( <SiteThumbnail mShotsUrl="" alt={ IMG_ALT } /> );
		expect( screen.queryByAltText( IMG_ALT ) ).toBeNull();

		rerender( <SiteThumbnail mShotsUrl={ MSHOTS_URL } alt={ IMG_ALT } /> );
		await waitFor( () => {
			expect( screen.queryByAltText( IMG_ALT ) ).not.toBeNull();
		} );

		rerender( <SiteThumbnail mShotsUrl="" alt={ IMG_ALT } /> );
		expect( screen.queryByAltText( IMG_ALT ) ).toBeNull();
	} );
} );