-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(smartredirects): add tests for the meat of the code
- Loading branch information
Showing
2 changed files
with
88 additions
and
10 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
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,71 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { readFile } from '@web/test-runner-commands'; | ||
import { activateRedirects, getRedirect } from '../../scripts/redirects.js'; | ||
/* eslint-env mocha */ | ||
|
||
document.body.innerHTML = await readFile({ path: './dummy.html' }); | ||
document.head.innerHTML = await readFile({ path: './head.html' }); | ||
|
||
describe.only('Redirects', () => { | ||
it('loads redirects', async () => { | ||
const emptyRedirects = []; | ||
const redirects = await activateRedirects(emptyRedirects); | ||
expect(redirects).to.deep.equal(emptyRedirects); | ||
}); | ||
|
||
it('applies applies a simple redirect', async () => { | ||
const exampleRedirects = [ | ||
{ | ||
from: '/foo', | ||
to: '/bar', | ||
start: 1, | ||
}, | ||
]; | ||
const redirects = await activateRedirects(exampleRedirects); | ||
const currentURL = new URL('https://example.com/foo'); | ||
const redirect = await getRedirect(redirects, '/foo', currentURL); | ||
expect(redirect).to.equal('https://example.com/bar?redirect_from=%2Ffoo'); | ||
}); | ||
|
||
it('applies applies a redirect with a parameter', async () => { | ||
const exampleRedirects = [ | ||
{ | ||
from: '/foo/*', | ||
to: '/bar/*', | ||
start: 1, | ||
}, | ||
]; | ||
const redirects = await activateRedirects(exampleRedirects); | ||
const currentURL = new URL('https://example.com/foo/baz'); | ||
const redirect = await getRedirect(redirects, '/foo/baz', currentURL); | ||
expect(redirect).to.equal('https://example.com/bar/baz?redirect_from=%2Ffoo%2Fbaz'); | ||
}); | ||
|
||
it('applies applies a redirect with multiple parameters', async () => { | ||
const exampleRedirects = [ | ||
{ | ||
from: '/foo/*/*', | ||
to: '/bar/*/baz/*', | ||
start: 1, | ||
}, | ||
]; | ||
const redirects = await activateRedirects(exampleRedirects); | ||
const currentURL = new URL('https://example.com/foo/fifi/qux'); | ||
const redirect = await getRedirect(redirects, '/foo/fifi/qux', currentURL); | ||
expect(redirect).to.equal('https://example.com/bar/fifi/baz/qux?redirect_from=%2Ffoo%2Ffifi%2Fqux'); | ||
}); | ||
|
||
it('applies applies a redirect with multiple parameters and changed order', async () => { | ||
const exampleRedirects = [ | ||
{ | ||
from: '/foo/*/*', | ||
to: '/bar/$2/baz/$1', | ||
start: 1, | ||
}, | ||
]; | ||
const redirects = await activateRedirects(exampleRedirects); | ||
const currentURL = new URL('https://example.com/foo/fifi/qux'); | ||
const redirect = await getRedirect(redirects, '/foo/fifi/qux', currentURL); | ||
expect(redirect).to.equal('https://example.com/bar/qux/baz/fifi?redirect_from=%2Ffoo%2Ffifi%2Fqux'); | ||
}); | ||
}); |