Skip to content

Commit

Permalink
Merge pull request #62 from AckeeCZ/feat/tests-2
Browse files Browse the repository at this point in the history
Feat/tests 2
  • Loading branch information
cermakjiri committed Sep 9, 2021
2 parents 58ab6f9 + c97ae34 commit 6a561cf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Node.js CI

on: [push, pull_request]
on: [pull_request]

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logger from 'loglevel';
import { createRequestUrl } from '..';

describe('createRequestUrl', () => {
it('composes request url', () => {
const url = createRequestUrl(
'/user/:id',
{
baseURL: 'https://example.com',
uriParams: {
id: '2',
},
params: new URLSearchParams({
page: '1',
}),
headers: {},
},
{
logger,
},
);

expect(url).toBe('https://example.com/user/2?page=1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { joinUrlChunks } from '..';

describe('joinUrlChunks', () => {
it('joins multiple pathnames with origin to create one URL', () => {
expect(joinUrlChunks('https://example.com/', '/a', 'b').toString()).toBe('https://example.com/a/b');
expect(joinUrlChunks('https://example.com', 'a').toString()).toBe('https://example.com/a');
expect(joinUrlChunks('https://example.com/')).toBeInstanceOf(URL);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { setUriParams } from '../uriParams';

describe('uriParams', () => {
it('replaces :id token with actual value', () => {
const url = setUriParams('/users/:group/:id', {
id: 1,
group: 'alpha',
});

expect(url).toEqual('/users/alpha/1');
});

it(`throws an error if value for given token isn't provided`, () => {
expect(() => setUriParams('/users/:id', {})).toThrow(TypeError);
});
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { GeneralConfig } from '../../../core/general-config';

import type { RequestConfig } from '../../../../types';
import type { FinalRequestConfig } from '../mergeRequestConfigs';

import { setUriParams } from './uriParams';

const removeSlashAtStartAndAtEnd = (chunk: string) => chunk.replace(/^\/|\/$/g, '');

function joinUrlChunks(baseUrl?: string, ...path: string[]) {
export function joinUrlChunks(baseUrl?: string, ...path: string[]) {
const joinedUrl = [baseUrl, ...path].filter(Boolean).map(removeSlashAtStartAndAtEnd).join('/');
return new URL(joinedUrl);
}

export function createRequestUrl(
requestUrl: string,
requestConfig: RequestConfig,
requestConfig: FinalRequestConfig,
generalConfig: GeneralConfig,
): string {
try {
Expand Down
3 changes: 2 additions & 1 deletion packages/@ackee/antonio-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FinalRequestConfig } from 'modules/request/utils';
import type { IterableStream } from './modules/response/iterableStream';

export type Primitive = bigint | boolean | null | number | string | undefined;
Expand Down Expand Up @@ -180,6 +181,6 @@ export interface RequestResult<D = any> {

export interface RequestParams {
url: string;
config: RequestConfig;
config: FinalRequestConfig;
bodyData: RequestBodyData;
}

0 comments on commit 6a561cf

Please sign in to comment.