-
-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(router-generator): add tests to the utils (#2270)
- Loading branch information
1 parent
5f791fa
commit 93fb3df
Showing
3 changed files
with
156 additions
and
30 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
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,125 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { | ||
cleanPath, | ||
determineInitialRoutePath, | ||
multiSortBy, | ||
removeExt, | ||
removeUnderscores, | ||
routePathToVariable, | ||
} from '../src/utils' | ||
|
||
describe('cleanPath', () => { | ||
it('keeps path with leading slash and trailing slash', () => { | ||
expect(cleanPath('/test/')).toBe('/test/') | ||
}) | ||
}) | ||
|
||
describe('determineInitialRoutePath', () => { | ||
it('removes dots and adds slashes', () => { | ||
expect(determineInitialRoutePath('test.test')).toBe('/test/test') | ||
}) | ||
|
||
it('keeps leading slash', () => { | ||
expect(determineInitialRoutePath('/test.test')).toBe('/test/test') | ||
}) | ||
|
||
it('keeps trailing slash', () => { | ||
expect(determineInitialRoutePath('test.test/')).toBe('/test/test/') | ||
}) | ||
|
||
it('removes dots and adds slashes with leading and trailing slashes', () => { | ||
expect(determineInitialRoutePath('/test.test/')).toBe('/test/test/') | ||
}) | ||
|
||
it("returns '/' if path is empty", () => { | ||
expect(determineInitialRoutePath('')).toBe('/') | ||
}) | ||
|
||
it("returns '/' if path is '.'", () => { | ||
expect(determineInitialRoutePath('.')).toBe('/') | ||
}) | ||
|
||
it("returns '/' if path is './'", () => { | ||
expect(determineInitialRoutePath('./')).toBe('/') | ||
}) | ||
}) | ||
|
||
describe('multiSortBy', () => { | ||
it('sorts by multiple criteria', () => { | ||
const data = [ | ||
{ routePath: '/test/1/2/index', f: 'b' }, | ||
{ routePath: '/test/1', f: 'b' }, | ||
{ routePath: '/test/1/2/3/4/index', f: 'b' }, | ||
{ routePath: '/test/1/2/3', f: 'b' }, | ||
{ routePath: '/test/1/2/3/index', f: 'b' }, | ||
{ routePath: '/test/1/2', f: 'b' }, | ||
{ routePath: '/test/1/2/3/4', f: 'b' }, | ||
] | ||
|
||
const sorted = multiSortBy(data, [ | ||
(d) => (d.routePath.includes('1') ? -1 : 1), | ||
(d) => d.routePath.split('/').length, | ||
(d) => (d.routePath.endsWith('index') ? -1 : 1), | ||
(d) => d, | ||
]) | ||
|
||
expect(sorted).toEqual([ | ||
{ routePath: '/test/1', f: 'b' }, | ||
{ routePath: '/test/1/2', f: 'b' }, | ||
{ routePath: '/test/1/2/index', f: 'b' }, | ||
{ routePath: '/test/1/2/3', f: 'b' }, | ||
{ routePath: '/test/1/2/3/index', f: 'b' }, | ||
{ routePath: '/test/1/2/3/4', f: 'b' }, | ||
{ routePath: '/test/1/2/3/4/index', f: 'b' }, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('removeExt', () => { | ||
it('removes extension', () => { | ||
expect(removeExt('test.ts')).toBe('test') | ||
}) | ||
|
||
it('does not remove extension if no extension', () => { | ||
expect(removeExt('test')).toBe('test') | ||
}) | ||
|
||
it('removes extension with multiple dots', () => { | ||
expect(removeExt('test.test.ts')).toBe('test.test') | ||
}) | ||
|
||
it('removes extension with leading dot', () => { | ||
expect(removeExt('.test.ts')).toBe('.test') | ||
}) | ||
|
||
it('removes extension when in a route path', () => { | ||
expect(removeExt('/test/test.ts')).toBe('/test/test') | ||
}) | ||
}) | ||
|
||
describe('removeUnderscores', () => { | ||
it('removes leading underscore', () => { | ||
expect(removeUnderscores('_test')).toBe('test') | ||
}) | ||
|
||
it('removes trailing underscore', () => { | ||
expect(removeUnderscores('test_')).toBe('test') | ||
}) | ||
|
||
it('removes leading and trailing underscores', () => { | ||
expect(removeUnderscores('_test_')).toBe('test') | ||
}) | ||
}) | ||
|
||
describe('routePathToVariable', () => { | ||
it.each([ | ||
['/test/$/index', 'TestSplatIndex'], | ||
['/test/$', 'TestSplat'], | ||
['/test/$/', 'TestSplat'], | ||
['/test/index', 'TestIndex'], | ||
['/test', 'Test'], | ||
['/test/', 'Test'], | ||
])(`converts "%s" to "%s"`, (routePath, expected) => { | ||
expect(routePathToVariable(routePath)).toBe(expected) | ||
}) | ||
}) |