Skip to content

Commit

Permalink
test: add cases for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 18, 2024
1 parent 85b7ab2 commit 1eb67b1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable unicorn/no-await-expression-member */

import {
cleanNilValues,
extractDataFromResponse,
isPlainObject,
normalizeUrl,
} from 'x-fetch'

test('isPlainObject', () => {
expect(isPlainObject({})).toBe(true)
expect(isPlainObject([])).toBe(false)
expect(isPlainObject(0)).toBe(false)
expect(isPlainObject(true)).toBe(false)
})

test('cleanNilValues', () => {
expect(cleanNilValues('')).toBe('')
expect(cleanNilValues(true)).toBe(true)
expect(cleanNilValues({ foo: 'bar' })).toEqual({ foo: 'bar' })
expect(cleanNilValues({ foo: 'bar', test: null })).toEqual({ foo: 'bar' })
expect(cleanNilValues({ foo: 'bar', test: '' })).toEqual({
foo: 'bar',
test: '',
})
expect(cleanNilValues({ foo: 'bar', test: '' }, true)).toEqual({ foo: 'bar' })
})

test('normalizeUrl', () => {
expect(normalizeUrl('')).toBe('')
expect(normalizeUrl('', { foo: 'bar' })).toBe('?foo=bar')
expect(normalizeUrl('', { foo: ['bar', 'baz'] })).toBe('?foo=bar&foo=baz')
expect(normalizeUrl('test?x=y')).toBe('test?x=y')
expect(normalizeUrl('test?x=y', { foo: 'bar' })).toBe('test?x=y&foo=bar')
expect(normalizeUrl('test?x=y', { foo: ['bar', 'baz'] })).toBe(
'test?x=y&foo=bar&foo=baz',
)
})

test('extractDataFromResponse', async () => {
expect(await extractDataFromResponse(new Response(''), null)).toBe(undefined)
expect(await extractDataFromResponse(new Response(), null)).toBe(undefined)
expect(await extractDataFromResponse(new Response(), 'text')).toBe('')
expect(await extractDataFromResponse(new Response(null), 'text')).toBe('')
expect(await extractDataFromResponse(new Response('foo'), 'text')).toBe('foo')
expect(await extractDataFromResponse(new Response('foo'), 'json')).toBe('foo')
expect(
await extractDataFromResponse(new Response('{"foo":"bar"}'), 'json'),
).toEqual({ foo: 'bar' })
expect(
(await extractDataFromResponse(new Response('foo'), 'arrayBuffer'))
.byteLength,
).toBe(3)
})

0 comments on commit 1eb67b1

Please sign in to comment.