re-nav/tests/redirect.test.ts

121 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-10-05 17:08:17 +00:00
/// <reference path="../source/types.d.ts" />
import test from 'ava';
import {
matcherTypes,
2022-10-19 11:31:53 +00:00
narrowMatcherType,
narrowRedirectType,
2022-10-05 17:08:17 +00:00
parseRedirect,
redirectTypes,
2022-10-05 17:08:17 +00:00
HostnameRedirect,
Redirect,
2022-10-12 22:05:10 +00:00
Redirects,
2022-10-05 17:08:17 +00:00
RedirectParameters,
2022-10-18 17:02:02 +00:00
SimpleRedirect,
2022-10-05 17:08:17 +00:00
} from '../source/redirect/exports.js';
2022-10-12 22:23:32 +00:00
const hostnameParameters: HostnameRedirect['parameters'] = {
hostname: 'example.org',
matcherType: 'hostname',
2022-10-12 22:23:32 +00:00
toMatch: 'example.com',
redirectType: 'hostname',
2022-10-12 22:23:32 +00:00
};
2022-10-18 17:02:02 +00:00
const simpleParameters: SimpleRedirect['parameters'] = {
matcherType: 'hostname',
2022-10-18 17:02:02 +00:00
target: 'https://example.org/simple',
toMatch: 'example.com',
redirectType: 'simple',
2022-10-18 17:02:02 +00:00
};
2022-10-05 17:08:17 +00:00
test('parseRedirect', (t) => {
2022-10-12 22:05:10 +00:00
const samples: Array<Redirects['parameters']> = [
2022-10-05 17:08:17 +00:00
{
test: 'Invalid parameters',
2022-10-12 22:05:10 +00:00
} as unknown as Redirects['parameters'],
2022-10-12 22:30:26 +00:00
undefined as unknown as Redirects['parameters'],
2022-10-12 22:23:32 +00:00
hostnameParameters,
2022-10-18 17:02:02 +00:00
simpleParameters,
2022-10-05 17:08:17 +00:00
];
for (const sample of samples) {
const redirect = parseRedirect(sample);
if (redirect === undefined) {
t.pass('parseRedirect returned undefined');
} else {
t.snapshot(redirect, `Class ${redirect.constructor.name}`);
}
}
});
test('Redirect.redirect', (t) => {
2022-10-12 22:23:32 +00:00
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
2022-10-18 17:02:02 +00:00
const simpleRedirect = new SimpleRedirect(simpleParameters);
2022-10-05 17:08:17 +00:00
const samples: Array<[string, Redirect<RedirectParameters>]> = [
['https://example.com', hostnameRedirect],
['https://example.com/path#hash?query=test', hostnameRedirect],
2022-10-18 17:02:02 +00:00
['https://example.com', simpleRedirect],
['https://example.com/path', simpleRedirect],
2022-10-05 17:08:17 +00:00
];
for (const [index, [url, redirect]] of samples.entries()) {
t.snapshot(
{
original: url,
redirected: redirect.redirect(url).href,
},
`${index} ${redirect.constructor.name}`,
);
}
});
2022-10-12 22:21:46 +00:00
test('Redirect.isMatch', (t) => {
type UrlSamples = Array<[string, boolean]>;
2022-10-12 22:23:32 +00:00
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
2022-10-12 22:21:46 +00:00
const hostnameSamples: UrlSamples = [
['https://example.com', true],
2022-10-18 20:24:59 +00:00
['https://www.example.com', true],
2022-10-12 22:21:46 +00:00
['https://example.org', false],
];
const invalidRedirect = new HostnameRedirect({
test: 'invalid',
2022-10-18 16:57:15 +00:00
} as unknown as HostnameRedirect['parameters']);
2022-10-12 22:21:46 +00:00
const samples: Array<[Redirects, UrlSamples]> = [
[invalidRedirect, [['https://example.org', false]]],
[hostnameRedirect, hostnameSamples],
];
for (const [redirect, urlSamples] of samples) {
for (const [sample, expected] of urlSamples) {
t.is(redirect.isMatch(new URL(sample)), expected);
}
}
});
test('Narrow match & redirect types', (t) => {
2022-10-19 11:31:53 +00:00
t.false(narrowMatcherType('invalid'));
t.false(narrowRedirectType('invalid'));
2022-10-19 11:31:53 +00:00
t.true(matcherTypes.every((value) => narrowMatcherType(value)));
t.true(redirectTypes.every((value) => narrowRedirectType(value)));
});
test('Redirect getters & setters', (t) => {
const samples: Array<[Redirects, string]> = [
[new HostnameRedirect(hostnameParameters), hostnameParameters.hostname],
[new SimpleRedirect(simpleParameters), simpleParameters.target],
];
for (const [redirect, value] of samples) {
t.is(redirect.redirectValue, value);
const newValue = `${value} test`;
redirect.redirectValue = newValue;
t.is(redirect.redirectValue, newValue);
}
});