Skip to content

Commit 1876eb8

Browse files
committed
Add unit test for DomSnapshot to cover base tag use case
1 parent b7e95ee commit 1876eb8

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/shared/src/utils/filePaths.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,19 @@ describe('truncateFileName', () => {
219219
expect(truncated).toMatch(new RegExp('^this-title-.*-a-file-system-[a-z0-9]{4}$'));
220220
});
221221
describe('removeLocalhostFromBaseRef', () => {
222-
it('should remove localhost from href', () => {
222+
it('should remove localhost from href but keeps the relative path', () => {
223223
const href = 'http://localhost:3000/some/path/';
224224
const result = removeLocalhostFromBaseRef(href);
225225
expect(result).toBe('/some/path/');
226226
});
227227

228-
it('should not remove localhost from href if it is not the first part', () => {
228+
it('should remove localhost from href', () => {
229229
const href = 'http://localhost:3000/';
230230
const result = removeLocalhostFromBaseRef(href);
231231
expect(result).toBe('/');
232232
});
233233

234-
it('should not remove localhost from href if it is not the first part', () => {
234+
it('should return the exact href if it is already relative', () => {
235235
const href = '/some/path/';
236236
const result = removeLocalhostFromBaseRef(href);
237237
expect(result).toBe('/some/path/');

packages/shared/src/write-archive/dom-snapshot.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ function createSnapshot(url1: string, url2: string, url3: string) {
1010
return `{"type":0,"childNodes":[{"type":1,"name":"html","publicId":"","systemId":"","id":2},{"type":2,"tagName":"html","attributes":{},"childNodes":[{"type":2,"tagName":"head","attributes":{},"childNodes":[{"type":3,"textContent":" ","id":5},{"type":2,"tagName":"link","attributes":{"rel":"stylesheet","href":"/styles-test.css"},"childNodes":[],"id":6},{"type":3,"textContent":" ","id":7},{"type":2,"tagName":"style","attributes":{},"childNodes":[{"type":3,"textContent":".test1 { background-image: url(\\"${url1}\\"); }.test2 { background-image: url(\\"${url2}\\"); }.test2 { background-image: url(\\"${url3}\\"); }","isStyle":true,"id":9}],"id":8},{"type":3,"textContent":" ","id":10}],"id":4},{"type":3,"textContent":" ","id":11},{"type":2,"tagName":"body","attributes":{},"childNodes":[{"type":3,"textContent":" ","id":13},{"type":2,"tagName":"div","attributes":{"class":"image-container flex flex-wrap"},"childNodes":[{"type":3,"textContent":" ","id":15},{"type":2,"tagName":"img","attributes":{"src":"${url1}"},"childNodes":[],"id":16},{"type":3,"textContent":" ","id":17},{"type":2,"tagName":"img","attributes":{"src":"${url2}"},"childNodes":[],"id":18},{"type":3,"textContent":" ","id":19},{"type":2,"tagName":"img","attributes":{"src":"${url3}"},"childNodes":[],"id":20},{"type":3,"textContent":" ","id":21},{"type":2,"tagName":"div","attributes":{"style":"background: url('${url1}'); no-repeat center;"},"childNodes":[],"id":22},{"type":3,"textContent":" ","id":23},{"type":2,"tagName":"div","attributes":{"style":"background: url('${url2}'); no-repeat center;"},"childNodes":[],"id":24},{"type":3,"textContent":" ","id":25},{"type":2,"tagName":"div","attributes":{"style":"background: url('${url3}'); no-repeat center;"},"childNodes":[],"id":26},{"type":3,"textContent":" ","id":27}],"id":14},{"type":3,"textContent":" ","id":28}],"id":12}],"id":3}],"id":1}`;
1111
}
1212

13+
function createBaseTagSnapshot(url: string) {
14+
return JSON.stringify({
15+
type: 2,
16+
tagName: 'base',
17+
attributes: {
18+
href: url,
19+
},
20+
childNodes: [],
21+
id: 5,
22+
});
23+
}
24+
1325
function createImgSrcsetSnapshot({
1426
backupUrl,
1527
smallUrl,
@@ -374,5 +386,26 @@ describe('DOMSnapshot', () => {
374386
],
375387
});
376388
});
389+
390+
it('does change base tag href when there is a localhost', async () => {
391+
const originalSnapshot = createBaseTagSnapshot('http://localhost:3000/');
392+
const domSnapshot = new DOMSnapshot(originalSnapshot);
393+
const mappedSnapshot = await domSnapshot.mapAssetPaths(new Map());
394+
expect(mappedSnapshot).toEqual(createBaseTagSnapshot('/'));
395+
});
396+
397+
it('does not change base tag href when not localhost', async () => {
398+
const originalSnapshot = createBaseTagSnapshot('https://example.com/app/');
399+
const domSnapshot = new DOMSnapshot(originalSnapshot);
400+
const mappedSnapshot = await domSnapshot.mapAssetPaths(new Map());
401+
expect(mappedSnapshot).toEqual(originalSnapshot);
402+
});
403+
404+
it('does not change base tag href when already relative', async () => {
405+
const originalSnapshot = createBaseTagSnapshot('/app/');
406+
const domSnapshot = new DOMSnapshot(originalSnapshot);
407+
const mappedSnapshot = await domSnapshot.mapAssetPaths(new Map());
408+
expect(mappedSnapshot).toEqual(originalSnapshot);
409+
});
377410
});
378411
});

0 commit comments

Comments
 (0)