|
1 |
| -import { beforeEach, describe, expect, it } from 'vitest'; |
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { TestScheduler } from 'rxjs/testing';
|
3 | 3 | import { getMockWebsocketConnector } from './get-mock-websocket.connector';
|
4 | 4 | import { STREAM_STATUS } from '../core/constants';
|
5 |
| -import { delay, filter, scan, tap } from 'rxjs'; |
| 5 | +import { delay, filter, Observable, scan, tap } from 'rxjs'; |
| 6 | +import { SendRequestParams } from '../core/types'; |
6 | 7 |
|
7 | 8 | interface TestEvent {
|
8 | 9 | from: string;
|
@@ -490,4 +491,123 @@ describe('[getStreamHandler] rxjs marbles tests', () => {
|
490 | 491 | expect(socket.send).toHaveBeenNthCalledWith(4, JSON.stringify({ from: 'b2' }));
|
491 | 492 | /* eslint-enable @typescript-eslint/unbound-method */
|
492 | 493 | });
|
| 494 | + |
| 495 | + it('does not reset response on next request `resetResponseOnNextRequest: false`', () => { |
| 496 | + const { wsConnector, socket } = getMockWebsocketConnector(); |
| 497 | + |
| 498 | + const handler = wsConnector.getStreamHandler<TestEvent, TestEvent, TestEvent, unknown>({ |
| 499 | + resetResponseOnNextRequest: false, |
| 500 | + }); |
| 501 | + const expectedMarbles = 'a(bc)de'; |
| 502 | + const expectedValues = { |
| 503 | + a: { |
| 504 | + status: STREAM_STATUS.uninitialized, |
| 505 | + response: undefined, |
| 506 | + }, |
| 507 | + b: { |
| 508 | + status: STREAM_STATUS.loading, |
| 509 | + request: { from: 'b' }, |
| 510 | + response: undefined, |
| 511 | + error: undefined, |
| 512 | + }, |
| 513 | + c: { |
| 514 | + status: STREAM_STATUS.ready, |
| 515 | + request: { from: 'b' }, |
| 516 | + response: { from: 'b' }, |
| 517 | + error: undefined, |
| 518 | + }, |
| 519 | + d: { |
| 520 | + status: STREAM_STATUS.loading, |
| 521 | + request: { from: 'c' }, |
| 522 | + response: { from: 'b' }, |
| 523 | + error: undefined, |
| 524 | + }, |
| 525 | + e: { |
| 526 | + status: STREAM_STATUS.ready, |
| 527 | + request: { from: 'c' }, |
| 528 | + response: { from: 'd' }, |
| 529 | + error: undefined, |
| 530 | + }, |
| 531 | + }; |
| 532 | + |
| 533 | + const triggerMarbles = 'ab 3ms cd'; |
| 534 | + const triggerValues = { |
| 535 | + a: () => { |
| 536 | + wsConnector.connect(); |
| 537 | + socket.onopen!({} as Event); |
| 538 | + socket.send(JSON.stringify({ from: 'a' })); |
| 539 | + }, |
| 540 | + b: () => { |
| 541 | + handler.send({ request: { from: 'b' } }); |
| 542 | + socket.send(JSON.stringify({ from: 'b' })); |
| 543 | + }, |
| 544 | + c: () => { |
| 545 | + handler.send({ request: { from: 'c' } }); |
| 546 | + }, |
| 547 | + d: () => { |
| 548 | + socket.send(JSON.stringify({ from: 'd' })); |
| 549 | + }, |
| 550 | + }; |
| 551 | + |
| 552 | + testScheduler.run(({ expectObservable, cold }) => { |
| 553 | + expectObservable(handler.$).toBe(expectedMarbles, expectedValues); |
| 554 | + expectObservable(cold(triggerMarbles, triggerValues).pipe(tap((fn) => fn()))); |
| 555 | + }); |
| 556 | + }); |
| 557 | + |
| 558 | + it('applies `transformRequest` operator', () => { |
| 559 | + const { wsConnector, socket } = getMockWebsocketConnector(); |
| 560 | + |
| 561 | + const tapFn = vi.fn(); |
| 562 | + const transformRequests = vi.fn( |
| 563 | + (source$: Observable<SendRequestParams<TestEvent, TestEvent, TestEvent>>) => |
| 564 | + source$.pipe(delay(10), tap(tapFn)), |
| 565 | + ); |
| 566 | + |
| 567 | + const handler = wsConnector.getStreamHandler<TestEvent, TestEvent, TestEvent>({ |
| 568 | + transformRequests, |
| 569 | + }); |
| 570 | + const expectedMarbles = 'a 9ms bc'; |
| 571 | + const expectedValues = { |
| 572 | + a: { |
| 573 | + status: STREAM_STATUS.uninitialized, |
| 574 | + response: undefined, |
| 575 | + }, |
| 576 | + b: { |
| 577 | + status: STREAM_STATUS.loading, |
| 578 | + request: { from: 'a' }, |
| 579 | + response: undefined, |
| 580 | + error: undefined, |
| 581 | + }, |
| 582 | + c: { |
| 583 | + status: STREAM_STATUS.ready, |
| 584 | + request: { from: 'a' }, |
| 585 | + response: { from: 'b' }, |
| 586 | + error: undefined, |
| 587 | + }, |
| 588 | + }; |
| 589 | + |
| 590 | + const request: SendRequestParams<TestEvent, TestEvent, TestEvent> = { request: { from: 'a' } }; |
| 591 | + |
| 592 | + const triggerMarbles = 'a 10ms b'; |
| 593 | + const triggerValues = { |
| 594 | + a: () => { |
| 595 | + wsConnector.connect(); |
| 596 | + socket.onopen!({} as Event); |
| 597 | + handler.send(request); |
| 598 | + socket.send(JSON.stringify({ from: 'a' })); // <- ignored |
| 599 | + }, |
| 600 | + b: () => { |
| 601 | + socket.send(JSON.stringify({ from: 'b' })); |
| 602 | + }, |
| 603 | + }; |
| 604 | + |
| 605 | + testScheduler.run(({ expectObservable, cold }) => { |
| 606 | + expectObservable(handler.$).toBe(expectedMarbles, expectedValues); |
| 607 | + expectObservable(cold(triggerMarbles, triggerValues).pipe(tap((fn) => fn()))); |
| 608 | + }); |
| 609 | + |
| 610 | + expect(transformRequests).toHaveBeenCalledOnce(); |
| 611 | + expect(tapFn).toHaveBeenNthCalledWith(1, request); |
| 612 | + }); |
493 | 613 | });
|
0 commit comments