Skip to content

Commit f09fd6e

Browse files
wardbellhansl
authored andcommitted
test(aio): add sidenav tests and refactor related tests
1 parent 8cfa587 commit f09fd6e

File tree

4 files changed

+174
-98
lines changed

4 files changed

+174
-98
lines changed

aio/src/app/app.component.spec.ts

Lines changed: 115 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import { MockSwUpdateNotificationsService } from 'testing/sw-update-notification
2323
describe('AppComponent', () => {
2424
let component: AppComponent;
2525
let fixture: ComponentFixture<AppComponent>;
26+
27+
let docViewer: HTMLElement;
28+
let hamburger: HTMLButtonElement;
29+
let locationService: MockLocationService;
30+
let sidenav: HTMLElement;
31+
2632
const initialUrl = 'a/b';
2733

2834
beforeEach(async(() => {
@@ -45,6 +51,11 @@ describe('AppComponent', () => {
4551
fixture = TestBed.createComponent(AppComponent);
4652
component = fixture.componentInstance;
4753
fixture.detectChanges();
54+
55+
docViewer = fixture.debugElement.query(By.css('aio-doc-viewer')).nativeElement;
56+
hamburger = fixture.debugElement.query(By.css('.hamburger')).nativeElement;
57+
locationService = fixture.debugElement.injector.get(LocationService) as any;
58+
sidenav = fixture.debugElement.query(By.css('md-sidenav')).nativeElement;
4859
});
4960

5061
it('should create', () => {
@@ -58,10 +69,6 @@ describe('AppComponent', () => {
5869
});
5970
});
6071

61-
describe('is Hamburger Visible', () => {
62-
console.log('PENDING: AppComponent');
63-
});
64-
6572
describe('onResize', () => {
6673
it('should update `isSideBySide` accordingly', () => {
6774
component.onResize(1033);
@@ -71,40 +78,34 @@ describe('AppComponent', () => {
7178
});
7279
});
7380

74-
describe('SideNav when side-by-side', () => {
75-
let hamburger: HTMLButtonElement;
76-
let locationService: MockLocationService;
77-
let sidenav: Element;
81+
describe('SideNav when side-by-side (wide)', () => {
7882

7983
beforeEach(() => {
8084
component.onResize(1033); // side-by-side
81-
locationService = fixture.debugElement.injector.get(LocationService) as any;
82-
hamburger = fixture.debugElement.query(By.css('.hamburger')).nativeElement;
83-
sidenav = fixture.debugElement.query(By.css('md-sidenav')).nativeElement;
8485
});
8586

8687
it('should open when nav to a guide page (guide/pipes)', () => {
87-
locationService.urlSubject.next('guide/pipes');
88+
locationService.go('guide/pipes');
8889
fixture.detectChanges();
8990
expect(sidenav.className).toMatch(/sidenav-open/);
9091
});
9192

9293
it('should open when nav to an api page', () => {
93-
locationService.urlSubject.next('api/a/b/c/d');
94+
locationService.go('api/a/b/c/d');
9495
fixture.detectChanges();
9596
expect(sidenav.className).toMatch(/sidenav-open/);
9697
});
9798

9899
it('should be closed when nav to a marketing page (features)', () => {
99-
locationService.urlSubject.next('features');
100+
locationService.go('features');
100101
fixture.detectChanges();
101102
expect(sidenav.className).toMatch(/sidenav-clos/);
102103
});
103104

104105
describe('when manually closed', () => {
105106

106107
beforeEach(() => {
107-
locationService.urlSubject.next('guide/pipes');
108+
locationService.go('guide/pipes');
108109
fixture.detectChanges();
109110
hamburger.click();
110111
fixture.detectChanges();
@@ -113,59 +114,110 @@ describe('AppComponent', () => {
113114
it('should be closed', () => {
114115
expect(sidenav.className).toMatch(/sidenav-clos/);
115116
});
117+
118+
it('should stay closed when nav to another guide page', () => {
119+
locationService.go('guide/bags');
120+
fixture.detectChanges();
121+
expect(sidenav.className).toMatch(/sidenav-clos/);
122+
});
123+
124+
it('should stay closed when nav to api page', () => {
125+
locationService.go('api');
126+
fixture.detectChanges();
127+
expect(sidenav.className).toMatch(/sidenav-clos/);
128+
});
129+
130+
it('should reopen when nav to market page and back to guide page', () => {
131+
locationService.go('features');
132+
fixture.detectChanges();
133+
locationService.go('guide/bags');
134+
fixture.detectChanges();
135+
expect(sidenav.className).toMatch(/sidenav-open/);
136+
});
116137
});
117138
});
118139

119-
describe('SideNav when NOT side-by-side (mobile)', () => {
120-
let sidenav: Element;
121-
let locationService: MockLocationService;
140+
describe('SideNav when NOT side-by-side (narrow)', () => {
122141

123142
beforeEach(() => {
124143
component.onResize(1000); // NOT side-by-side
125-
locationService = fixture.debugElement.injector.get(LocationService) as any;
126-
sidenav = fixture.debugElement.query(By.css('md-sidenav')).nativeElement;
127144
});
128145

129146
it('should be closed when nav to a guide page (guide/pipes)', () => {
130-
locationService.urlSubject.next('guide/pipes');
147+
locationService.go('guide/pipes');
131148
fixture.detectChanges();
132149
expect(sidenav.className).toMatch(/sidenav-clos/);
133150
});
134151

135152
it('should be closed when nav to an api page', () => {
136-
locationService.urlSubject.next('api/a/b/c/d');
153+
locationService.go('api/a/b/c/d');
137154
fixture.detectChanges();
138155
expect(sidenav.className).toMatch(/sidenav-clos/);
139156
});
140157

141158
it('should be closed when nav to a marketing page (features)', () => {
142-
locationService.urlSubject.next('features');
159+
locationService.go('features');
143160
fixture.detectChanges();
144161
expect(sidenav.className).toMatch(/sidenav-clos/);
145162
});
146-
});
147163

148-
describe('pageId', () => {
149-
let locationService: MockLocationService;
164+
describe('when manually opened', () => {
165+
166+
beforeEach(() => {
167+
locationService.go('guide/pipes');
168+
fixture.detectChanges();
169+
hamburger.click();
170+
fixture.detectChanges();
171+
});
172+
173+
it('should be open', () => {
174+
expect(sidenav.className).toMatch(/sidenav-open/);
175+
});
176+
177+
it('should close when click in gray content area overlay', () => {
178+
const sidenavBackdrop = fixture.debugElement.query(By.css('.mat-sidenav-backdrop')).nativeElement;
179+
sidenavBackdrop.click();
180+
fixture.detectChanges();
181+
expect(sidenav.className).toMatch(/sidenav-clos/);
182+
});
183+
184+
it('should stay open when nav to another guide page', () => {
185+
locationService.go('guide/bags');
186+
fixture.detectChanges();
187+
expect(sidenav.className).toMatch(/sidenav-open/);
188+
});
189+
190+
it('should stay open when nav to api page', () => {
191+
locationService.go('api');
192+
fixture.detectChanges();
193+
expect(sidenav.className).toMatch(/sidenav-open/);
194+
});
195+
196+
it('should close again when nav to market page', () => {
197+
locationService.go('features');
198+
fixture.detectChanges();
199+
expect(sidenav.className).toMatch(/sidenav-clos/);
200+
});
150201

151-
beforeEach(() => {
152-
locationService = fixture.debugElement.injector.get(LocationService) as any;
153202
});
203+
});
204+
205+
describe('pageId', () => {
154206

155207
it('should set the id of the doc viewer container based on the current url', () => {
156208
const container = fixture.debugElement.query(By.css('section.sidenav-content'));
157209

158-
locationService.urlSubject.next('guide/pipes');
210+
locationService.go('guide/pipes');
159211
fixture.detectChanges();
160212
expect(component.pageId).toEqual('guide-pipes');
161213
expect(container.properties['id']).toEqual('guide-pipes');
162214

163-
locationService.urlSubject.next('news');
215+
locationService.go('news');
164216
fixture.detectChanges();
165217
expect(component.pageId).toEqual('news');
166218
expect(container.properties['id']).toEqual('news');
167219

168-
locationService.urlSubject.next('');
220+
locationService.go('');
169221
fixture.detectChanges();
170222
expect(component.pageId).toEqual('home');
171223
expect(container.properties['id']).toEqual('home');
@@ -174,35 +226,48 @@ describe('AppComponent', () => {
174226
it('should not be affected by changes to the query or hash', () => {
175227
const container = fixture.debugElement.query(By.css('section.sidenav-content'));
176228

177-
locationService.urlSubject.next('guide/pipes');
229+
locationService.go('guide/pipes');
178230
fixture.detectChanges();
179231

180-
locationService.urlSubject.next('guide/other?search=http');
232+
locationService.go('guide/other?search=http');
181233
fixture.detectChanges();
182234
expect(component.pageId).toEqual('guide-other');
183235
expect(container.properties['id']).toEqual('guide-other');
184236

185-
locationService.urlSubject.next('guide/http#anchor-1');
237+
locationService.go('guide/http#anchor-1');
186238
fixture.detectChanges();
187239
expect(component.pageId).toEqual('guide-http');
188240
expect(container.properties['id']).toEqual('guide-http');
189241
});
190242
});
191243

192244
describe('currentDocument', () => {
193-
console.log('PENDING: AppComponent currentDocument');
194-
});
195245

196-
describe('navigationViews', () => {
197-
console.log('PENDING: AppComponent navigationViews');
246+
it('should display a guide page (guide/pipes)', () => {
247+
locationService.go('guide/pipes');
248+
fixture.detectChanges();
249+
expect(docViewer.innerText).toMatch(/Pipes/i);
250+
});
251+
252+
it('should display the api page', () => {
253+
locationService.go('api');
254+
fixture.detectChanges();
255+
expect(docViewer.innerText).toMatch(/API/i);
256+
});
257+
258+
it('should display a marketing page', () => {
259+
locationService.go('features');
260+
fixture.detectChanges();
261+
expect(docViewer.innerText).toMatch(/Test Doc/i);
262+
});
263+
198264
});
199265

200266
describe('autoScrolling', () => {
201267
it('should AutoScrollService.scroll when the url changes', () => {
202-
const locationService: MockLocationService = fixture.debugElement.injector.get(LocationService) as any;
203268
const scrollService: AutoScrollService = fixture.debugElement.injector.get(AutoScrollService);
204269
spyOn(scrollService, 'scroll');
205-
locationService.urlSubject.next('some/url#fragment');
270+
locationService.go('some/url#fragment');
206271
expect(scrollService.scroll).toHaveBeenCalledWith(jasmine.any(HTMLElement));
207272
});
208273

@@ -234,9 +299,9 @@ describe('AppComponent', () => {
234299

235300
it('should intercept clicks not on the search elements and hide the search results', () => {
236301
const searchResults: SearchResultsComponent = fixture.debugElement.query(By.directive(SearchResultsComponent)).componentInstance;
237-
const docViewer = fixture.debugElement.query(By.css('aio-doc-viewer'));
238302
spyOn(searchResults, 'hideResults');
239-
docViewer.nativeElement.click();
303+
// docViewer is a commonly-clicked, non-search element
304+
docViewer.click();
240305
expect(searchResults.hideResults).toHaveBeenCalled();
241306
});
242307

@@ -263,6 +328,8 @@ describe('AppComponent', () => {
263328

264329
});
265330

331+
//// test helpers ////
332+
266333
class TestGaService {
267334
locationChanged = jasmine.createSpy('locationChanged');
268335
}
@@ -292,7 +359,12 @@ class TestHttp {
292359
"url": "guide/pipes",
293360
"title": "Pipes",
294361
"tooltip": "Pipes transform displayed values within a template."
295-
}
362+
},
363+
{
364+
"url": "guide/bags",
365+
"title": "Bags",
366+
"tooltip": "Pack your bags for a code adventure."
367+
},
296368
]
297369
},
298370
{

0 commit comments

Comments
 (0)