-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from swup/chore/tests
Add tests
- Loading branch information
Showing
10 changed files
with
2,820 additions
and
832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Unit tests | ||
|
||
on: | ||
push: | ||
branches: [main, master, next] | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
run-tests: | ||
name: Run unit tests | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- name: Check out repo | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
|
||
- run: npm ci | ||
- run: npm run build | ||
|
||
- name: Run tests | ||
run: npm run test:unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function updateClassNames(el: HTMLElement, newEl: HTMLElement, { prefix = '' } = {}): void { | ||
const remove = [...el.classList].filter((className) => isValid(className, { prefix })); | ||
const add = [...newEl.classList].filter((className) => isValid(className, { prefix })); | ||
el.classList.remove(...remove); | ||
el.classList.add(...add); | ||
} | ||
|
||
function isValid(className: string, { prefix = '' } = {}): boolean { | ||
return !!className && className.startsWith(prefix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Vitest config file | ||
* @see https://vitest.dev/config/ | ||
*/ | ||
|
||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
environment: 'jsdom', | ||
include: ['tests/unit/**/*.test.ts'], | ||
setupFiles: [], | ||
testTimeout: 1000 | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { updateClassNames } from '../../src/classes.js'; | ||
|
||
const createElement = (className: string = '', { tagName = 'div' } = {}) => { | ||
const el = document.createElement(tagName); | ||
el.className = className; | ||
return el; | ||
}; | ||
|
||
const mergeClasses = ( | ||
currentClassName: string, | ||
incomingClassName: string, | ||
options: Parameters<typeof updateClassNames>[2] = {} | ||
) => { | ||
const current = createElement(currentClassName); | ||
const incoming = createElement(incomingClassName); | ||
updateClassNames(current, incoming, options); | ||
return current; | ||
}; | ||
|
||
describe('updateClassNames', () => { | ||
describe('class names', () => { | ||
it('adds a single class', () => { | ||
const el = mergeClasses('', 'a'); | ||
expect(el.className).toBe('a'); | ||
}); | ||
|
||
it('removes a single class', () => { | ||
const el = mergeClasses('a', ''); | ||
expect(el.className).toBe(''); | ||
}); | ||
|
||
it('clears out classname', () => { | ||
const el = mergeClasses('a b c', ''); | ||
expect(el.className).toBe(''); | ||
}); | ||
|
||
it('builds up classname', () => { | ||
const el = mergeClasses('', 'a b c'); | ||
expect(el.className).toBe('a b c'); | ||
}); | ||
|
||
it('only keeps classes present in new element', () => { | ||
const el = mergeClasses('a b c', 'd'); | ||
expect(el.className).toBe('d'); | ||
}); | ||
|
||
it('keeps classes present in both elements', () => { | ||
const el = mergeClasses('a b c', 'b e'); | ||
expect(el.className).toBe('b e'); | ||
}); | ||
}); | ||
|
||
describe('filtering', () => { | ||
it('only adds classes matching the prefix', () => { | ||
const el = mergeClasses('a b c', 'd pre-e f', { prefix: 'pre-' }); | ||
expect(el.className).toBe('a b c pre-e'); | ||
}); | ||
|
||
it('only removes classes matching the prefix', () => { | ||
const el = mergeClasses('a b pre-c', 'd e', { prefix: 'pre-' }); | ||
expect(el.className).toBe('a b'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { vitest, describe, expect, it, beforeEach, afterEach } from 'vitest'; | ||
import Swup, { Visit } from 'swup'; | ||
|
||
import SwupBodyClassPlugin from '../../src/index.js'; | ||
|
||
vitest.mock('../../src/classes.js'); | ||
|
||
const page = { page: { html: '', url: '/' } }; | ||
|
||
describe('SwupBodyClassPlugin', () => { | ||
let swup: Swup; | ||
let plugin: SwupBodyClassPlugin; | ||
let visit: Visit; | ||
|
||
beforeEach(() => { | ||
swup = new Swup(); | ||
plugin = new SwupBodyClassPlugin(); | ||
swup.use(plugin); | ||
// @ts-ignore - createVisit is marked internal | ||
visit = swup.createVisit({ url: '/' }); | ||
visit.to.document = new window.DOMParser().parseFromString( | ||
'<html><head></head><body></body></html>', | ||
'text/html' | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
swup.unuse(plugin); | ||
swup.destroy(); | ||
}); | ||
|
||
it('merges user options', async () => { | ||
const plugin = new SwupBodyClassPlugin({ prefix: 'pre-' }); | ||
expect(plugin.options).toMatchObject({ prefix: 'pre-' }); | ||
}); | ||
|
||
it('updates body classname from content:replace hook handler', async () => { | ||
const classes = await import('../../src/classes.js'); | ||
const spy = vitest.spyOn(classes, 'updateClassNames'); | ||
// classes.updateClassNames = vitest.fn() | ||
// .mockImplementation(() => ({ removed: [], added: [] })); | ||
|
||
await swup.hooks.call('content:replace', visit, page); | ||
|
||
expect(spy).toHaveBeenCalledOnce(); | ||
expect(spy).toHaveBeenCalledWith(document.body, visit.to.document!.body, { | ||
prefix: plugin.options.prefix | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
{ | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
"moduleResolution": "Node16", /* Specify how TypeScript looks up a file from a given module specifier. */ | ||
"rootDirs": ["./src"], /* Allow multiple folders to be treated as one when resolving modules. */ | ||
"resolveJsonModule": true, /* Enable importing .json files. */ | ||
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ | ||
"outDir": "./dist", /* Specify an output folder for all emitted files. */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */ | ||
"target": "es2022", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"rootDirs": ["./src"], | ||
"resolveJsonModule": true, | ||
"allowJs": true, | ||
"outDir": "./dist", | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"noImplicitAny": true | ||
} | ||
} |