I have this function which I hope to return the url added with the parameters
export const addParamsToURL = (
params: param | param[],
url?: string
): string => {
if (!url) return ''
const __url = new URL(url)
const __params = new URLSearchParams(__url.search)
if (params instanceof Array) {
params.map(({key, value}) => __params.append(key, value))
} else {
__params.append(params.key, params.value)
}
return `${__url.origin}${__url.pathname}?${__params}`
}
I created the scenario test below and I would like to know if I should make a mock for it and what would be the best approach. Do I really need to mock? jest.fn, jest.mock? I'm still pretty new to testing
import {addParamsToURL} from '../../src/utils/addParamsToURL'
const baseURL = 'https://www.idinheiro.com.br'
const singleParam = {key: 'a', value: 'b'}
const multipleParams = [
{key: 'a', value: 'b'},
{key: 'c', value: 'd'},
]
describe('AddParamsToURL input a base url and params returns the full URL', () => {
it('Should return "" if not given a url', () => {
const res = addParamsToURL(singleParam, undefined)
expect(res).toBe('')
})
it('Should return the full url if given a simple param', () => {
const res = addParamsToURL(singleParam, baseURL)
expect(res).toBe('https://www.idinheiro.com.br/?a=b')
})
})