-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add tests for mergeRequestConfigs util
- Loading branch information
1 parent
8f7fc6c
commit 41dc0af
Showing
5 changed files
with
137 additions
and
54 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
packages/@ackee/antonio-core/src/modules/request/utils/__tests__/mergeRequestConfigs.test.ts
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,133 @@ | ||
import Headers from 'fetch-headers'; | ||
import { mergeHeaders, mergeRequestConfigs } from '../mergeRequestConfigs'; | ||
|
||
describe('mergeHeaders', () => { | ||
it('returns empty headers if no args are provided', () => { | ||
expect(mergeHeaders()).toBeInstanceOf(Headers); | ||
expect([...mergeHeaders().keys()]).toHaveLength(0); | ||
}); | ||
|
||
it('merges object with nothing', () => { | ||
const headersA = { a: 'foo' }; | ||
const headersB = undefined; | ||
|
||
expect(mergeHeaders(headersA, headersB).get('a')).toBe('foo'); | ||
expect([...mergeHeaders(headersA, headersB).entries()]).toHaveLength(1); | ||
}); | ||
|
||
it('merges object with object', () => { | ||
const headersA = { a: 'foo' }; | ||
const headersB = { a: 'bar' }; | ||
|
||
expect(mergeHeaders(headersA, headersB).get('a')).toBe('bar'); | ||
expect([...mergeHeaders(headersA, headersB).entries()]).toHaveLength(1); | ||
}); | ||
|
||
it('merges object with headers', () => { | ||
const headersA = { a: 'foo' }; | ||
const headersB = new Headers({ a: 'bar' }); | ||
|
||
expect(mergeHeaders(headersA, headersB).get('a')).toBe('bar'); | ||
expect([...mergeHeaders(headersA, headersB).entries()]).toHaveLength(1); | ||
}); | ||
|
||
it('merges headers with headers', () => { | ||
const headersA = new Headers({ a: 'foo' }); | ||
const headersB = new Headers({ a: 'bar' }); | ||
|
||
expect(mergeHeaders(headersA, headersB).get('a')).toBe('bar'); | ||
expect(headersA.get('a')).toBe('foo'); | ||
expect(headersB.get('a')).toBe('bar'); | ||
expect([...mergeHeaders(headersA, headersB).entries()]).toHaveLength(1); | ||
}); | ||
|
||
it('merges headers with different headers', () => { | ||
const headersA = new Headers({ a: 'foo' }); | ||
const headersB = new Headers({ b: 'bar' }); | ||
|
||
expect(mergeHeaders(headersA, headersB).get('a')).toBe('foo'); | ||
expect(mergeHeaders(headersA, headersB).get('b')).toBe('bar'); | ||
expect(headersA.get('a')).toBe('foo'); | ||
expect(headersB.get('b')).toBe('bar'); | ||
expect([...mergeHeaders(headersA, headersB).entries()]).toHaveLength(2); | ||
}); | ||
}); | ||
|
||
describe('mergeRequestConfigs', () => { | ||
let configA; | ||
|
||
beforeEach(() => { | ||
configA = { | ||
headers: new Headers(), | ||
}; | ||
}); | ||
|
||
it('assigns cancel to signal', () => { | ||
const configB = { | ||
cancelToken: 'cancelToken', | ||
}; | ||
|
||
expect(mergeRequestConfigs(configA, configB)).toHaveProperty('signal'); | ||
}); | ||
|
||
it('merges uri params', () => { | ||
const configB = { | ||
uriParams: { id: '1' }, | ||
}; | ||
|
||
expect(mergeRequestConfigs(configA, configB)).toHaveProperty('uriParams'); | ||
expect(mergeRequestConfigs(configA, configB).uriParams).toHaveProperty('id'); | ||
expect(mergeRequestConfigs(configA, configB).uriParams?.id).toBe('1'); | ||
}); | ||
|
||
it('merges params (object with object)', () => { | ||
// before each creates a new instance of configA object, | ||
// so this mutation is fine | ||
configA.params = { | ||
foo: 'bar', | ||
page: 2, | ||
items: [4, 5, 6], | ||
}; | ||
const configB = { | ||
params: { | ||
page: 1, | ||
limit: 20, | ||
items: [1, 2, 3], | ||
}, | ||
}; | ||
|
||
const params = mergeRequestConfigs(configA, configB).params as URLSearchParams; | ||
|
||
expect(mergeRequestConfigs(configA, configB)).toHaveProperty('params'); | ||
expect(configA).toHaveProperty('params'); | ||
expect(configA.params.page).toBe(2); | ||
expect(configA.params.items).toEqual([4, 5, 6]); | ||
expect(configB).toHaveProperty('params'); | ||
expect(params.get('page')).toBe('1'); | ||
expect(params.get('limit')).toBe('20'); | ||
expect(params.get('foo')).toBe('bar'); | ||
expect(params.getAll('items')).toEqual(['1', '2', '3']); | ||
}); | ||
|
||
it('merges params (URLSearchParams with object)', () => { | ||
configA.params = { | ||
foo: 'bar', | ||
page: 2, | ||
}; | ||
const configB = { | ||
params: new URLSearchParams({ | ||
page: '1', | ||
limit: '20', | ||
}), | ||
}; | ||
|
||
const params = mergeRequestConfigs(configA, configB).params as URLSearchParams; | ||
|
||
expect(mergeRequestConfigs(configA, configB)).toHaveProperty('params'); | ||
expect(configA).toHaveProperty('params'); | ||
expect(configA.params.page).toBe(2); | ||
expect(configB).toHaveProperty('params'); | ||
expect(params.get('page')).toBe('1'); | ||
expect(params.get('limit')).toBe('20'); | ||
}); | ||
}); |
28 changes: 0 additions & 28 deletions
28
packages/@ackee/antonio-core/src/modules/request/utils/url/__tests__/searchParams.test.ts
This file was deleted.
Oops, something went wrong.
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
23 changes: 0 additions & 23 deletions
23
packages/@ackee/antonio-core/src/modules/request/utils/url/searchParams.ts
This file was deleted.
Oops, something went wrong.
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