Skip to content

Commit

Permalink
Merge pull request #102 from basics/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
StephanGerbeth authored Nov 27, 2024
2 parents 39aa30c + 71292a2 commit e3649d7
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 78 deletions.
12 changes: 12 additions & 0 deletions packages/operators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Project Changelog

# [@rxjs-collection/operators-v1.0.13-beta.1](https://github.com/basics/rxjs-collection/compare/@rxjs-collection/operators-v1.0.12...@rxjs-collection/operators-v1.0.13-beta.1) (2024-11-27)


### Bug Fixes

* **operators:** added error test case ([8495596](https://github.com/basics/rxjs-collection/commit/84955962d3f6bc50a6fe7e47bafaf0bcc686c231))
* **operators:** cleanup ([236fb2e](https://github.com/basics/rxjs-collection/commit/236fb2e6119c4e6911cf4ec7deed9afffe6cbb04))
* **operators:** log test ([14bcad4](https://github.com/basics/rxjs-collection/commit/14bcad4549391af15ec8486ecd4c6303d2ac372c))
* **operators:** more readable test data ([5edeb7c](https://github.com/basics/rxjs-collection/commit/5edeb7c73d542401c92f9f30f16ac3ebe3de7088))
* **operators:** refactored log ([345487e](https://github.com/basics/rxjs-collection/commit/345487e7f8aa0072c728d726a7a8517fae154330))
* **operators:** strip datetimeisostring from log output ([d26ab18](https://github.com/basics/rxjs-collection/commit/d26ab186cfbc91c3e1afc7237198475192049822))

# [@rxjs-collection/operators-v1.0.12](https://github.com/basics/rxjs-collection/compare/@rxjs-collection/operators-v1.0.11...@rxjs-collection/operators-v1.0.12) (2024-11-26)


Expand Down
2 changes: 1 addition & 1 deletion packages/operators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxjs-collection/operators",
"version": "1.0.12",
"version": "1.0.13-beta.1",
"description": "rxjs operators",
"license": "MIT",
"contributors": [
Expand Down
19 changes: 14 additions & 5 deletions packages/operators/src/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { mockResponse } from '#mocks/response.js';
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { cache } from './cache';
import { log } from './log';

describe('cache', () => {
let testScheduler;

beforeAll(async () => {
global.Response = mockResponse();
});

beforeEach(() => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('default', () => {
const expectedVal = {
a: new Response('initial', { status: 200 }),
b: new Response('updated', { status: 200 })
a: new Response('initial'),
b: new Response('updated')
};

const triggerVal = [expectedVal.a, expectedVal.b];
Expand All @@ -24,8 +33,8 @@ describe('cache', () => {
const stream = cold('a', { a: () => triggerVal.shift() }).pipe(
map(fn => fn()),
log('operators:cache:default:input'),
cache({ ttl: 2 })
// log('operators:cache:default:output')
cache({ ttl: 2 }),
log('operators:cache:default:output')
);

const unsubA = '-^!';
Expand Down
74 changes: 24 additions & 50 deletions packages/operators/src/log.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { bgGreen } from 'ansi-colors';
import debug from 'debug';
import { connectable, finalize, Subject, tap } from 'rxjs';
import { connectable, finalize, Observable, Subject } from 'rxjs';

import { pipeWhen } from './when';

export const enableLog = tag => {
debug.enable(tag);
};
export const enableLog = tag => debug.enable(tag);

export const log = tag => {
const logger = debug(tag);
logger.log = global.console.log.bind(console);
const error = debug(`${tag}:error`);

return source =>
source.pipe(
pipeWhen(
() => debug.enabled(tag),
tap({
subscribe: () => logger('subscribed'),
unsubscribe: () => logger('unsubscribed'),
finalize: () => logger('finalize'),
next: val => logger(val),
error: err => error(err),
complete: () => logger(bgGreen.bold('complete!'))
})
)
);
if (debug.enabled(tag)) {
return source => {
return new Observable(observer => {
return source.subscribe({
next: val => {
logger(val);
observer.next(val);
},
error: err => {
error(err);
observer.error(err);
},
complete: () => {
logger(bgGreen.bold('complete!'));
observer.complete();
}
});
});
};
}

return source => source;
};

export const logResult = (tag, observable) => {
Expand All @@ -40,33 +44,3 @@ export const logResult = (tag, observable) => {
).connect();
});
};

// export const log = tag => {
// var logger = debug(tag);
// logger.log = global.console.log.bind(console);
// var error = debug(`${tag}:error`);

// if (debug.enabled(tag)) {
// return source =>
// new Observable(observer => {
// source.subscribe({
// subscribe: () => logger('subscribed'),
// unsubscribe: () => logger('unsubscribed'),
// finalize: () => logger('finalize'),
// next: val => {
// logger(val);
// observer.next(val);
// },
// error: err => {
// error(err);
// observer.error(err);
// },
// complete: () => {
// logger(bgGreen.bold('complete!'));
// observer.complete();
// }
// });
// });
// }
// return source => source;
// };
90 changes: 86 additions & 4 deletions packages/operators/src/log.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,93 @@
import { describe, test } from 'vitest';
import { from, map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { enableLog, log, logResult } from './log';

describe('log', () => {
let testScheduler;

beforeEach(() => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('default', () => {
//TODO: add test
const expectedVal = {
a: 'content a',
b: 'content b',
c: 'content c'
};

const triggerVal = {
a: () => expectedVal.a,
b: () => expectedVal.b,
c: () => expectedVal.c,
d: () => {
throw new Error('custom error');
}
};

const expected = [
' operators:log:default content a',
' operators:log:default content b',
' operators:log:default content c'
];

const actual = [];
vi.spyOn(console, 'log').mockImplementation(v => {
actual.push(replaceDateTimeISOString(stripAnsiCodes(v)));
return v;
});

enableLog('operators:log:default');
testScheduler.run(({ cold, expectObservable, flush }) => {
const stream = cold('a-b-c-d|', triggerVal).pipe(
map(v => v()),
log('operators:log:default')
);
expectObservable(stream).toBe('a-b-c-#', expectedVal, new Error('custom error'));
flush();
expect(actual).deep.equal(expected);
});
});

test('logResult', () => {
//TODO: add test
test('logResult', async () => {
const actual = [];
vi.spyOn(console, 'log').mockImplementation(v => {
actual.push(replaceDateTimeISOString(stripAnsiCodes(v)));
return v;
});

const expectedVal = [
' operators:log:result content a',
' operators:log:result content b',
' operators:log:result content c',
' operators:log:result complete!'
];

const triggerVal = ['content a', 'content b', 'content c'];

enableLog('operators:log:result');
await logResult('operators:log:result', from(triggerVal));
expect(actual).deep.equal(expectedVal);
});
});

const stripAnsiCodes = str => {
return str.replace(
// eslint-disable-next-line security/detect-unsafe-regex, no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
);
};

const replaceDateTimeISOString = str => {
return str.replace(
/^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])\.[0-9]{3}Z/,
' '
);
};
10 changes: 5 additions & 5 deletions packages/operators/src/request/autoPagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe('auto pagination', () => {
const { autoPagination } = await import('./autoPagination');

const expectedVal = {
a: { value: 'a', next: 'b' },
b: { value: 'b', next: 'c' },
c: { value: 'c', next: 'd' },
d: { value: 'd', next: 'e' },
e: { value: 'e', next: null }
a: { value: 'content a', next: 'b' },
b: { value: 'content b', next: 'c' },
c: { value: 'content c', next: 'd' },
d: { value: 'content d', next: 'e' },
e: { value: 'content e', next: null }
};

const triggerVal = {
Expand Down
20 changes: 14 additions & 6 deletions packages/operators/src/request/concurrentRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ describe('concurrent request', () => {
test('default', async () => {
const { concurrentRequest } = await import('./concurrentRequest');

const expectedVal = {
a: 'content a',
b: 'content b',
c: 'content c',
d: 'content d',
e: 'content e'
};

const triggerVal = {
a: { t: 2, v: new Response('a') },
b: { t: 5, v: new Response('b') },
c: { t: 1, v: new Response('c') },
d: { t: 3, v: new Response('d') },
e: { t: 4, v: new Response('e') }
a: { t: 2, v: new Response(expectedVal.a) },
b: { t: 5, v: new Response(expectedVal.b) },
c: { t: 1, v: new Response(expectedVal.c) },
d: { t: 3, v: new Response(expectedVal.d) },
e: { t: 4, v: new Response(expectedVal.e) }
};

testScheduler.run(({ cold, expectObservable }) => {
Expand All @@ -43,7 +51,7 @@ describe('concurrent request', () => {
resolveText(),
log('operators:request:concurrent:output')
)
).toBe('---a--c-(bd)--(e|)');
).toBe('---a--c-(bd)--(e|)', expectedVal);
});
});
});
Expand Down
10 changes: 5 additions & 5 deletions packages/operators/src/request/lazyPagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ describe('lazy pagination', () => {
};

const expectedVal = {
a: { value: '1' },
b: { value: '2' },
c: { value: '3' },
d: { value: '4' },
e: { value: '5' }
a: { value: 'content a' },
b: { value: 'content b' },
c: { value: 'content c' },
d: { value: 'content d' },
e: { value: 'content e' }
};

const responseVal = {
Expand Down
6 changes: 5 additions & 1 deletion packages/operators/src/retry.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { log } from './log';
import { retryWhenRequestError } from './retry';
Expand All @@ -12,6 +12,10 @@ describe('request retry', () => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('2x error -> 1x success', () => {
const expectedVal = {
a: new Response('', { status: 500 }),
Expand Down
6 changes: 5 additions & 1 deletion packages/operators/src/when.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { log } from './log';
import { pipeWhen } from './when';
Expand All @@ -12,6 +12,10 @@ describe('when', () => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('default', () => {
const triggerVal = {
a: 1,
Expand Down

0 comments on commit e3649d7

Please sign in to comment.