Skip to content

Commit 597c52e

Browse files
authored
Merge pull request #72 from swup/fix/tests
fix: compatibility with swup 4.6.0
2 parents c7ce48f + e14793a commit 597c52e

14 files changed

+1388
-1206
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<div class="shields">
44

55
<!--[![npm version](https://img.shields.io/npm/v/@swup/fragment-plugin.svg)](https://www.npmjs.com/package/@swup/fragment-plugin) -->
6+
67
[![Unit Tests](https://img.shields.io/github/actions/workflow/status/swup/fragment-plugin/unit-tests.yml?branch=master&label=vitest)](https://github.com/swup/fragment-plugin/actions/workflows/unit-tests.yml)
78
[![E2E Tests](https://img.shields.io/github/actions/workflow/status/swup/fragment-plugin/e2e-tests.yml?branch=master&label=playwright)](https://github.com/swup/fragment-plugin/actions/workflows/e2e-tests.yml)
89
[![License](https://img.shields.io/github/license/swup/fragment-plugin.svg)](https://github.com/swup/fragment-plugin/blob/master/LICENSE)

package-lock.json

+1,289-1,121
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"vitest": "^0.34.6"
6363
},
6464
"peerDependencies": {
65-
"swup": "^4.0.0"
65+
"swup": "^4.6.0"
6666
},
6767
"browserslist": [
6868
"extends @swup/browserslist-config"

src/SwupFragmentPlugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type InitOptions = RequireKeys<Options, 'rules'>;
2020
*/
2121
export default class SwupFragmentPlugin extends PluginBase {
2222
readonly name = 'SwupFragmentPlugin';
23-
readonly requires = { swup: '>=4' };
23+
readonly requires = { swup: '>=4.6' };
2424

2525
protected _rawRules: Rule[] = [];
2626
protected _parsedRules: ParsedRule[] = [];

src/inc/ParsedRule.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type Options = {
2020
* Represents a Rule
2121
*/
2222
export default class ParsedRule {
23-
readonly matchesFrom;
24-
readonly matchesTo;
23+
readonly matchesFrom: ReturnType<typeof matchPath>;
24+
readonly matchesTo: ReturnType<typeof matchPath>;
2525

2626
swup: Swup;
2727
from: Path;

src/inc/functions.ts

-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ export const cacheForeignFragmentElements = ({ swup, logger }: FragmentPlugin):
316316

317317
// Update the cache of the current page with the updated html
318318
cache.update(currentUrl, {
319-
...currentCache,
320319
fragmentHtml: currentCachedDocument.documentElement.outerHTML
321320
});
322321

src/inc/handlers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ export const beforeContentReplace: Handler<'content:replace'> = function (
130130
const cache = this.swup.cache.get(visit.to.url);
131131
if (!cache || !cache.fragmentHtml) return;
132132

133-
args.page.html = cache.fragmentHtml;
133+
visit.to.document = new DOMParser().parseFromString(cache.fragmentHtml, 'text/html');
134+
visit.to.html = cache.fragmentHtml;
135+
134136
if (__DEV__) this.logger?.log(`fragment cache used for ${highlight(visit.to.url!)}`);
135137
};
136138

tests/config/vitest.config.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
* @see https://vitest.dev/config/
44
*/
55

6-
import path from "path";
6+
import path from 'path';
77
import { defineConfig } from 'vitest/config';
88

99
const __dirname = path.dirname(__filename);
1010

1111
export default defineConfig({
1212
test: {
1313
environment: 'jsdom',
14-
include: [
15-
'tests/vitest/**/*.test.ts'
16-
],
17-
setupFiles: [
18-
path.resolve(__dirname, './vitest.setup.ts')
19-
]
14+
include: ['tests/vitest/**/*.test.ts'],
15+
setupFiles: [path.resolve(__dirname, './vitest.setup.ts')]
2016
}
2117
});

tests/fixtures/assets/script.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ const allContainers = [...new Set([...swup.options.containers, ...fragmentContai
4242
* This will help to identify persisted elements more easily
4343
*/
4444
function addUniqueIds(selectors) {
45-
selectors.forEach(selector => {
45+
selectors.forEach((selector) => {
4646
document.querySelector(selector)?.setAttribute('data-uniqueid', uniqueId());
47-
})
47+
});
4848
}
4949

5050
addUniqueIds(allContainers);
5151
swup.hooks.on('content:replace', ({ containers }) => addUniqueIds(containers));
5252

53-
5453
swup.hooks.on('visit:start', () => document.documentElement.setAttribute('aria-busy', 'true'));
5554
swup.hooks.on('visit:end', () => document.documentElement.removeAttribute('aria-busy'));

tests/fixtures/assets/style.css

+59-56
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,108 @@
1-
@import url("./transitions.css");
1+
@import url('./transitions.css');
22

3-
*, *:before, *:after {
4-
box-sizing: border-box;
3+
*,
4+
*:before,
5+
*:after {
6+
box-sizing: border-box;
57
}
68

79
:root {
8-
--red: hsl(0, 74%, 67%);
9-
--green: hsl(100, 68%, 58%);
10-
--blue: oklch(59.12% 0.153 230.82);
10+
--red: hsl(0, 74%, 67%);
11+
--green: hsl(100, 68%, 58%);
12+
--blue: oklch(59.12% 0.153 230.82);
1113
}
1214
body {
13-
font-family: system-ui;
14-
padding: 1rem;
15-
max-width: 800px;
16-
margin: 5% auto;
17-
padding-bottom: 100vh;
15+
font-family: system-ui;
16+
padding: 1rem;
17+
max-width: 800px;
18+
margin: 5% auto;
19+
padding-bottom: 100vh;
1820
}
1921

20-
h1, h2, ul {
21-
margin-bottom: 1rem;
22+
h1,
23+
h2,
24+
ul {
25+
margin-bottom: 1rem;
2226
}
2327

2428
:focus-visible {
25-
box-shadow: 0 0 0 3px gold;
26-
outline: 0;
29+
box-shadow: 0 0 0 3px gold;
30+
outline: 0;
2731
}
2832

2933
/*
3034
* Lists
3135
*/
3236
:where(ul[class]) {
33-
list-style: none;
34-
padding: 0;
37+
list-style: none;
38+
padding: 0;
3539
}
3640
:where(ul:not([class])) {
37-
list-style-type: "> ";
38-
padding-left: 1rem;
39-
& li {
40-
padding-block: 0.2em;
41-
}
41+
list-style-type: '> ';
42+
padding-left: 1rem;
43+
& li {
44+
padding-block: 0.2em;
45+
}
4246
}
4347
:where(a) {
44-
color: var(--blue);
45-
text-decoration: none;
48+
color: var(--blue);
49+
text-decoration: none;
4650
}
4751

4852
/*
4953
* List
5054
*/
5155
.filters {
52-
display: flex;
53-
gap: 1rem;
56+
display: flex;
57+
gap: 1rem;
5458
}
5559
.filters a,
5660
a.button {
57-
padding: 0.2em 0.5em;
58-
border: 1px solid var(--blue);
59-
border-radius: 0.5em;
60-
&.is-active,
61-
&:hover {
62-
background: var(--blue);
63-
color: white;
64-
}
61+
padding: 0.2em 0.5em;
62+
border: 1px solid var(--blue);
63+
border-radius: 0.5em;
64+
&.is-active,
65+
&:hover {
66+
background: var(--blue);
67+
color: white;
68+
}
6569
}
6670
.list {
67-
display: grid;
68-
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
69-
gap: 1rem;
71+
display: grid;
72+
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
73+
gap: 1rem;
7074
}
7175
.list_item {
72-
border-radius: 100%;
73-
background: black;
76+
border-radius: 100%;
77+
background: black;
7478
}
7579
.list_item--red {
76-
background: var(--red);
80+
background: var(--red);
7781
}
7882
.list_item--green {
79-
background: var(--green);
83+
background: var(--green);
8084
}
8185
.list_item_link {
82-
display: flex;
83-
color: white;
84-
aspect-ratio: 1;
85-
display: flex;
86-
align-items: center;
87-
justify-content: center;
88-
border-radius: 100%;
89-
text-decoration: none;
90-
font-size: 2rem;
86+
display: flex;
87+
color: white;
88+
aspect-ratio: 1;
89+
display: flex;
90+
align-items: center;
91+
justify-content: center;
92+
border-radius: 100%;
93+
text-decoration: none;
94+
font-size: 2rem;
9195
}
9296

9397
/*
9498
* Modals
9599
*/
96100
.modal {
97-
max-width: 600px;
98-
padding: 1rem;
101+
max-width: 600px;
102+
padding: 1rem;
99103
}
100104
.modal_close {
101-
position: absolute;
102-
inset-block-start: 1rem;
103-
inset-inline-end: 1rem;
105+
position: absolute;
106+
inset-block-start: 1rem;
107+
inset-inline-end: 1rem;
104108
}
105-

tests/fixtures/assets/transitions.css

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@
22
* Default
33
*/
44
html.is-changing .transition-main {
5-
transition: opacity 250ms, transform 250ms;
5+
transition:
6+
opacity 250ms,
7+
transform 250ms;
68
}
79
html.is-animating .transition-main {
810
opacity: 0;
9-
transform: translateY(-30px);
11+
transform: translateY(-30px);
1012
}
1113
html.is-leaving .transition-main {
12-
transform: translateY(30px);
14+
transform: translateY(30px);
1315
}
1416
/*
1517
* List
1618
*/
1719
#list.is-changing {
18-
transition: opacity 300ms;
20+
transition: opacity 300ms;
1921
}
2022
#list.is-animating {
21-
opacity: 0;
23+
opacity: 0;
2224
}
2325

2426
/*
2527
* Modal
2628
*/
2729
#detail.is-changing {
28-
transition: transform 300ms, opacity 300ms;
30+
transition:
31+
transform 300ms,
32+
opacity 300ms;
2933
}
3034
#detail.is-animating {
31-
opacity: 0;
32-
transform: scale(0.9);
35+
opacity: 0;
36+
transform: scale(0.9);
3337
}

tests/vitest/ParsedRule.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ describe('ParsedRule', () => {
8686
});
8787

8888
it('should correctly match a rule', () => {
89-
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`);
89+
stubGlobalDocument(
90+
/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`
91+
);
9092
const rule = new ParsedRule({
9193
from: '/users/',
9294
to: '/user/:slug',
@@ -115,7 +117,9 @@ describe('ParsedRule', () => {
115117
expect(console.error).toBeCalledWith(new Error('skipping rule since #fragment-1 doesn\'t exist in the current document'), expect.any(Object)) // prettier-ignore
116118

117119
/** fragment element outside of swup's default containers */
118-
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`);
120+
stubGlobalDocument(
121+
/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`
122+
);
119123
expect(rule.matches({ from: '/foo/', to: '/bar/' })).toBe(false);
120124
expect(console.error).toBeCalledWith(new Error('skipping rule since #fragment-1 is outside of swup\'s default containers'), expect.any(Object)) // prettier-ignore
121125
});

tests/vitest/getFragmentVisit.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const fragmentPlugin = getMountedPluginInstance({
1313

1414
describe('getFragmentVisit()', () => {
1515
beforeEach(() => {
16-
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`);
16+
stubGlobalDocument(
17+
/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`
18+
);
1719
});
1820
afterEach(() => {
1921
vi.restoreAllMocks();

tests/vitest/handlePageView.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,19 @@ describe('handlePageView()', () => {
5252
});
5353

5454
it('should handle <dialog> fragment elements', () => {
55-
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"><dialog open id="fragment-1"></div></div>`);
55+
stubGlobalDocument(
56+
/*html*/ `<div id="swup" class="transition-main"><dialog open id="fragment-1"></div></div>`
57+
);
5658
handlePageView(fragmentPlugin);
5759

5860
const dialog = document.querySelector<HTMLDialogElement & FragmentElement>('#fragment-1');
5961
expect(dialog?.__swupFragment?.modalShown).toEqual(true);
6062
});
6163

6264
it("should ignore fragments outside of swup's main containers", () => {
63-
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`);
65+
stubGlobalDocument(
66+
/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`
67+
);
6468
handlePageView(fragmentPlugin);
6569

6670
const el = document.querySelector('#fragment-1');

0 commit comments

Comments
 (0)