Skip to content

Commit a86ab58

Browse files
committed
chore: add tests
1 parent 64fe173 commit a86ab58

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/helpers/files.js

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const matchesRedirect = (file, redirects) => {
4242
}
4343

4444
const matchesRewrite = (file, rewrites) => {
45+
if (Array.isArray(rewrites)) {
46+
return matchesRedirect(file, rewrites)
47+
}
4548
if (!Array.isArray(rewrites?.beforeFiles)) {
4649
return false
4750
}

test/index.js

+80-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const plugin = require('../src')
99

1010
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } = require('../src/constants')
1111
const { join } = require('pathe')
12-
const { matchMiddleware, stripLocale } = require('../src/helpers/files')
12+
const { matchMiddleware, stripLocale, matchesRedirect, matchesRewrite } = require('../src/helpers/files')
1313

1414
const FIXTURES_DIR = `${__dirname}/fixtures`
1515
const SAMPLE_PROJECT_DIR = `${__dirname}/../demo`
@@ -31,6 +31,45 @@ const utils = {
3131
},
3232
}
3333

34+
const REDIRECTS = [
35+
{
36+
source: '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/',
37+
destination: '/:file',
38+
locale: false,
39+
internal: true,
40+
statusCode: 308,
41+
regex: '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+))/$',
42+
},
43+
{
44+
source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)',
45+
destination: '/:notfile/',
46+
locale: false,
47+
internal: true,
48+
statusCode: 308,
49+
regex: '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+))$',
50+
},
51+
{
52+
source: '/en/redirectme',
53+
destination: '/',
54+
statusCode: 308,
55+
regex: '^(?!/_next)/en/redirectme(?:/)?$',
56+
},
57+
{
58+
source: '/:nextInternalLocale(en|es|fr)/redirectme',
59+
destination: '/:nextInternalLocale/',
60+
statusCode: 308,
61+
regex: '^(?!/_next)(?:/(en|es|fr))/redirectme(?:/)?$',
62+
},
63+
]
64+
65+
const REWRITES = [
66+
{
67+
source: '/:nextInternalLocale(en|es|fr)/old/:path*',
68+
destination: '/:nextInternalLocale/:path*',
69+
regex: '^(?:/(en|es|fr))/old(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$',
70+
},
71+
]
72+
3473
// Temporary switch cwd
3574
const changeCwd = function (cwd) {
3675
const originalCwd = process.cwd()
@@ -447,4 +486,44 @@ describe('utility functions', () => {
447486
expect(stripLocale(path, locales)).toEqual(path)
448487
}
449488
})
489+
490+
test('matchesRedirect correctly matches paths with locales', () => {
491+
const paths = ['en/redirectme.html', 'en/redirectme.json', 'fr/redirectme.html', 'fr/redirectme.json']
492+
paths.forEach((path) => {
493+
expect(matchesRedirect(path, REDIRECTS)).toBeTruthy()
494+
})
495+
})
496+
497+
test("matchesRedirect doesn't match paths with invalid locales", () => {
498+
const paths = ['dk/redirectme.html', 'dk/redirectme.json', 'gr/redirectme.html', 'gr/redirectme.json']
499+
paths.forEach((path) => {
500+
expect(matchesRedirect(path, REDIRECTS)).toBeFalsy()
501+
})
502+
})
503+
504+
test("matchesRedirect doesn't match internal redirects", () => {
505+
const paths = ['en/notrailingslash']
506+
paths.forEach((path) => {
507+
expect(matchesRedirect(path, REDIRECTS)).toBeFalsy()
508+
})
509+
})
510+
511+
it('matchesRewrite matches array of rewrites', () => {
512+
expect(matchesRewrite('en/old/page.html', REWRITES)).toBeTruthy()
513+
})
514+
515+
it('matchesRewrite matches beforeFiles rewrites', () => {
516+
expect(matchesRewrite('en/old/page.html', { beforeFiles: REWRITES })).toBeTruthy()
517+
})
518+
519+
it("matchesRewrite doesn't match afterFiles rewrites", () => {
520+
expect(matchesRewrite('en/old/page.html', { afterFiles: REWRITES })).toBeFalsy()
521+
})
522+
523+
it('matchesRewrite matches various paths', () => {
524+
const paths = ['en/old/page.html', 'fr/old/page.html', 'en/old/deep/page.html', 'en/old.html']
525+
paths.forEach((path) => {
526+
expect(matchesRewrite(path, REWRITES)).toBeTruthy()
527+
})
528+
})
450529
})

0 commit comments

Comments
 (0)