私はAngularを使用しており、jasmine-marblesを使用してユニットテストをしばらく書いています。これは私が理解できないように見えるシナリオです。をどのようにテストしdetermineNextSteps$
ますか? ジャスミン大理石を使用する以外に、これをテストするための他のソリューションも受け入れています。
例:
export class Service {
private person$: Observable<any> = this.store.pipe(
select(fromStore.selectPersonState),
map(({ person }) => person)
);
private parentSetsState$: Observable<any> = this.store.pipe(
select(fromStore.selectParentState)
);
private childSet$: Observable<any> = this.parentSetsState$.pipe(
map(({ parentSets }) =>
parentSets.find(set => set.name === 'FooBar')
)
);
determineNextSteps$: Observable<any> = this.childSet$.pipe(
tap(set =>
this.store.dispatch(
fromStore.determineStep({
set: set
})
)
),
pairwise(),
concatMap(pairedSet =>
this.person$.pipe(
first(),
map(person =>
!pairedSet[0].complete && pairedSet[1].complete
? this.router.navigate(['/home', 'profile', person.id, 'next-steps'])
: pairedSet
)
)
)
);
constructor(private store: Store<any>, private router: Router) {
}
}
現在の試行:
interface StoreState extends MockStoreState {}
describe('Service', () => {
let service: Service;
let mockStore: MockStore<fromStore.State>;
let router: Router;
let mockPersonStateSelector: MemoizedSelector<fromStore.State, fromPersonReducer.PersonState>;
let mockRegistrationSetsStateSelector: MemoizedSelector<
fromStore.State,
fromSetsReducer.SetsState
>;
const initialState: StoreState = { ...mockStoreInitialState };
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
Service,
{ provide: Router, useValue: { navigate: jest.fn() } },
provideMockStore({ initialState })
]
});
mockStore = TestBed.get(Store);
router = TestBed.get(Router);
mockPersonStateSelector = mockStore.overrideSelector(fromStore.selectPersonState, {
...fromPersonReducer.initialState
});
mockSetsStateSelector = mockStore.overrideSelector(fromProfileStore.selectSetsState, {
...fromSetsReducer.initialState
});
service = TestBed.get(Service);
spyOn(mockStore, 'dispatch');
});
it('should be created', () => {
expect(service).toBeTruthy();
});
describe('determineNextSteps$', () => {
describe('when the set completes after a previous incompletion', () => {
it('navigates the user to their next-steps', () => {
mockPersonStateSelector.setResult({ ...fromPersonReducer.initialState, person: { id: '1' } as any });
service['childSet$'] = cold('(ab)', {
a: { name: 'foobar', complete: false },
b: { name: 'foobar', complete: true }
});
expect(service.determineNextSteps$).toBeObservable(cold(''));
expect(router.navigate).toHaveBeenCalledWith(['/home', 'profile', '1', 'next-steps']);
});
});
});
});