utils.test.ts
This file
Path
src/client/utils.test.ts
85 lines · 2.7 KiB · TypeScript
Last changed by
fix(client): Fix regex pattern with length limit at replaceUrlParam (#2193)Fukuibefore adoption13b5855
History of this fileTake it
The walk stopped after 200 commits: lines older than 2f9a07d point at it.
perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 1–32why, one step back1import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './utils'23describe('mergePath', () => {4 it('Should merge paths correctly', () => {5 expect(mergePath('http://localhost', '/api')).toBe('http://localhost/api')6 expect(mergePath('http://localhost/', '/api')).toBe('http://localhost/api')7 expect(mergePath('http://localhost', 'api')).toBe('http://localhost/api')8 expect(mergePath('http://localhost/', 'api')).toBe('http://localhost/api')9 expect(mergePath('http://localhost/', '/')).toBe('http://localhost/')10 })11})1213describe('replaceUrlParams', () => {14 it('Should replace correctly', () => {15 const url = 'http://localhost/posts/:postId/comments/:commentId'16 const params = {17 postId: '123',18 commentId: '456',19 }20 const replacedUrl = replaceUrlParam(url, params)21 expect(replacedUrl).toBe('http://localhost/posts/123/comments/456')22 })2324 it('Should replace correctly when there is regex pattern', () => {25 const url = 'http://localhost/posts/:postId{[abc]+}/comments/:commentId{[0-9]+}'26 const params = {27 postId: 'abc',28 commentId: '456',29 }30 const replacedUrl = replaceUrlParam(url, params)31 expect(replacedUrl).toBe('http://localhost/posts/abc/comments/456')32 })fix(client): Fix regex pattern with length limit at replaceUrlParam (#2193)Fukuibefore adoption13b5855
lines 33–42why, one step back3334 it('Should replace correctly when there is regex pattern with length limit', () => {35 const url = 'http://localhost/year/:year{[1-9]{1}[0-9]{3}}/month/:month{[0-9]{2}}'36 const params = {37 year: '2024',38 month: '2',39 }40 const replacedUrl = replaceUrlParam(url, params)41 expect(replacedUrl).toBe('http://localhost/year/2024/month/2')42 })perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 43–85why, one step back43})4445describe('removeIndexString', () => {46 it('Should remove last `index` string', () => {47 let url = 'http://localhost/index'48 let newUrl = removeIndexString(url)49 expect(newUrl).toBe('http://localhost/')5051 url = '/index'52 newUrl = removeIndexString(url)53 expect(newUrl).toBe('/')5455 url = '/sub/index'56 newUrl = removeIndexString(url)57 expect(newUrl).toBe('/sub/')5859 url = '/subindex'60 newUrl = removeIndexString(url)61 expect(newUrl).toBe('/subindex')62 })63})6465describe('deepMerge', () => {66 it('should return the source object if the target object is not an object', () => {67 const target = null68 const source = { a: 1 }69 const result = deepMerge(target, source)70 expect(result).toEqual(source)71 })7273 it('should merge two objects with object properties', () => {74 expect(75 deepMerge(76 { headers: { hono: '1' }, timeout: 2, params: {} },77 { headers: { hono: '2', demo: 2 }, params: undefined }78 )79 ).toStrictEqual({80 params: undefined,81 headers: { hono: '2', demo: 2 },82 timeout: 2,83 })84 })85})