Skip to content

Instantly share code, notes, and snippets.

@ardydedase
Created March 12, 2022 16:51
Show Gist options
  • Save ardydedase/5e09998d1c9dcd0af5fa9db81e3eb2d8 to your computer and use it in GitHub Desktop.
Save ardydedase/5e09998d1c9dcd0af5fa9db81e3eb2d8 to your computer and use it in GitHub Desktop.
Spectator test for sample app
import { AppComponent } from './app.component';
import { Spectator, createComponentFactory } from '@ngneat/spectator';
import { GithubService } from './github.service';
import { of } from 'rxjs';
describe('AppComponentSpectator', () => {
let spectator: Spectator<AppComponent>;
// Create componnent providers
const createComponent = createComponentFactory({
component: AppComponent,
providers: [
{ provide: GithubService, useValue: {} }
],
mocks: [GithubService],
});
beforeEach(() => spectator = createComponent());
it('should create', () => {
expect(spectator.component).toBeTruthy();
});
describe('initSearchUsers', () => {
const searchTerm = "angular";
it('should start search when there is search string entered', done => {
// Assert emitted values from the observables
spectator.component.searchSubject$.subscribe(res => {
expect(res).toBe(searchTerm);
done();
});
spectator.component.results$.subscribe(res => {
expect(res.total_count).toBe(100);
done();
});
// Mock the service
const githubService = spectator.inject(GithubService);
githubService.searchUsers.andReturn(of({
total_count: 100,
}));
// Call the methods
spectator.component.initSearchUsers();
spectator.component.searchUsersFormControl.setValue(searchTerm);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment