Skip to content

Commit 8227b56

Browse files
petebacondarwinAndrewKushnir
authored andcommitted
style(docs-infra): reformat ScrollService file (angular#30630)
Pre-empting code formatting changes when the code is updated in a subsequent commit. PR Close angular#30630
1 parent 1609815 commit 8227b56

File tree

2 files changed

+146
-152
lines changed

2 files changed

+146
-152
lines changed

aio/src/app/shared/scroll.service.spec.ts

Lines changed: 125 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ReflectiveInjector } from '@angular/core';
2-
import { Location, LocationStrategy, PlatformLocation, ViewportScroller } from '@angular/common';
3-
import { DOCUMENT } from '@angular/common';
4-
import { MockLocationStrategy, SpyLocation } from '@angular/common/testing';
5-
import { fakeAsync, tick } from '@angular/core/testing';
1+
import {Location, LocationStrategy, PlatformLocation, ViewportScroller} from '@angular/common';
2+
import {DOCUMENT} from '@angular/common';
3+
import {MockLocationStrategy, SpyLocation} from '@angular/common/testing';
4+
import {ReflectiveInjector} from '@angular/core';
5+
import {fakeAsync, tick} from '@angular/core/testing';
66

7-
import { ScrollService, topMargin } from './scroll.service';
7+
import {ScrollService, topMargin} from './scroll.service';
88

99
describe('ScrollService', () => {
1010
const scrollServiceInstances: ScrollService[] = [];
@@ -32,27 +32,25 @@ describe('ScrollService', () => {
3232
}
3333

3434
class MockElement {
35-
getBoundingClientRect = jasmine.createSpy('Element getBoundingClientRect')
36-
.and.returnValue({top: 0});
35+
getBoundingClientRect =
36+
jasmine.createSpy('Element getBoundingClientRect').and.returnValue({top: 0});
3737
scrollIntoView = jasmine.createSpy('Element scrollIntoView');
3838
}
3939

40-
const viewportScrollerStub = jasmine.createSpyObj(
41-
'viewportScroller',
42-
['getScrollPosition', 'scrollToPosition']);
40+
const viewportScrollerStub =
41+
jasmine.createSpyObj('viewportScroller', ['getScrollPosition', 'scrollToPosition']);
4342

4443
beforeEach(() => {
4544
injector = ReflectiveInjector.resolveAndCreate([
46-
{
47-
provide: ScrollService,
48-
useFactory: createScrollService,
49-
deps: [DOCUMENT, PlatformLocation, ViewportScroller, Location],
50-
},
51-
{ provide: Location, useClass: SpyLocation },
52-
{ provide: DOCUMENT, useClass: MockDocument },
53-
{ provide: PlatformLocation, useClass: MockPlatformLocation },
54-
{ provide: ViewportScroller, useValue: viewportScrollerStub },
55-
{ provide: LocationStrategy, useClass: MockLocationStrategy }
45+
{
46+
provide: ScrollService,
47+
useFactory: createScrollService,
48+
deps: [DOCUMENT, PlatformLocation, ViewportScroller, Location],
49+
},
50+
{provide: Location, useClass: SpyLocation}, {provide: DOCUMENT, useClass: MockDocument},
51+
{provide: PlatformLocation, useClass: MockPlatformLocation},
52+
{provide: ViewportScroller, useValue: viewportScrollerStub},
53+
{provide: LocationStrategy, useClass: MockLocationStrategy}
5654
]);
5755
platformLocation = injector.get(PlatformLocation);
5856
document = injector.get(DOCUMENT);
@@ -68,18 +66,19 @@ describe('ScrollService', () => {
6866
});
6967

7068
it('should debounce `updateScrollPositonInHistory()`', fakeAsync(() => {
71-
const updateScrollPositionInHistorySpy = spyOn(scrollService, 'updateScrollPositionInHistory');
72-
73-
window.dispatchEvent(new Event('scroll'));
74-
tick(249);
75-
window.dispatchEvent(new Event('scroll'));
76-
tick(249);
77-
window.dispatchEvent(new Event('scroll'));
78-
tick(249);
79-
expect(updateScrollPositionInHistorySpy).not.toHaveBeenCalled();
80-
tick(1);
81-
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(1);
82-
}));
69+
const updateScrollPositionInHistorySpy =
70+
spyOn(scrollService, 'updateScrollPositionInHistory');
71+
72+
window.dispatchEvent(new Event('scroll'));
73+
tick(249);
74+
window.dispatchEvent(new Event('scroll'));
75+
tick(249);
76+
window.dispatchEvent(new Event('scroll'));
77+
tick(249);
78+
expect(updateScrollPositionInHistorySpy).not.toHaveBeenCalled();
79+
tick(1);
80+
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(1);
81+
}));
8382

8483
it('should set `scrollRestoration` to `manual` if supported', () => {
8584
if (scrollService.supportManualScrollRestoration) {
@@ -96,7 +95,9 @@ describe('ScrollService', () => {
9695
try {
9796
// Simulate `window.sessionStorage` being inaccessible, when cookies are disabled.
9897
Object.defineProperty(window, 'sessionStorage', {
99-
get() { throw new Error('The operation is insecure'); },
98+
get() {
99+
throw new Error('The operation is insecure');
100+
},
100101
});
101102

102103
const platformLoc = platformLocation as PlatformLocation;
@@ -198,8 +199,7 @@ describe('ScrollService', () => {
198199
platformLocation.hash = '';
199200

200201
const topOfPage = new MockElement();
201-
document.getElementById.and
202-
.callFake((id: string) => id === 'top-of-page' ? topOfPage : null);
202+
document.getElementById.and.callFake((id: string) => id === 'top-of-page' ? topOfPage : null);
203203

204204
scrollService.scroll();
205205
expect(topOfPage.scrollIntoView).toHaveBeenCalled();
@@ -227,7 +227,7 @@ describe('ScrollService', () => {
227227

228228
it('should scroll to the element whose id matches the hash with encoded characters', () => {
229229
const element = new MockElement();
230-
platformLocation.hash = '%F0%9F%91%8D'; // 👍
230+
platformLocation.hash = '%F0%9F%91%8D'; // 👍
231231
document.getElementById.and.returnValue(element);
232232

233233
scrollService.scroll();
@@ -289,8 +289,7 @@ describe('ScrollService', () => {
289289
it('should scroll to top', () => {
290290
const topOfPageElement = new MockElement() as any as Element;
291291
document.getElementById.and.callFake(
292-
(id: string) => id === 'top-of-page' ? topOfPageElement : null
293-
);
292+
(id: string) => id === 'top-of-page' ? topOfPageElement : null);
294293

295294
scrollService.scrollToTop();
296295
expect(topOfPageElement.scrollIntoView).toHaveBeenCalled();
@@ -312,58 +311,55 @@ describe('ScrollService', () => {
312311

313312
describe('#needToFixScrollPosition', async () => {
314313
it('should return true when popState event was fired after a back navigation if the browser supports ' +
315-
'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false', () => {
316-
317-
if (scrollService.supportManualScrollRestoration) {
318-
location.go('/initial-url1');
319-
// We simulate a scroll down
320-
location.replaceState('/initial-url1', 'hack', {scrollPosition: [2000, 0]});
321-
location.go('/initial-url2');
322-
location.back();
323-
324-
expect(scrollService.poppedStateScrollPosition).toEqual([2000, 0]);
325-
expect(scrollService.needToFixScrollPosition()).toBe(true);
326-
} else {
327-
location.go('/initial-url1');
328-
location.go('/initial-url2');
329-
location.back();
330-
331-
expect(scrollService.poppedStateScrollPosition).toBe(null);
332-
expect(scrollService.needToFixScrollPosition()).toBe(false);
333-
}
334-
335-
});
314+
'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false',
315+
() => {
316+
if (scrollService.supportManualScrollRestoration) {
317+
location.go('/initial-url1');
318+
// We simulate a scroll down
319+
location.replaceState('/initial-url1', 'hack', {scrollPosition: [2000, 0]});
320+
location.go('/initial-url2');
321+
location.back();
322+
323+
expect(scrollService.poppedStateScrollPosition).toEqual([2000, 0]);
324+
expect(scrollService.needToFixScrollPosition()).toBe(true);
325+
} else {
326+
location.go('/initial-url1');
327+
location.go('/initial-url2');
328+
location.back();
329+
330+
expect(scrollService.poppedStateScrollPosition).toBe(null);
331+
expect(scrollService.needToFixScrollPosition()).toBe(false);
332+
}
333+
});
336334

337335
it('should return true when popState event was fired after a forward navigation if the browser supports ' +
338-
'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false', () => {
339-
340-
if (scrollService.supportManualScrollRestoration) {
341-
location.go('/initial-url1');
342-
location.go('/initial-url2');
343-
// We simulate a scroll down
344-
location.replaceState('/initial-url1', 'hack', {scrollPosition: [2000, 0]});
345-
346-
location.back();
347-
scrollService.poppedStateScrollPosition = [0, 0];
348-
location.forward();
349-
350-
expect(scrollService.poppedStateScrollPosition).toEqual([2000, 0]);
351-
expect(scrollService.needToFixScrollPosition()).toBe(true);
352-
} else {
353-
location.go('/initial-url1');
354-
location.go('/initial-url2');
355-
location.back();
356-
location.forward();
357-
358-
expect(scrollService.poppedStateScrollPosition).toBe(null);
359-
expect(scrollService.needToFixScrollPosition()).toBe(false);
360-
}
361-
362-
});
336+
'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false',
337+
() => {
338+
if (scrollService.supportManualScrollRestoration) {
339+
location.go('/initial-url1');
340+
location.go('/initial-url2');
341+
// We simulate a scroll down
342+
location.replaceState('/initial-url1', 'hack', {scrollPosition: [2000, 0]});
343+
344+
location.back();
345+
scrollService.poppedStateScrollPosition = [0, 0];
346+
location.forward();
347+
348+
expect(scrollService.poppedStateScrollPosition).toEqual([2000, 0]);
349+
expect(scrollService.needToFixScrollPosition()).toBe(true);
350+
} else {
351+
location.go('/initial-url1');
352+
location.go('/initial-url2');
353+
location.back();
354+
location.forward();
355+
356+
expect(scrollService.poppedStateScrollPosition).toBe(null);
357+
expect(scrollService.needToFixScrollPosition()).toBe(false);
358+
}
359+
});
363360
});
364361

365362
describe('#scrollAfterRender', async () => {
366-
367363
let scrollSpy: jasmine.Spy;
368364
let scrollToTopSpy: jasmine.Spy;
369365
let needToFixScrollPositionSpy: jasmine.Spy;
@@ -383,69 +379,69 @@ describe('ScrollService', () => {
383379

384380

385381
it('should call `scroll` when we navigate to a location with anchor', fakeAsync(() => {
386-
needToFixScrollPositionSpy.and.returnValue(false);
387-
getStoredScrollPositionSpy.and.returnValue(null);
388-
isLocationWithHashSpy.and.returnValue(true);
382+
needToFixScrollPositionSpy.and.returnValue(false);
383+
getStoredScrollPositionSpy.and.returnValue(null);
384+
isLocationWithHashSpy.and.returnValue(true);
389385

390-
scrollService.scrollAfterRender(scrollDelay);
386+
scrollService.scrollAfterRender(scrollDelay);
391387

392-
expect(scrollSpy).not.toHaveBeenCalled();
393-
tick(scrollDelay);
394-
expect(scrollSpy).toHaveBeenCalled();
395-
}));
388+
expect(scrollSpy).not.toHaveBeenCalled();
389+
tick(scrollDelay);
390+
expect(scrollSpy).toHaveBeenCalled();
391+
}));
396392

397393
it('should call `scrollToTop` when we navigate to a location without anchor', fakeAsync(() => {
398-
needToFixScrollPositionSpy.and.returnValue(false);
399-
getStoredScrollPositionSpy.and.returnValue(null);
400-
isLocationWithHashSpy.and.returnValue(false);
394+
needToFixScrollPositionSpy.and.returnValue(false);
395+
getStoredScrollPositionSpy.and.returnValue(null);
396+
isLocationWithHashSpy.and.returnValue(false);
401397

402-
scrollService.scrollAfterRender(scrollDelay);
398+
scrollService.scrollAfterRender(scrollDelay);
403399

404-
expect(scrollToTopSpy).toHaveBeenCalled();
405-
tick(scrollDelay);
406-
expect(scrollSpy).not.toHaveBeenCalled();
407-
}));
400+
expect(scrollToTopSpy).toHaveBeenCalled();
401+
tick(scrollDelay);
402+
expect(scrollSpy).not.toHaveBeenCalled();
403+
}));
408404

409405
it('should call `viewportScroller.scrollToPosition` when we reload a page', fakeAsync(() => {
410-
getStoredScrollPositionSpy.and.returnValue([0, 1000]);
406+
getStoredScrollPositionSpy.and.returnValue([0, 1000]);
411407

412-
scrollService.scrollAfterRender(scrollDelay);
408+
scrollService.scrollAfterRender(scrollDelay);
413409

414-
expect(viewportScrollerStub.scrollToPosition).toHaveBeenCalled();
415-
expect(getStoredScrollPositionSpy).toHaveBeenCalled();
416-
}));
410+
expect(viewportScrollerStub.scrollToPosition).toHaveBeenCalled();
411+
expect(getStoredScrollPositionSpy).toHaveBeenCalled();
412+
}));
417413

418414
it('should call `scrollToPosition` after a popState', fakeAsync(() => {
419-
needToFixScrollPositionSpy.and.returnValue(true);
420-
getStoredScrollPositionSpy.and.returnValue(null);
421-
scrollService.scrollAfterRender(scrollDelay);
422-
expect(scrollToPosition).toHaveBeenCalled();
423-
tick(scrollDelay);
424-
expect(scrollSpy).not.toHaveBeenCalled();
425-
expect(scrollToTopSpy).not.toHaveBeenCalled();
426-
}));
415+
needToFixScrollPositionSpy.and.returnValue(true);
416+
getStoredScrollPositionSpy.and.returnValue(null);
417+
scrollService.scrollAfterRender(scrollDelay);
418+
expect(scrollToPosition).toHaveBeenCalled();
419+
tick(scrollDelay);
420+
expect(scrollSpy).not.toHaveBeenCalled();
421+
expect(scrollToTopSpy).not.toHaveBeenCalled();
422+
}));
427423
});
428424

429425
describe('once destroyed', () => {
430426
it('should stop updating scroll position', fakeAsync(() => {
431-
const updateScrollPositionInHistorySpy =
432-
spyOn(scrollService, 'updateScrollPositionInHistory');
427+
const updateScrollPositionInHistorySpy =
428+
spyOn(scrollService, 'updateScrollPositionInHistory');
433429

434-
window.dispatchEvent(new Event('scroll'));
435-
tick(250);
436-
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(1);
430+
window.dispatchEvent(new Event('scroll'));
431+
tick(250);
432+
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(1);
437433

438-
window.dispatchEvent(new Event('scroll'));
439-
tick(250);
440-
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(2);
434+
window.dispatchEvent(new Event('scroll'));
435+
tick(250);
436+
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(2);
441437

442-
updateScrollPositionInHistorySpy.calls.reset();
443-
scrollService.ngOnDestroy();
438+
updateScrollPositionInHistorySpy.calls.reset();
439+
scrollService.ngOnDestroy();
444440

445-
window.dispatchEvent(new Event('scroll'));
446-
tick(250);
447-
expect(updateScrollPositionInHistorySpy).not.toHaveBeenCalled();
448-
}));
441+
window.dispatchEvent(new Event('scroll'));
442+
tick(250);
443+
expect(updateScrollPositionInHistorySpy).not.toHaveBeenCalled();
444+
}));
449445

450446
it('should stop updating the stored location href', () => {
451447
const updateScrollLocationHrefSpy = spyOn(scrollService, 'updateScrollLocationHref');

0 commit comments

Comments
 (0)