Skip to content
Myr

utils.test.ts

This file

Path

src/client/utils.test.ts

141 lines · 4.2 KiB · TypeScript

Last changed by

fix(client): return query params in $url (#3541)Alex Groverbefore adoption3d8abbc

History of this file

Take it

The walk stopped after 200 commits: lines older than 7ee829b point at it.

v4.4.7Yusuke Wadabefore adoption7ee829b

line 1why, one step back
1import {

fix(client): return query params in $url (#3541)Alex Groverbefore adoption3d8abbc

line 2why, one step back
2 buildSearchParams,

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 3–49why, one step back
3 deepMerge,
4 mergePath,5 removeIndexString,6 replaceUrlParam,7 replaceUrlProtocol,8} from './utils'910describe('mergePath', () => {11 it('Should merge paths correctly', () => {12 expect(mergePath('http://localhost', '/api')).toBe('http://localhost/api')13 expect(mergePath('http://localhost/', '/api')).toBe('http://localhost/api')14 expect(mergePath('http://localhost', 'api')).toBe('http://localhost/api')15 expect(mergePath('http://localhost/', 'api')).toBe('http://localhost/api')16 expect(mergePath('http://localhost/', '/')).toBe('http://localhost/')17 })18})1920describe('replaceUrlParams', () => {21 it('Should replace correctly', () => {22 const url = 'http://localhost/posts/:postId/comments/:commentId'23 const params = {24 postId: '123',25 commentId: '456',26 }27 const replacedUrl = replaceUrlParam(url, params)28 expect(replacedUrl).toBe('http://localhost/posts/123/comments/456')29 })3031 it('Should replace correctly when there is regex pattern', () => {32 const url = 'http://localhost/posts/:postId{[abc]+}/comments/:commentId{[0-9]+}'33 const params = {34 postId: 'abc',35 commentId: '456',36 }37 const replacedUrl = replaceUrlParam(url, params)38 expect(replacedUrl).toBe('http://localhost/posts/abc/comments/456')39 })4041 it('Should replace correctly when there is regex pattern with length limit', () => {42 const url = 'http://localhost/year/:year{[1-9]{1}[0-9]{3}}/month/:month{[0-9]{2}}'43 const params = {44 year: '2024',45 month: '2',46 }47 const replacedUrl = replaceUrlParam(url, params)48 expect(replacedUrl).toBe('http://localhost/year/2024/month/2')49 })

fix(client): replace optional params to url correctly (#3304)Yusuke Wadabefore adoption331b3d8

lines 50–59why, one step back
50
51 it('Should replace correctly when it has optional parameters', () => {52 const url = 'http://localhost/something/:firstId/:secondId/:version?'53 const params = {54 firstId: '123',55 secondId: '456',56 version: undefined,57 }58 const replacedUrl = replaceUrlParam(url, params)59 expect(replacedUrl).toBe('http://localhost/something/123/456')

fix(client): return query params in $url (#3541)Alex Groverbefore adoption3d8abbc

lines 60–71why, one step back
60 })
61})6263describe('buildSearchParams', () => {64 it('Should build URLSearchParams correctly', () => {65 const query = {66 id: '123',67 type: 'test',68 tag: ['a', 'b'],69 }70 const searchParams = buildSearchParams(query)71 expect(searchParams.toString()).toBe('id=123&type=test&tag=a&tag=b')

fix(client): replace optional params to url correctly (#3304)Yusuke Wadabefore adoption331b3d8

line 72why, one step back
72 })

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 73–141why, one step back
73})
7475describe('replaceUrlProtocol', () => {76 it('Should replace http to ws', () => {77 const url = 'http://localhost'78 const newUrl = replaceUrlProtocol(url, 'ws')79 expect(newUrl).toBe('ws://localhost')80 })8182 it('Should replace https to wss', () => {83 const url = 'https://localhost'84 const newUrl = replaceUrlProtocol(url, 'ws')85 expect(newUrl).toBe('wss://localhost')86 })8788 it('Should replace ws to http', () => {89 const url = 'ws://localhost'90 const newUrl = replaceUrlProtocol(url, 'http')91 expect(newUrl).toBe('http://localhost')92 })9394 it('Should replace wss to https', () => {95 const url = 'wss://localhost'96 const newUrl = replaceUrlProtocol(url, 'http')97 expect(newUrl).toBe('https://localhost')98 })99})100101describe('removeIndexString', () => {102 it('Should remove last `/index` string', () => {103 let url = 'http://localhost/index'104 let newUrl = removeIndexString(url)105 expect(newUrl).toBe('http://localhost/')106107 url = '/index'108 newUrl = removeIndexString(url)109 expect(newUrl).toBe('')110111 url = '/sub/index'112 newUrl = removeIndexString(url)113 expect(newUrl).toBe('/sub')114115 url = '/subindex'116 newUrl = removeIndexString(url)117 expect(newUrl).toBe('/subindex')118 })119})120121describe('deepMerge', () => {122 it('should return the source object if the target object is not an object', () => {123 const target = null124 const source = 'not an object' as unknown as Record<string, unknown>125 const result = deepMerge(target, source)126 expect(result).toEqual(source)127 })128129 it('should merge two objects with object properties', () => {130 expect(131 deepMerge(132 { headers: { hono: '1' }, timeout: 2, params: {} },133 { headers: { hono: '2', demo: 2 }, params: undefined }134 )135 ).toStrictEqual({136 params: undefined,137 headers: { hono: '2', demo: 2 },138 timeout: 2,139 })140 })141})