From 494d056c48f052d5e22c24cee7ef094e6ef079f0 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 28 Oct 2021 19:01:44 +0300 Subject: [PATCH] refactor(docs-infra): remove obsolete way of disabling `DocViewer` animations (#43977) Previously, there were two ways to disable view transition animations in `DocViewerComponent`: Using a `.no-animations` CSS class on the `` element and setting the static `DocViewerComponent.animationsEnabled` property to `false`. The latter was only used to disable animations for testing purposes. With the recent introduction of `AppComponent.reducedMotion`, there is now an easy way to disable animations in tests using the `.no-animations` class (which is applied by `AppComponent` based on the value of `AppComponent.reducedMotion`). Therefore, it is no longer necessary to have `DocViewerComponent.animationEnabled`. This commit removes the now obsolete `animationsEnalbed` property. PR Close #43977 --- aio/src/app/app.component.spec.ts | 6 +- .../doc-viewer/doc-viewer.component.spec.ts | 269 +++++++++--------- .../layout/doc-viewer/doc-viewer.component.ts | 6 +- 3 files changed, 136 insertions(+), 145 deletions(-) diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index de13b7015d6..f970c25f8ed 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -76,15 +76,17 @@ describe('AppComponent', () => { describe('with proper DocViewer', () => { + const originalReducedMotion = AppComponent.reducedMotion; beforeEach(async () => { - DocViewerComponent.animationsEnabled = false; + // Set `reducedMotion` to `true` to disable animations (such as view transitions) for the tests. + AppComponent.reducedMotion = true; createTestingModule('a/b'); await initializeTest(); }); - afterEach(() => DocViewerComponent.animationsEnabled = true); + afterEach(() => AppComponent.reducedMotion = originalReducedMotion); it('should create', () => { expect(component).toBeDefined(); diff --git a/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts b/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts index 21b71e3ef1c..5eaba0827b8 100644 --- a/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts +++ b/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts @@ -594,146 +594,139 @@ describe('DocViewerComponent', () => { }); [true, false].forEach(animationsEnabled => { - describe(`(animationsEnabled: ${animationsEnabled})`, () => { - beforeEach(() => DocViewerComponent.animationsEnabled = animationsEnabled); - afterEach(() => DocViewerComponent.animationsEnabled = true); + describe(`(animations enabled: ${animationsEnabled})`, () => { + beforeEach(() => docViewerEl.classList[animationsEnabled ? 'remove' : 'add'](NO_ANIMATIONS)); - [true, false].forEach(noAnimations => { - describe(`(.${NO_ANIMATIONS}: ${noAnimations})`, () => { - beforeEach(() => docViewerEl.classList[noAnimations ? 'add' : 'remove'](NO_ANIMATIONS)); - - it('should return an observable', done => { - docViewer.swapViews().subscribe(done, done.fail); - }); - - it('should swap the views', async () => { - await doSwapViews(); - - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - expect(docViewer.currViewContainer).toBe(oldNextViewContainer); - expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); - - await doSwapViews(); - - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); - expect(docViewer.currViewContainer).toBe(oldCurrViewContainer); - expect(docViewer.nextViewContainer).toBe(oldNextViewContainer); - }); - - it('should emit `docRemoved` after removing the leaving view', async () => { - const onDocRemovedSpy = jasmine.createSpy('onDocRemoved').and.callFake(() => { - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); - }); - - docViewer.docRemoved.subscribe(onDocRemovedSpy); - - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); - - await doSwapViews(); - - expect(onDocRemovedSpy).toHaveBeenCalledTimes(1); - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - }); - - it('should not emit `docRemoved` if the leaving view is already removed', async () => { - const onDocRemovedSpy = jasmine.createSpy('onDocRemoved'); - - docViewer.docRemoved.subscribe(onDocRemovedSpy); - docViewerEl.removeChild(oldCurrViewContainer); - - await doSwapViews(); - - expect(onDocRemovedSpy).not.toHaveBeenCalled(); - }); - - it('should emit `docInserted` after inserting the entering view', async () => { - const onDocInsertedSpy = jasmine.createSpy('onDocInserted').and.callFake(() => { - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - }); - - docViewer.docInserted.subscribe(onDocInsertedSpy); - - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); - - await doSwapViews(); - - expect(onDocInsertedSpy).toHaveBeenCalledTimes(1); - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - }); - - it('should call the callback after inserting the entering view', async () => { - const onInsertedCb = jasmine.createSpy('onInsertedCb').and.callFake(() => { - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - }); - const onDocInsertedSpy = jasmine.createSpy('onDocInserted'); - - docViewer.docInserted.subscribe(onDocInsertedSpy); - - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); - - await doSwapViews(onInsertedCb); - - expect(onInsertedCb).toHaveBeenCalledTimes(1); - expect(onInsertedCb).toHaveBeenCalledBefore(onDocInsertedSpy); - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - }); - - it('should empty the previous view', async () => { - await doSwapViews(); - - expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); - expect(docViewer.nextViewContainer.innerHTML).toBe(''); - - docViewer.nextViewContainer.innerHTML = 'Next view 2'; - await doSwapViews(); - - expect(docViewer.currViewContainer.innerHTML).toBe('Next view 2'); - expect(docViewer.nextViewContainer.innerHTML).toBe(''); - }); - - if (animationsEnabled && !noAnimations) { - // Only test this when there are animations. Without animations, the views are swapped - // synchronously, so there is no need (or way) to abort. - it('should abort swapping if the returned observable is unsubscribed from', async () => { - docViewer.swapViews().subscribe().unsubscribe(); - await doSwapViews(); - - // Since the first call was cancelled, only one swapping should have taken place. - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - expect(docViewer.currViewContainer).toBe(oldNextViewContainer); - expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); - expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); - expect(docViewer.nextViewContainer.innerHTML).toBe(''); - }); - } else { - it('should swap views synchronously when animations are disabled', () => { - const cbSpy = jasmine.createSpy('cb'); - - docViewer.swapViews(cbSpy).subscribe(); - - expect(cbSpy).toHaveBeenCalledTimes(1); - expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); - expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); - expect(docViewer.currViewContainer).toBe(oldNextViewContainer); - expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); - expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); - expect(docViewer.nextViewContainer.innerHTML).toBe(''); - }); - } - }); + it('should return an observable', done => { + docViewer.swapViews().subscribe(done, done.fail); }); + + it('should swap the views', async () => { + await doSwapViews(); + + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + expect(docViewer.currViewContainer).toBe(oldNextViewContainer); + expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); + + await doSwapViews(); + + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); + expect(docViewer.currViewContainer).toBe(oldCurrViewContainer); + expect(docViewer.nextViewContainer).toBe(oldNextViewContainer); + }); + + it('should emit `docRemoved` after removing the leaving view', async () => { + const onDocRemovedSpy = jasmine.createSpy('onDocRemoved').and.callFake(() => { + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); + }); + + docViewer.docRemoved.subscribe(onDocRemovedSpy); + + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); + + await doSwapViews(); + + expect(onDocRemovedSpy).toHaveBeenCalledTimes(1); + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + }); + + it('should not emit `docRemoved` if the leaving view is already removed', async () => { + const onDocRemovedSpy = jasmine.createSpy('onDocRemoved'); + + docViewer.docRemoved.subscribe(onDocRemovedSpy); + docViewerEl.removeChild(oldCurrViewContainer); + + await doSwapViews(); + + expect(onDocRemovedSpy).not.toHaveBeenCalled(); + }); + + it('should emit `docInserted` after inserting the entering view', async () => { + const onDocInsertedSpy = jasmine.createSpy('onDocInserted').and.callFake(() => { + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + }); + + docViewer.docInserted.subscribe(onDocInsertedSpy); + + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); + + await doSwapViews(); + + expect(onDocInsertedSpy).toHaveBeenCalledTimes(1); + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + }); + + it('should call the callback after inserting the entering view', async () => { + const onInsertedCb = jasmine.createSpy('onInsertedCb').and.callFake(() => { + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + }); + const onDocInsertedSpy = jasmine.createSpy('onDocInserted'); + + docViewer.docInserted.subscribe(onDocInsertedSpy); + + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(true); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(false); + + await doSwapViews(onInsertedCb); + + expect(onInsertedCb).toHaveBeenCalledTimes(1); + expect(onInsertedCb).toHaveBeenCalledBefore(onDocInsertedSpy); + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + }); + + it('should empty the previous view', async () => { + await doSwapViews(); + + expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); + expect(docViewer.nextViewContainer.innerHTML).toBe(''); + + docViewer.nextViewContainer.innerHTML = 'Next view 2'; + await doSwapViews(); + + expect(docViewer.currViewContainer.innerHTML).toBe('Next view 2'); + expect(docViewer.nextViewContainer.innerHTML).toBe(''); + }); + + if (animationsEnabled) { + // Only test this when there are animations. Without animations, the views are swapped + // synchronously, so there is no need (or way) to abort. + it('should abort swapping if the returned observable is unsubscribed from', async () => { + docViewer.swapViews().subscribe().unsubscribe(); + await doSwapViews(); + + // Since the first call was cancelled, only one swapping should have taken place. + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + expect(docViewer.currViewContainer).toBe(oldNextViewContainer); + expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); + expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); + expect(docViewer.nextViewContainer.innerHTML).toBe(''); + }); + } else { + it('should swap views synchronously when animations are disabled', () => { + const cbSpy = jasmine.createSpy('cb'); + + docViewer.swapViews(cbSpy).subscribe(); + + expect(cbSpy).toHaveBeenCalledTimes(1); + expect(docViewerEl.contains(oldCurrViewContainer)).toBe(false); + expect(docViewerEl.contains(oldNextViewContainer)).toBe(true); + expect(docViewer.currViewContainer).toBe(oldNextViewContainer); + expect(docViewer.nextViewContainer).toBe(oldCurrViewContainer); + expect(docViewer.currViewContainer.innerHTML).toBe('Next view'); + expect(docViewer.nextViewContainer.innerHTML).toBe(''); + }); + } }); }); }); diff --git a/aio/src/app/layout/doc-viewer/doc-viewer.component.ts b/aio/src/app/layout/doc-viewer/doc-viewer.component.ts index 6b03d81363d..9a805f7608a 100644 --- a/aio/src/app/layout/doc-viewer/doc-viewer.component.ts +++ b/aio/src/app/layout/doc-viewer/doc-viewer.component.ts @@ -26,9 +26,6 @@ const initialDocViewerContent = initialDocViewerElement ? fromInnerHTML(initialD // encapsulation: ViewEncapsulation.ShadowDom }) export class DocViewerComponent implements OnDestroy { - // Enable/Disable view transition animations. - static animationsEnabled = true; - private hostElement: HTMLElement; private void$ = of(undefined); @@ -220,8 +217,7 @@ export class DocViewerComponent implements OnDestroy { >; const animateProp = (elem: HTMLElement, prop: StringValueCSSStyleDeclaration, from: string, to: string, duration = 200) => { - const animationsDisabled = !DocViewerComponent.animationsEnabled - || this.hostElement.classList.contains(NO_ANIMATIONS); + const animationsDisabled = this.hostElement.classList.contains(NO_ANIMATIONS); elem.style.transition = ''; return animationsDisabled ? this.void$.pipe(tap(() => elem.style[prop] = to))