summaryrefslogtreecommitdiff
path: root/packages/integrations/image/test/rotation.test.js
blob: 19333e572f324b3dd49355e7b555be8011d61ed6 (plain) (blame)
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
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import sizeOf from 'image-size';
import { fileURLToPath } from 'url';
import { loadFixture } from './test-utils.js';

let fixture;

describe('Image rotation', function () {
	before(async () => {
		fixture = await loadFixture({ root: './fixtures/rotation/' });
	});

	function verifyImage(pathname, expected) {
		const url = new URL('./fixtures/rotation/dist/' + pathname, import.meta.url);
		const dist = fileURLToPath(url);
		const result = sizeOf(dist);
		expect(result).to.deep.equal(expected);
	}

	describe('build', () => {
		let $;
		let html;

		before(async () => {
			await fixture.build();

			html = await fixture.readFile('/index.html');
			$ = cheerio.load(html);
		});

		describe('Landscape images', () => {
			it('includes <img> attributes', () => {
				for (let i = 0; i < 9; i++) {
					const image = $(`#landscape-${i}`);

					expect(image.attr('src')).to.equal(`/_image/assets/Landscape_${i}_1800x1200.jpg`);
					expect(image.attr('width')).to.equal('1800');
					expect(image.attr('height')).to.equal('1200');
				}
			});

			it('built the optimized image', () => {
				for (let i = 0; i < 9; i++) {
					verifyImage(`/_image/assets/Landscape_${i}_1800x1200.jpg`, {
						width: 1800,
						height: 1200,
						type: 'jpg',
					});
				}
			});
		});

		describe('Portait images', () => {
			it('includes <img> attributes', () => {
				for (let i = 0; i < 9; i++) {
					const image = $(`#portrait-${i}`);

					expect(image.attr('src')).to.equal(`/_image/assets/Portrait_${i}_1200x1800.jpg`);
					expect(image.attr('width')).to.equal('1200');
					expect(image.attr('height')).to.equal('1800');
				}
			});

			it('built the optimized image', () => {
				for (let i = 0; i < 9; i++) {
					verifyImage(`/_image/assets/Portrait_${i}_1200x1800.jpg`, {
						width: 1200,
						height: 1800,
						type: 'jpg',
					});
				}
			});
		});
	});
});