Skip to content

Commit 1daafbd

Browse files
committed
refactor: improve testing
1 parent 038ec8a commit 1daafbd

File tree

4 files changed

+78
-64
lines changed

4 files changed

+78
-64
lines changed
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
export async function load({ fetch }) {
22
// fetch to root with trailing slash
3-
const response1 = await fetch('/');
3+
const response1 = await fetch('/', { redirect: 'manual' });
44
// fetch to root without trailing slash
5-
const response2 = await fetch('');
5+
const response2 = await fetch('', { redirect: 'manual' });
66
// fetch to root with custom base path with trailing slash
7-
const response3 = await fetch('/path-base/');
7+
const response3 = await fetch('/path-base/', { redirect: 'manual' });
88
// fetch to root with custom base path without trailing slash
9-
const response4 = await fetch('/path-base');
10-
9+
const response4 = await fetch('/path-base', { redirect: 'manual' });
1110
return {
12-
fetchUrl1: response1.url,
13-
fetchUrl2: response2.url,
14-
fetchUrl3: response3.url,
15-
fetchUrl4: response4.url,
16-
fetchResponse1: await response1.text(),
17-
fetchResponse2: await response2.text(),
18-
fetchResponse3: await response3.text(),
19-
fetchRedirect4: response4.headers.get('location')
11+
fetches: [
12+
{
13+
url: response1.url,
14+
response: await response1.text(),
15+
redirect: response1.headers.get('location')
16+
},
17+
{
18+
url: response2.url,
19+
response: await response2.text(),
20+
redirect: response2.headers.get('location')
21+
},
22+
{
23+
url: response3.url,
24+
response: await response3.text(),
25+
redirect: response3.headers.get('location')
26+
},
27+
{
28+
url: response4.url,
29+
response: await response4.text(),
30+
redirect: response4.headers.get('location')
31+
}
32+
]
2033
};
2134
}

packages/kit/test/apps/options/source/pages/fetch/link-root/+page.svelte

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
<h2>Fetch URLs</h2>
77

88
<dl>
9-
<dt>fetch1-url</dt>
10-
<dd data-testid="fetch1-url">{data.fetchUrl1}</dd>
11-
<dt>fetch2-url</dt>
12-
<dd data-testid="fetch2-url">{data.fetchUrl2}</dd>
13-
<dt>fetch3-url</dt>
14-
<dd data-testid="fetch3-url">{data.fetchUrl3}</dd>
15-
<dt>fetch4-url</dt>
16-
<dd data-testid="fetch4-url">{data.fetchUrl4}</dd>
9+
{#each data.fetches as item, index}
10+
<dt>fetch{index + 1}-url</dt>
11+
<dd data-testid={`fetch${index + 1}-url`}>{item.url}</dd>
12+
{/each}
1713
</dl>
1814

1915
<h2>Fetch Responses</h2>
2016
<dl>
21-
<dt>fetch1-response</dt>
22-
<dd data-testid="fetch1-response">{data.fetchResponse1}</dd>
23-
<dt>fetch2-response</dt>
24-
<dd data-testid="fetch2-response">{data.fetchResponse2}</dd>
25-
<dt>fetch3-response</dt>
26-
<dd data-testid="fetch3-response">{data.fetchResponse3}</dd>
27-
<dt>fetch4-redirect</dt>
28-
<dd data-testid="fetch4-redirect">{data.fetchRedirect4}</dd>
17+
{#each data.fetches as item, index}
18+
<dt>fetch{index + 1}-response</dt>
19+
<dd data-testid="fetch{index + 1}-response">{item.response}</dd>
20+
{/each}
21+
</dl>
22+
23+
<h2>Fetch Redirects</h2>
24+
<dl>
25+
{#each data.fetches as item, index}
26+
<dt>fetch{index + 1}-redirect</dt>
27+
<dd data-testid="fetch{index + 1}-redirect">{item.redirect}</dd>
28+
{/each}
2929
</dl>

packages/kit/test/apps/options/test/paths-assets.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ test.describe('base path', () => {
7777
expect(page.url()).toBe(`${baseURL}/path-base/resolve-route/resolved/`);
7878
expect(await page.textContent('h2')).toBe('resolved');
7979
});
80+
81+
test('fetch outside base path succeeds', async ({ page, baseURL }) => {
82+
await page.goto('/path-base/fetch/link-outside-base/');
83+
expect(await page.locator('[data-testid="fetch-url"]').textContent()).toContain(
84+
`${baseURL}/not-base-path/`
85+
);
86+
expect(await page.locator('[data-testid="fetch-response"]').textContent()).toContain(
87+
'did you mean to visit'
88+
);
89+
});
90+
91+
test('fetch to root succeeds', async ({ page, baseURL }) => {
92+
await page.goto('/path-base/fetch/link-root/');
93+
// fetch to root with trailing slash
94+
expect(await page.locator('[data-testid="fetch1-url"]').textContent()).toContain(`${baseURL}/`);
95+
const fetch1Response = await page.locator('[data-testid="fetch1-response"]').textContent();
96+
const fetch1Redirect = await page.locator('[data-testid="fetch1-redirect"]').textContent();
97+
expect(
98+
// production
99+
fetch1Response?.includes('did you mean to visit') ||
100+
// dev
101+
fetch1Redirect === '/path-base'
102+
).toBe(true);
103+
104+
// fetch to root without trailing slash should be relative
105+
expect(await page.locator('[data-testid="fetch2-url"]').textContent()).toBeFalsy();
106+
expect(await page.locator('[data-testid="fetch2-response"]').textContent()).toBe('relative');
107+
108+
// fetch to root with custom base path with trailing slash
109+
expect(await page.locator('[data-testid="fetch3-url"]').textContent()).toBeFalsy();
110+
expect(await page.locator('[data-testid="fetch3-response"]').textContent()).toBe('root');
111+
112+
// fetch to root with custom base path without trailing slash
113+
expect(await page.locator('[data-testid="fetch4-url"]').textContent()).toBeFalsy();
114+
expect(await page.locator('[data-testid="fetch4-redirect"]').textContent()).toBe('/path-base/');
115+
});
80116
});
81117

82118
test.describe('assets path', () => {

packages/kit/test/apps/options/test/test.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -258,39 +258,4 @@ test.describe('Async', () => {
258258
await expect(page.locator('h1', { hasText: 'Page B' })).toBeVisible();
259259
expect(logs).toEqual(['mounted', 'navigated']);
260260
});
261-
262-
test.describe('Fetch', () => {
263-
test('fetch outside base path succeeds', async ({ page, baseURL }) => {
264-
await page.goto('/path-base/fetch/link-outside-base/');
265-
expect(await page.locator('[data-testid="fetch-url"]').textContent()).toContain(
266-
`${baseURL}/not-base-path/`
267-
);
268-
expect(await page.locator('[data-testid="fetch-response"]').textContent()).toContain(
269-
'did you mean to visit'
270-
);
271-
});
272-
273-
test('fetch to root succeeds', async ({ page, baseURL }) => {
274-
await page.goto('/path-base/fetch/link-root/');
275-
// fetch to root with trailing slash
276-
expect(await page.locator('[data-testid="fetch1-url"]').textContent()).toContain(
277-
`${baseURL}/`
278-
);
279-
expect(await page.locator('[data-testid="fetch1-response"]').textContent()).toContain('root');
280-
281-
// fetch to root without trailing slash should be relative
282-
expect(await page.locator('[data-testid="fetch2-url"]').textContent()).toBeFalsy();
283-
expect(await page.locator('[data-testid="fetch2-response"]').textContent()).toBe('relative');
284-
285-
// fetch to root with custom base path with trailing slash
286-
expect(await page.locator('[data-testid="fetch3-url"]').textContent()).toBeFalsy();
287-
expect(await page.locator('[data-testid="fetch3-response"]').textContent()).toBe('root');
288-
289-
// fetch to root with custom base path without trailing slash
290-
expect(await page.locator('[data-testid="fetch4-url"]').textContent()).toBeFalsy();
291-
expect(await page.locator('[data-testid="fetch4-redirect"]').textContent()).toBe(
292-
'/path-base/'
293-
);
294-
});
295-
});
296261
});

0 commit comments

Comments
 (0)