|
| 1 | +For every component, we create 3 type of tests: |
| 2 | +- isolated: we only test class logic (with mocks) |
| 3 | +- shallow: we test class and template logic (with mocks) |
| 4 | +- integrated: we test the integration with other components / services |
| 5 | + |
| 6 | +https://jasmine.github.io/2.0/introduction.html |
| 7 | + |
| 8 | + |
| 9 | +## Wait for async http |
| 10 | + |
| 11 | +Imports HttpClientTestingModule; use the mock as follow: |
| 12 | + |
| 13 | +```typescript |
| 14 | + httpMock = TestBed.get(HttpTestingController); |
| 15 | + let balanceRequest = httpMock.expectOne(AppSettings.apiUrl + '/wallet/1MN/balance'); |
| 16 | + balanceRequest.flush({ balance: 1, received: 2, unconfirmed: 3 }); |
| 17 | +``` |
| 18 | + |
| 19 | +We do not have to wait, the result will appear as expected in sync. |
| 20 | + |
| 21 | +## onChanges |
| 22 | +OnChanges should be triggered manually: |
| 23 | + |
| 24 | +```typescript |
| 25 | +component.address = "1MN"; |
| 26 | +component.ngOnChanges({ address: new SimpleChange(null, component.address, true) }); |
| 27 | +``` |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +- home-stats-component |
| 32 | +- address-balance-component |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; |
| 36 | +import { By } from '@angular/platform-browser'; |
| 37 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 38 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 39 | +import { Router } from '@angular/router'; |
| 40 | +// app |
| 41 | +import { CounterComponent } from './counter.component'; |
| 42 | +import { MenuComponent } from './menu.component'; |
| 43 | + |
| 44 | +/** |
| 45 | + * This component has some logic in it, and a very simple template. |
| 46 | + * For now, we only want to test the class logic. For doing so, |
| 47 | + * we will test only the component class without rendering the template. |
| 48 | + * This test is an Isolated test. |
| 49 | + */ |
| 50 | +describe('CounterComponent (isolated test)', () => { |
| 51 | + it('should instantiate', () => { |
| 52 | + const component: CounterComponent = new CounterComponent(); |
| 53 | + expect(component).toBeDefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should start with a counter at `0`', () => { |
| 57 | + const component: CounterComponent = new CounterComponent(); |
| 58 | + expect(component.counter).toEqual(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should be able to increment the counter (+1)', () => { |
| 62 | + const component: CounterComponent = new CounterComponent(); |
| 63 | + component.counter = 5; |
| 64 | + |
| 65 | + component.increment(); |
| 66 | + component.increment(); |
| 67 | + |
| 68 | + expect(component.counter).toEqual(7); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should be able to decrement the counter (-1)', () => { |
| 72 | + const component: CounterComponent = new CounterComponent(); |
| 73 | + component.counter = 5; |
| 74 | + |
| 75 | + component.decrement(); |
| 76 | + component.decrement(); |
| 77 | + |
| 78 | + expect(component.counter).toEqual(3); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +/** |
| 83 | + * Now that the inner class' logic is tested. |
| 84 | + * To test if the buttons trigger the right logic, we need to test them. |
| 85 | + * We need to render the template and trigger some clicks. |
| 86 | + * Because this component'template contains another component, |
| 87 | + * and we only want to test the relevant part of the template, we will not |
| 88 | + * render the child component. |
| 89 | + * This is a Shallow test. |
| 90 | + */ |
| 91 | +describe('CounterComponent (shallow test)', () => { |
| 92 | + let component: CounterComponent; |
| 93 | + let fixture: ComponentFixture<CounterComponent>; |
| 94 | + |
| 95 | + beforeEach(async(() => { |
| 96 | + TestBed.configureTestingModule({ |
| 97 | + declarations: [CounterComponent], |
| 98 | + schemas: [NO_ERRORS_SCHEMA] |
| 99 | + }).compileComponents(); // This is not needed if you are in the CLI context |
| 100 | + })); |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + fixture = TestBed.createComponent(CounterComponent); |
| 104 | + component = fixture.componentInstance; |
| 105 | + fixture.detectChanges(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should instantiate', () => { |
| 109 | + expect(component).toBeDefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should increment the counter if increment button is clicked (+1)', () => { |
| 113 | + const button = fixture.debugElement.nativeElement.querySelector('.button-up'); |
| 114 | + |
| 115 | + button.click(); |
| 116 | + button.click(); |
| 117 | + |
| 118 | + expect(component.counter).toEqual(2); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should decrement the counter if decrement button is clicked (-1)', () => { |
| 122 | + component.counter = 5; // Fake some increment clicks before. |
| 123 | + const button = fixture.debugElement.nativeElement.querySelector('.button-down'); |
| 124 | + |
| 125 | + button.click(); |
| 126 | + button.click(); |
| 127 | + |
| 128 | + expect(component.counter).toEqual(3); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should reset the counter if reset button is clicked (0)', () => { |
| 132 | + component.counter = 3; // Fake some increment clicks before. |
| 133 | + const button = fixture.debugElement.nativeElement.querySelector('.button-0'); |
| 134 | + |
| 135 | + button.click(); |
| 136 | + |
| 137 | + expect(component.counter).toEqual(0); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +/** |
| 142 | + * We could now go deeper and test the whole component with its dependencies, |
| 143 | + * see if everything is working great. |
| 144 | + * This is an Integrated test. |
| 145 | + */ |
| 146 | +describe('CounterComponent (integrated test)', () => { |
| 147 | + let component: CounterComponent; |
| 148 | + let fixture: ComponentFixture<CounterComponent>; |
| 149 | + let router: Router; |
| 150 | + |
| 151 | + beforeEach(async(() => { |
| 152 | + TestBed.configureTestingModule({ |
| 153 | + declarations: [CounterComponent, MenuComponent], |
| 154 | + imports: [RouterTestingModule] |
| 155 | + }).compileComponents(); // This is not needed if you are in the CLI context |
| 156 | + })); |
| 157 | + |
| 158 | + beforeEach(() => { |
| 159 | + fixture = TestBed.createComponent(CounterComponent); |
| 160 | + component = fixture.componentInstance; |
| 161 | + |
| 162 | + router = TestBed.get(Router); |
| 163 | + spyOn(router, 'navigateByUrl'); |
| 164 | + |
| 165 | + fixture.detectChanges(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should instantiate', () => { |
| 169 | + expect(component).toBeDefined(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should trigger the navigation to `/home`', async(() => { |
| 173 | + const link = fixture.debugElement.nativeElement.querySelector('.home-link'); |
| 174 | + |
| 175 | + link.click(); |
| 176 | + |
| 177 | + expect(router.navigateByUrl).toHaveBeenCalled(); |
| 178 | + })); |
| 179 | + |
| 180 | + it('should trigger the navigation to `/about`', async(() => { |
| 181 | + const link = fixture.debugElement.nativeElement.querySelector('.about-link'); |
| 182 | + |
| 183 | + link.click(); |
| 184 | + |
| 185 | + expect(router.navigateByUrl).toHaveBeenCalled(); |
| 186 | + })); |
| 187 | +}); |
| 188 | +``` |
0 commit comments