If anotherObject is an object as you specify above you could do something like this. AnotherObject Stackblitz
describe("HelloComponent", () => {
let spectator: Spectator<HelloComponent>;
const createComponent = createComponentFactory({
component: HelloComponent,
providers: [],
detectChanges: false
});
beforeEach(() => {
spectator = createComponent();
});
it("should be disabled", done => {
const errors = [new Error("xyz"), new Error("abc")];
spectator.component.anotherObject = { errorSource$: of(errors) };
spectator.detectChanges();
spectator.component.isSearchEnabled$.subscribe({
next: isSearchEnabled => {
expect(isSearchEnabled).toBeFalse();
//You might want to test here whether your component template/state changed..
done();
}
});
});
it("should be enabled", done => {
spectator.component.anotherObject = { errorSource$: of([]) };
spectator.detectChanges();
spectator.component.isSearchEnabled$.subscribe({
next: isSearchEnabled => {
expect(isSearchEnabled).toBeTrue();
done();
}
});
});
});
If you get the observable from the akita query the best option is to mock the query method your observable is dependent on. I haven't tested the code below, but something similar should work.
let spectator: Spectator<HelloComponent>;
let query: SpyObject<AkitaAnotherObjectQuery>;
const createComponent = createComponentFactory({
component: HelloComponent,
mocks: [AkitaAnotherObjectQuery],
detectChanges: false
});
beforeEach(() => {
spectator = createComponent();
query = spectator.inject(AkitaAnotherObjectQuery);
//return empty array to check if search is enabled by default
query.selectErrorSource.andReturn(of([]));
});
it('should be enabled', () => {
spectator.detectChanges();
//todo test whether component state changed..
});
//test could be fake async or if you want to test the observable you can subscribe to it in the test below
it('should be disabled', () => {
const errors = [new Error('myError')];
query.selectErrorSource.andReturn(of(errors));
spectator.detectChanges();
//todo test whether component state changed..
});
....
Hope it works out, let me know if you have any more questions. If you want I can also add an example of the akita query mock in Stackblitz, just let me know.