summaryrefslogtreecommitdiff
path: root/packages/integrations/sitemap/test/trailing-slash.test.js
blob: 181f0def53d20f10f0468c45238c457c804892aa (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
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
122
123
124
125
126
127
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture, readXML } from './test-utils.js';

describe('Trailing slash', () => {
	/** @type {import('./test-utils').Fixture} */
	let fixture;

	describe('trailingSlash: ignore', () => {
		describe('build.format: directory', () => {
			before(async () => {
				fixture = await loadFixture({
					root: './fixtures/trailing-slash/',
					trailingSlash: 'ignore',
					build: {
						format: 'directory',
					},
				});
				await fixture.build();
			});

			it('URLs end with trailing slash', async () => {
				const data = await readXML(fixture.readFile('/sitemap-0.xml'));
				const urls = data.urlset.url;

				assert.equal(urls[0].loc[0], 'http://example.com/');
				assert.equal(urls[1].loc[0], 'http://example.com/one/');
				assert.equal(urls[2].loc[0], 'http://example.com/two/');
			});
		});

		describe('build.format: file', () => {
			before(async () => {
				fixture = await loadFixture({
					root: './fixtures/trailing-slash/',
					trailingSlash: 'ignore',
					build: {
						format: 'file',
					},
				});
				await fixture.build();
			});

			it('URLs do not end with trailing slash', async () => {
				const data = await readXML(fixture.readFile('/sitemap-0.xml'));
				const urls = data.urlset.url;

				assert.equal(urls[0].loc[0], 'http://example.com');
				assert.equal(urls[1].loc[0], 'http://example.com/one');
				assert.equal(urls[2].loc[0], 'http://example.com/two');
			});
		});
	});

	describe('trailingSlash: never', () => {
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/trailing-slash/',
				trailingSlash: 'never',
			});
			await fixture.build();
		});

		it('URLs do not end with trailing slash', async () => {
			const data = await readXML(fixture.readFile('/sitemap-0.xml'));
			const urls = data.urlset.url;

			assert.equal(urls[0].loc[0], 'http://example.com');
			assert.equal(urls[1].loc[0], 'http://example.com/one');
			assert.equal(urls[2].loc[0], 'http://example.com/two');
		});
		describe('with base path', () => {
			before(async () => {
				fixture = await loadFixture({
					root: './fixtures/trailing-slash/',
					trailingSlash: 'never',
					base: '/base',
				});
				await fixture.build();
			});

			it('URLs do not end with trailing slash', async () => {
				const data = await readXML(fixture.readFile('/sitemap-0.xml'));
				const urls = data.urlset.url;
				assert.equal(urls[0].loc[0], 'http://example.com/base');
				assert.equal(urls[1].loc[0], 'http://example.com/base/one');
				assert.equal(urls[2].loc[0], 'http://example.com/base/two');
			});
		});
	});

	describe('trailingSlash: always', () => {
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/trailing-slash/',
				trailingSlash: 'always',
			});
			await fixture.build();
		});

		it('URLs end with trailing slash', async () => {
			const data = await readXML(fixture.readFile('/sitemap-0.xml'));
			const urls = data.urlset.url;
			assert.equal(urls[0].loc[0], 'http://example.com/');
			assert.equal(urls[1].loc[0], 'http://example.com/one/');
			assert.equal(urls[2].loc[0], 'http://example.com/two/');
		});
		describe('with base path', () => {
			before(async () => {
				fixture = await loadFixture({
					root: './fixtures/trailing-slash/',
					trailingSlash: 'always',
					base: '/base',
				});
				await fixture.build();
			});

			it('URLs end with trailing slash', async () => {
				const data = await readXML(fixture.readFile('/sitemap-0.xml'));
				const urls = data.urlset.url;
				assert.equal(urls[0].loc[0], 'http://example.com/base/');
				assert.equal(urls[1].loc[0], 'http://example.com/base/one/');
				assert.equal(urls[2].loc[0], 'http://example.com/base/two/');
			});
		});
	});
});