Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Sep 27, 2020
1 parent 2723107 commit fb8ede7
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 95 deletions.
1 change: 0 additions & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:
- ubuntu-latest
node:
- 14.x
- 12.x
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .nycrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ require:
- source-map-support/register
exclude:
- '**/*.spec.ts'
- node_modules/**/*
- build/**/*
- 'projects/**/test-util/**/*'
- test/**/*
sourceMap: true
instrument: true
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@alorel-personal/conventional-changelog-alorel": "^2.1.3",
"@alorel/commons-array-types": "^1.1.0",
"@alorel/commons-obj": "^1.0.2",
"@alorel/eslint-config-base": "^1.0.17",
"@alorel/eslint-config-typescript": "^1.0.17",
"@alorel/rollup-plugin-copy": "^1.0.2",
Expand All @@ -48,12 +49,14 @@
"@semantic-release/github": "^7.1.1",
"@semantic-release/npm": "^7.0.6",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/jsdom": "^16.2.4",
"@types/mocha": "^8.0.3",
"@types/node": "^14.11.2",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.2.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cross-env": "^7.0.2",
"del-cli": "^3.0.1",
"doctoc": "^1.4.0",
Expand Down
38 changes: 0 additions & 38 deletions projects/core/index.spec.ts

This file was deleted.

61 changes: 61 additions & 0 deletions projects/core/lib/makeRequest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {expect} from 'chai';
import type {Mockttp} from 'mockttp';
import {RSP_HEADERS, setUpMockServer} from '../test-util/MockServer';
import {testAjaxOpts} from '../test-util/testAjax';
import type {UnprocessedResponse} from '../types/Response';
import {makeRequest} from './makeRequest';

/* eslint-disable @typescript-eslint/no-magic-numbers */

describe('makeRequest', function () {

describe('Successful response', () => {
let server: Mockttp;
let url: string;
let rsp: UnprocessedResponse<any>;

setUpMockServer(true, v => (server = v));
before(async () => {
await server.get('/success').thenJson(200, {su: 'ccess'}, RSP_HEADERS);
url = server.urlFor('/success');
rsp = await makeRequest<any>({...testAjaxOpts, preAsync: [], url}).toPromise();
});

it('Ok should be true', () => {
expect(rsp.ok).to.eq(true);
});

it('Original request should contain custom props', () => {
expect(rsp.response.request).to.haveOwnProperty('preAsync');
});

it('Should have json response', () => {
expect(rsp.response.response).to.deep.eq({su: 'ccess'});
});
});

describe('Error response', () => {
let server: Mockttp;
let url: string;
let rsp: UnprocessedResponse<any>;

setUpMockServer(true, v => (server = v));
before(async () => {
await server.get('/fail').thenJson(420, {fai: 'lure'}, RSP_HEADERS);
url = server.urlFor('/fail');
rsp = await makeRequest<any>({...testAjaxOpts, postAsync: [], url}).toPromise();
});

it('Ok should be true', () => {
expect(rsp.ok).to.eq(false);
});

it('Original request should contain custom props', () => {
expect(rsp.response.request).to.haveOwnProperty('postAsync');
});

it('Should have json response', () => {
expect(rsp.response.response).to.deep.eq({fai: 'lure'});
});
});
});
55 changes: 55 additions & 0 deletions projects/core/lib/merge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {expect} from 'chai';
import {identity, noop, of} from 'rxjs';
import type {RxAjaxOptions as Opt} from '../types/Options';
import {merge} from './merge';

/* eslint-disable @typescript-eslint/no-magic-numbers */

describe('merge', function () {
describe('headers', () => {
it('Should omit if absent in both defaults and custom', () => {
const d: Opt = {url: '/foo'};
const c: Opt = {withCredentials: true};
expect(merge(d, c)).to.deep.eq({url: '/foo', withCredentials: true});
});

it('Should merge if it has custom headers', () => {
const d: Opt = {headers: {foo: 'bar'}};
const c: Opt = {headers: {qux: 'baz'}};
expect(merge(d, c)).to.deep.eq({headers: {foo: 'bar', qux: 'baz'}});
});

it('Shouldn\'t change reference of using defaults', () => {
const d: Opt = {headers: {}};
const c: Opt = {url: '/'};
expect(merge(d, c).headers).to.eq(d.headers);
});
});

describe('arrays', () => {
it('Should omit if absent on both', () => {
expect(merge({}, {})).to.deep.eq({});
});

it('Should reuse default instance', () => {
const d: Opt = {pre: [identity]};
expect(merge(d, {}).pre).to.eq(d.pre);
});

it('Should reuse custom instance', () => {
const d: Opt = {post: [identity as any]};
expect(merge({}, d).post).to.eq(d.post);
});

it('Should concat and dedupe', () => {
const d: Opt = {postAsync: [identity, noop] as any[]};
const c: Opt = {postAsync: [noop, of] as any[]};
const {postAsync} = merge(d, c);

expect(postAsync).to.have.lengthOf(3, 'length');
expect(postAsync![0]).to.eq(identity, '[identity]');
expect(postAsync![1]).to.eq(noop, '[noop]');
expect(postAsync![2]).to.eq(of, '[of]');
});
});
});
7 changes: 5 additions & 2 deletions projects/core/lib/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export function merge(defaults: RxAjaxOptions, custom: RxAjaxOptions): RxAjaxOpt
} = custom;

const out: RxAjaxOptions = {...defaults, ...restCustom};
if (defaults.headers || headersCustom) {
out.headers = {...defaults.headers, ...headersCustom};
if (headersCustom) {
out.headers = {
...out.headers,
...headersCustom
};
}
mergeProp(out, 'pre', defaults.pre, preCustom);
mergeProp(out, 'preAsync', defaults.preAsync, preCustomAsync);
Expand Down
5 changes: 2 additions & 3 deletions projects/core/lib/middleware/applyAsyncPreMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {MonoTypeOperatorFunction as MonoOp, Observable} from 'rxjs';
import {of} from 'rxjs';
import {last, switchMap} from 'rxjs/operators';
import {switchMap} from 'rxjs/operators';
import type {RxAjaxOptions, RxAjaxPostAsyncHook} from '../../types/Options';

/** @internal */
Expand All @@ -12,8 +12,7 @@ export function applyAsyncPreMiddleware(opts: RxAjaxOptions): null | Observable<

const src$ = of(opts);
const args: Array<MonoOp<any>> = (preAsync as RxAjaxPostAsyncHook[])
.map(switchMap)
.concat(last());
.map(switchMap);

return src$.pipe.apply(src$, args); // eslint-disable-line prefer-spread
}
26 changes: 7 additions & 19 deletions projects/core/lib/middleware/applyPostAsyncMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import type {MonoTypeOperatorFunction as MonoOp, Observable} from 'rxjs';
import {from, of} from 'rxjs';
import {last, switchMap} from 'rxjs/operators';
import type {MonoTypeOperatorFunction as MonoOp} from 'rxjs';
import {pipe} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import type {RxAjaxOptions} from '../../types/Options';
import type {RxAjaxPostAsyncHook} from '../../types/Options';
import type {UnprocessedResponse} from '../../types/Response';

/** @internal */
export function applyPostAsyncMiddleware(
idx: number,
middleware: Required<RxAjaxOptions>['postAsync']
): MonoOp<UnprocessedResponse<any>> {
return switchMap(
function applyPostMiddlewareExecutor(response: UnprocessedResponse<any>): Observable<UnprocessedResponse<any>> {
if (response.stop) {
return of(response);
}
const pipes = (middleware as RxAjaxPostAsyncHook[])
.map((mid): MonoOp<UnprocessedResponse<any>> => switchMap(mid));

const pipes: Array<MonoOp<any>> = [last()];
if (middleware[idx + 1]) {
pipes[1] = applyPostAsyncMiddleware(idx + 1, middleware);
}

const src$ = from(middleware[idx](response));

return src$.pipe.apply(src$, pipes) as Observable<UnprocessedResponse<any>>; // eslint-disable-line prefer-spread
}
);
return pipe.apply(null, pipes as any[]) as MonoOp<UnprocessedResponse<any>>; // eslint-disable-line prefer-spread
}
7 changes: 0 additions & 7 deletions projects/core/lib/middleware/applyPostMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ export function applyPostMiddleware<T>(
): MonoTypeOperatorFunction<UnprocessedResponse<T>> {
return map(
function applyPostMiddlewareExecutor(rsp: UnprocessedResponse<T>): UnprocessedResponse<T> {
if (rsp.stop) {
return rsp;
}

let out = rsp;
for (let i = 0; i < middleware.length; i++) {
out = middleware[i](out);
if (out.stop) {
break;
}
}

return out;
Expand Down
Loading

0 comments on commit fb8ede7

Please sign in to comment.