Skip to content

Commit

Permalink
Apply formatting to tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hirasso committed Mar 26, 2024
1 parent a87f597 commit 9d84715
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 79 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div class="shields">

<!--[![npm version](https://img.shields.io/npm/v/@swup/fragment-plugin.svg)](https://www.npmjs.com/package/@swup/fragment-plugin) -->

[![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)
[![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)
[![License](https://img.shields.io/github/license/swup/fragment-plugin.svg)](https://github.com/swup/fragment-plugin/blob/master/LICENSE)
Expand Down
10 changes: 3 additions & 7 deletions tests/config/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
* @see https://vitest.dev/config/
*/

import path from "path";
import path from 'path';
import { defineConfig } from 'vitest/config';

const __dirname = path.dirname(__filename);

export default defineConfig({
test: {
environment: 'jsdom',
include: [
'tests/vitest/**/*.test.ts'
],
setupFiles: [
path.resolve(__dirname, './vitest.setup.ts')
]
include: ['tests/vitest/**/*.test.ts'],
setupFiles: [path.resolve(__dirname, './vitest.setup.ts')]
}
});
5 changes: 2 additions & 3 deletions tests/fixtures/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ const allContainers = [...new Set([...swup.options.containers, ...fragmentContai
* This will help to identify persisted elements more easily
*/
function addUniqueIds(selectors) {
selectors.forEach(selector => {
selectors.forEach((selector) => {
document.querySelector(selector)?.setAttribute('data-uniqueid', uniqueId());
})
});
}

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


swup.hooks.on('visit:start', () => document.documentElement.setAttribute('aria-busy', 'true'));
swup.hooks.on('visit:end', () => document.documentElement.removeAttribute('aria-busy'));
115 changes: 59 additions & 56 deletions tests/fixtures/assets/style.css
Original file line number Diff line number Diff line change
@@ -1,105 +1,108 @@
@import url("./transitions.css");
@import url('./transitions.css');

*, *:before, *:after {
box-sizing: border-box;
*,
*:before,
*:after {
box-sizing: border-box;
}

:root {
--red: hsl(0, 74%, 67%);
--green: hsl(100, 68%, 58%);
--blue: oklch(59.12% 0.153 230.82);
--red: hsl(0, 74%, 67%);
--green: hsl(100, 68%, 58%);
--blue: oklch(59.12% 0.153 230.82);
}
body {
font-family: system-ui;
padding: 1rem;
max-width: 800px;
margin: 5% auto;
padding-bottom: 100vh;
font-family: system-ui;
padding: 1rem;
max-width: 800px;
margin: 5% auto;
padding-bottom: 100vh;
}

h1, h2, ul {
margin-bottom: 1rem;
h1,
h2,
ul {
margin-bottom: 1rem;
}

:focus-visible {
box-shadow: 0 0 0 3px gold;
outline: 0;
box-shadow: 0 0 0 3px gold;
outline: 0;
}

/*
* Lists
*/
:where(ul[class]) {
list-style: none;
padding: 0;
list-style: none;
padding: 0;
}
:where(ul:not([class])) {
list-style-type: "> ";
padding-left: 1rem;
& li {
padding-block: 0.2em;
}
list-style-type: '> ';
padding-left: 1rem;
& li {
padding-block: 0.2em;
}
}
:where(a) {
color: var(--blue);
text-decoration: none;
color: var(--blue);
text-decoration: none;
}

/*
* List
*/
.filters {
display: flex;
gap: 1rem;
display: flex;
gap: 1rem;
}
.filters a,
a.button {
padding: 0.2em 0.5em;
border: 1px solid var(--blue);
border-radius: 0.5em;
&.is-active,
&:hover {
background: var(--blue);
color: white;
}
padding: 0.2em 0.5em;
border: 1px solid var(--blue);
border-radius: 0.5em;
&.is-active,
&:hover {
background: var(--blue);
color: white;
}
}
.list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
gap: 1rem;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
gap: 1rem;
}
.list_item {
border-radius: 100%;
background: black;
border-radius: 100%;
background: black;
}
.list_item--red {
background: var(--red);
background: var(--red);
}
.list_item--green {
background: var(--green);
background: var(--green);
}
.list_item_link {
display: flex;
color: white;
aspect-ratio: 1;
display: flex;
align-items: center;
justify-content: center;
border-radius: 100%;
text-decoration: none;
font-size: 2rem;
display: flex;
color: white;
aspect-ratio: 1;
display: flex;
align-items: center;
justify-content: center;
border-radius: 100%;
text-decoration: none;
font-size: 2rem;
}

/*
* Modals
*/
.modal {
max-width: 600px;
padding: 1rem;
max-width: 600px;
padding: 1rem;
}
.modal_close {
position: absolute;
inset-block-start: 1rem;
inset-inline-end: 1rem;
position: absolute;
inset-block-start: 1rem;
inset-inline-end: 1rem;
}

20 changes: 12 additions & 8 deletions tests/fixtures/assets/transitions.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@
* Default
*/
html.is-changing .transition-main {
transition: opacity 250ms, transform 250ms;
transition:
opacity 250ms,
transform 250ms;
}
html.is-animating .transition-main {
opacity: 0;
transform: translateY(-30px);
transform: translateY(-30px);
}
html.is-leaving .transition-main {
transform: translateY(30px);
transform: translateY(30px);
}
/*
* List
*/
#list.is-changing {
transition: opacity 300ms;
transition: opacity 300ms;
}
#list.is-animating {
opacity: 0;
opacity: 0;
}

/*
* Modal
*/
#detail.is-changing {
transition: transform 300ms, opacity 300ms;
transition:
transform 300ms,
opacity 300ms;
}
#detail.is-animating {
opacity: 0;
transform: scale(0.9);
opacity: 0;
transform: scale(0.9);
}
8 changes: 6 additions & 2 deletions tests/vitest/ParsedRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ describe('ParsedRule', () => {
});

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

/** fragment element outside of swup's default containers */
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`);
stubGlobalDocument(
/*html*/ `<div id="swup" class="transition-main"></div><div id="fragment-1"></div>`
);
expect(rule.matches({ from: '/foo/', to: '/bar/' })).toBe(false);
expect(console.error).toBeCalledWith(new Error('skipping rule since #fragment-1 is outside of swup\'s default containers'), expect.any(Object)) // prettier-ignore
});
Expand Down
4 changes: 3 additions & 1 deletion tests/vitest/getFragmentVisit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const fragmentPlugin = getMountedPluginInstance({

describe('getFragmentVisit()', () => {
beforeEach(() => {
stubGlobalDocument(/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`);
stubGlobalDocument(
/*html*/ `<div id="swup" class="transition-main"><div id="fragment-1"></div></div>`
);
});
afterEach(() => {
vi.restoreAllMocks();
Expand Down
8 changes: 6 additions & 2 deletions tests/vitest/handlePageView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ describe('handlePageView()', () => {
});

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

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

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

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

0 comments on commit 9d84715

Please sign in to comment.