diff --git a/libs/rx-stateful/testing/src/index.ts b/libs/rx-stateful/testing/src/index.ts index 4209579..5ece960 100644 --- a/libs/rx-stateful/testing/src/index.ts +++ b/libs/rx-stateful/testing/src/index.ts @@ -1 +1,2 @@ export {mockRxStateful, RxStatefulMock} from './lib/mock-rx-stateful$' +export {mockRxRequest, RxRequestMock} from './lib/mock-rx-request' diff --git a/libs/rx-stateful/testing/src/lib/mock-rx-request.spec.ts b/libs/rx-stateful/testing/src/lib/mock-rx-request.spec.ts new file mode 100644 index 0000000..3b95e4e --- /dev/null +++ b/libs/rx-stateful/testing/src/lib/mock-rx-request.spec.ts @@ -0,0 +1,41 @@ +import { mockRxRequest } from "./mock-rx-request"; +import { subscribeSpyTo } from "@hirez_io/observer-spy"; + +describe('mockRxRequest', () => { + it('should create mock with all properties defined', () => { + const mock = mockRxRequest(); + + expect(mock.instance).toBeTruthy(); + expect(mock.instance.value$).toBeTruthy(); + expect(mock.instance.refresh).toBeTruthy(); + expect(mock.state$Trigger).toBeTruthy(); + expect(mock.refreshTrigger).toBeTruthy(); + }); + + describe('state$Trigger', () => { + it('should emit when trigger emits', () => { + const mock = mockRxRequest(); + + const result = subscribeSpyTo(mock.instance.value$()); + mock.state$Trigger.next({ isSuspense: true }); + mock.state$Trigger.next({ value: 'test', isSuspense: false }); + + expect(result.getValues()).toEqual([ + { isSuspense: true }, + { value: 'test', isSuspense: false } + ]); + }); + }); + + describe('refresh', () => { + it('should emit when refresh is called', () => { + const mock = mockRxRequest(); + const refreshSpy = jest.fn(); + + mock.refreshTrigger.subscribe(refreshSpy); + mock.instance.refresh(); + + expect(refreshSpy).toHaveBeenCalled(); + }); + }); +}); diff --git a/libs/rx-stateful/testing/src/lib/mock-rx-request.ts b/libs/rx-stateful/testing/src/lib/mock-rx-request.ts new file mode 100644 index 0000000..cbf2735 --- /dev/null +++ b/libs/rx-stateful/testing/src/lib/mock-rx-request.ts @@ -0,0 +1,29 @@ +import { RxStateful, RxRequest } from '@angular-kit/rx-stateful'; +import { BehaviorSubject, Observable, ReplaySubject, Subject } from "rxjs"; + +export interface RxRequestMock { + instance: RxRequest; + state$Trigger: Subject>>; + refreshTrigger: Subject; +} + +export function mockRxRequest(): RxRequestMock { + function createTrigger(startValue?: T | null | undefined) { + const trigger = startValue ? new BehaviorSubject(startValue) : new ReplaySubject(1); + return trigger; + } + + const state$Trigger = createTrigger>>(); + const refreshTrigger = new Subject(); + + const instance: RxRequest = { + value$: () => state$Trigger.asObservable() as Observable>, + refresh: () => refreshTrigger.next() + }; + + return { + instance, + state$Trigger, + refreshTrigger + }; +}