-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rx-stateful): add testing utils for rxRequest
close #7
- Loading branch information
1 parent
4dd7ea2
commit db89be4
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {mockRxStateful, RxStatefulMock} from './lib/mock-rx-stateful$' | ||
export {mockRxRequest, RxRequestMock} from './lib/mock-rx-request' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>(); | ||
|
||
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<string>(); | ||
const refreshSpy = jest.fn(); | ||
|
||
mock.refreshTrigger.subscribe(refreshSpy); | ||
mock.instance.refresh(); | ||
|
||
expect(refreshSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { RxStateful, RxRequest } from '@angular-kit/rx-stateful'; | ||
import { BehaviorSubject, Observable, ReplaySubject, Subject } from "rxjs"; | ||
|
||
export interface RxRequestMock<T, E = unknown> { | ||
instance: RxRequest<T, E>; | ||
state$Trigger: Subject<Partial<RxStateful<T, E>>>; | ||
refreshTrigger: Subject<void>; | ||
} | ||
|
||
export function mockRxRequest<T, E = unknown>(): RxRequestMock<T, E> { | ||
function createTrigger<T>(startValue?: T | null | undefined) { | ||
const trigger = startValue ? new BehaviorSubject(startValue) : new ReplaySubject<T>(1); | ||
return trigger; | ||
} | ||
|
||
const state$Trigger = createTrigger<Partial<RxStateful<T, E>>>(); | ||
const refreshTrigger = new Subject<void>(); | ||
|
||
const instance: RxRequest<T, E> = { | ||
value$: () => state$Trigger.asObservable() as Observable<RxStateful<T, E>>, | ||
refresh: () => refreshTrigger.next() | ||
}; | ||
|
||
return { | ||
instance, | ||
state$Trigger, | ||
refreshTrigger | ||
}; | ||
} |