Fabrício Silva
test('When customer spent more than 500$, should be classified as premium', () => { // Arrange const customerToClassify = { spent:505, joined: new Date(), id:1 } const DBStub = sinon.stub(dataAccess, 'getCustomer') .reply({id:1, classification: 'regular'}); // Act const receivedClassification = customerClassifier .classifyCustomer(customerToClassify); // Assert expect(receivedClassification).toMatch('premium'); });
faker.js
let service: ValueService; beforeEach(() => { TestBed.configureTestingModule({ providers: [ ValueService ] }); service = TestBed.get(ValueService); }); it('should use ValueService', () => { expect(service.getValue()).toBe('real value'); });
beforeEach(() => { TestBed.configureTestingModule({ declarations: [ WelcomeComponent ], providers: [ { provide: UserService, useClass: MockUserService } ] }); comp = TestBed.get(WelcomeComponent); userService = TestBed.get(UserService); });
it('should fire a change event after 200 millis', fakeAsync(() => { const component = new FiltersCmp((v) => v); const events = []; component.change.subscribe(v => events.push(v)); component.filters.controls['title'].setValue('N'); setTimeout(() => { component.filters.controls['title'].setValue('Ne'); }, 150); setTimeout(() => { component.filters.controls['title'].setValue('New'); }, 200); expect(events).toEqual([]); tick(1000); expect(events).toEqual([ {title: 'New', speaker: null, highRating: false} ]); component.filters.controls['title'].setValue('New!'); tick(1000); expect(events).toEqual([ {title: 'New', speaker: null, highRating: false}, {title: 'New!', speaker: null, highRating: false} ]); }));
beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ TalksCmp ], schemas: [ NO_ERRORS_SCHEMA ] }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(TalksCmp); component = fixture.componentInstance; fixture.detectChanges(); }); it('should render a list of talks', () => { component.talks = < any>[ { title: 'Are we there yet?' }, { title: 'The Value of Values' } ]; fixture.detectChanges(); const s = fixture.debugElement.nativeElement; const ts = s.querySelectorAll('talk-cmp'); expect(ts.length).toEqual(2); expect(ts[0].talk.title).toEqual('Are we there yet?'); expect(ts[1].talk.title).toEqual('The Value of Values'); });
beforeEach(async(() => { TestBed.configureTestingModule({ imports: [AppModule] }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(AppCmp); component = fixture.componentInstance; fixture.detectChanges(); el = fixture.debugElement.nativeElement; }); it('should filter talks by title', async(inject([App], (app: App) => { app.model.talks = [ ... ]; fixture.detectChanges(); expect(el.innerHTML).toContain("Are we there yet?"); expect(el.innerHTML).toContain("The Value of Values"); component.handleFiltersChange({ title: 'we', speaker: null, minRating: 0 }); fixture.detectChanges(); expect(el.innerHTML).toContain("Are we there yet?"); expect(el.innerHTML).not.toContain("The Value of Values"); })));
describe('ValueService', () => { let service: ValueService; beforeEach(() => { service = new ValueService(); }); it('#getValue should return real value', () => { expect(service.getValue()).toBe('real value'); }); it('#getObservableValue should return value from observable', (done: DoneFn) => { service.getObservableValue().subscribe(value => { expect(value).toBe('observable value'); done(); }); }); });
beforeEach(() => { TestBed.configureTestingModule({ imports: [ HttpClientTestingModule ] }); httpClient = TestBed.get(HttpClient); httpTestingController = TestBed.get(HttpTestingController); }); it('can test HttpClient.get', () => { const testData: Data = {name: 'Test Data'}; httpClient.get< Data >(testUrl) .subscribe(data => expect(data).toEqual(testData) ); const req = httpTestingController.expectOne('/data'); expect(req.request.method).toEqual('GET'); req.flush(testData); httpTestingController.verify(); });
import { Spectator, createComponentFactory } from '@ngneat/spectator'; import { ButtonComponent } from './button.component'; describe('ButtonComponent', () => { let spectator: Spectator< ButtonComponent >; const createComponent = createComponentFactory(ButtonComponent); beforeEach(() => spectator = createComponent()); it('should have a success class by default', () => { expect(spectator.query('button')).toHaveClass('success'); }); it('should set the class name according to the [className] input', () => { spectator.setInput('className', 'danger'); expect(spectator.query('button')).toHaveClass('danger'); expect(spectator.query('button')).not.toHaveClass('success'); }); });