Skip to content

Commit

Permalink
test: update http source tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid committed Jul 28, 2023
1 parent 123bb53 commit ff132db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
14 changes: 6 additions & 8 deletions packages/fromHttpRequest/src/fromFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import * as nodeFetch from 'node-fetch';
import { fromFetch } from 'rxjs/fetch';

if (!globalThis.fetch) {
// eslint-disable-next-line
// @ts-ignore
globalThis.fetch = nodeFetch.default;
// eslint-disable-next-line
// @ts-ignore
globalThis.Request = nodeFetch.Request;
}
// eslint-disable-next-line
// @ts-ignore
globalThis.fetch = nodeFetch.default;
// eslint-disable-next-line
// @ts-ignore
globalThis.Request = nodeFetch.Request;

export { fromFetch };
/* eslint-enable eslint-comments/no-unlimited-disable */
15 changes: 9 additions & 6 deletions packages/source-http/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ const endpoints: [string, string, string] = [
'https://api.endpoint.com/3'
];

const options = {
const schedule = {
checkIntervalMins: 3,
initialDelayMs: 100,
initialDelayMs: 100
};

const options = {
endpoints,
transformResponseToPagesModulePath: '',
prefixDir: 'prefixDir'
Expand Down Expand Up @@ -62,7 +65,7 @@ describe('GIVEN an HTTP Source ', () => {
});

it('should merge results from all endpoints into 1 array', done => {
const source$: Observable<Page[]> = Source.create(options, {});
const source$: Observable<Page[]> = Source.create(options, { schedule });

source$.pipe(take(1)).subscribe({
next: result => {
Expand All @@ -73,7 +76,7 @@ describe('GIVEN an HTTP Source ', () => {
});

it('should transform the responses using the transform function', done => {
const source$: Observable<Page[]> = Source.create(options, {});
const source$: Observable<Page[]> = Source.create(options, { schedule });

source$.pipe(take(1)).subscribe({
next: result => {
Expand Down Expand Up @@ -107,7 +110,7 @@ describe('GIVEN an HTTP Source ', () => {
});

it('should merge results from **successful** endpoints into 1 array', done => {
const source$: Observable<Page[]> = Source.create(options, {});
const source$: Observable<Page[]> = Source.create(options, { schedule });

source$.pipe(take(1)).subscribe({
next: result => {
Expand Down Expand Up @@ -139,7 +142,7 @@ describe('GIVEN an HTTP Source ', () => {
server.close();
});
it('should merge results from **successful** endpoints into 1 array', done => {
const source$: Observable<Page[]> = Source.create(options, {});
const source$: Observable<Page[]> = Source.create(options, { schedule });

source$.pipe(take(1)).subscribe({
next: result => {
Expand Down
15 changes: 10 additions & 5 deletions packages/source-http/src/__tests__/scheduling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ function mockTransformer(response: any) {

import Source from '../index.js';

const options = {
const schedule = {
checkIntervalMins: 3,
initialDelayMs: 100,
initialDelayMs: 100
};

const options = {
endpoints: ['https://api.endpoint.com'],
transformResponseToPagesModulePath: '',
prefixDir: 'prefixDir'
Expand All @@ -52,7 +55,9 @@ describe('GIVEN an HTTP Source ', () => {

it('it emits a response after `initialDelayMs` and periodically after checkIntervalMins', () => {
testScheduler.run(({ expectObservable }) => {
const Source$: Observable<Page[]> = Source.create(options, {}).pipe(
const Source$: Observable<Page[]> = Source.create(options, {
schedule
}).pipe(
take(4), // make it actually finite, so it can be rendered
concatWith(NEVER) // but pretend it's infinite by not completing
);
Expand All @@ -65,8 +70,8 @@ describe('GIVEN an HTTP Source ', () => {
* Note, you need to knock 1 ms off the expected delay because the marbles themselves advance time by 1 virtual frame
* https://rxjs.dev/guide/testing/marble-testing#time-progression-syntax
*/
const delayMs = options.checkIntervalMins * 60000 - 1;
const expected = `${options.initialDelayMs}ms a ${delayMs}ms b ${delayMs}ms c ${delayMs}ms d`;
const delayMs = schedule.checkIntervalMins * 60000 - 1;
const expected = `${schedule.initialDelayMs}ms a ${delayMs}ms b ${delayMs}ms c ${delayMs}ms d`;

const expectedValues = options.endpoints.map(() => 'response');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ function toUpperCaseTransformer(response) {
return [response.correctHeaders.toUpperCase()];
}

const endpoints: [string] = ['https://api.endpoint.com/1'];
const endpoints: [string] = ['https://api.endpoint.com/my-source'];

const options = {
const schedule = {
checkIntervalMins: 3,
initialDelayMs: 100,
initialDelayMs: 100
};

const options = {
endpoints,
transformResponseToPagesModulePath: '',
prefixDir: 'prefixDir'
Expand Down Expand Up @@ -54,7 +57,7 @@ describe('GIVEN an HTTP Source ', () => {
server.close();
});
it('should use the request config', done => {
const source$: Observable<Page[]> = Source.create(options, {});
const source$: Observable<Page[]> = Source.create(options, { schedule });

source$.pipe(take(1)).subscribe({
next: result => {
Expand Down

0 comments on commit ff132db

Please sign in to comment.