Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dialog): Cancel open's rAF when close is called #4087

Merged
merged 5 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/mdc-dialog/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ class MDCDialogFoundation extends MDCFoundation {
this.close(strings.DESTROY_ACTION);
}

if (this.animationFrame_) {
cancelAnimationFrame(this.animationFrame_);
}

if (this.animationTimer_) {
clearTimeout(this.animationTimer_);
this.handleAnimationTimerEnd_();
Expand Down Expand Up @@ -153,6 +149,9 @@ class MDCDialogFoundation extends MDCFoundation {
this.adapter_.removeClass(cssClasses.OPEN);
this.adapter_.removeBodyClass(cssClasses.SCROLL_LOCK);

cancelAnimationFrame(this.animationFrame_);
this.animationFrame_ = 0;

clearTimeout(this.animationTimer_);
this.animationTimer_ = setTimeout(() => {
this.handleAnimationTimerEnd_();
Expand Down
7 changes: 6 additions & 1 deletion test/screenshot/spec/mdc-dialog/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {strings} from '../../../../packages/mdc-dialog/constants';

window.mdc.testFixture.fontsLoaded.then(() => {
class DialogFixture {
get dialog() {
return this.dialogInstance_;
}

/** @param {!HTMLElement} dialogEl */
initialize(dialogEl) {
/**
Expand Down Expand Up @@ -390,8 +394,9 @@ window.mdc.testFixture.fontsLoaded.then(() => {
}
}

[].forEach.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
mdc.testFixture.dialogs = [].map.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
const dialogFixture = new DialogFixture();
dialogFixture.initialize(dialogEl);
return dialogFixture.dialog;
});
});
23 changes: 20 additions & 3 deletions test/unit/mdc-dialog/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ test('#destroy cancels layout handling if called on same frame as layout', () =>
td.verify(mockAdapter.isContentScrollable(), {times: 0});
});

test('#open adds CSS classes', () => {
test('#open adds CSS classes after rAF', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();

foundation.open();
clock.runToFrame();
clock.tick(100);
td.verify(mockAdapter.addClass(cssClasses.OPEN), {times: 0});
td.verify(mockAdapter.addBodyClass(cssClasses.SCROLL_LOCK), {times: 0});

// Note: #open has exactly one rAF call, but for some reason it doesn't lapse without advancing the lolex clock twice.
clock.runToFrame();
clock.runToFrame();
td.verify(mockAdapter.addClass(cssClasses.OPEN));
td.verify(mockAdapter.addBodyClass(cssClasses.SCROLL_LOCK));
});
Expand All @@ -140,6 +143,20 @@ test('#close removes CSS classes', () => {
td.verify(mockAdapter.removeBodyClass(cssClasses.SCROLL_LOCK));
});

test('#close cancels rAF scheduled by open if still pending', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();

foundation.open();
td.reset();
foundation.close();

// Note: #open has exactly one rAF call, but for some reason it doesn't lapse without advancing the lolex clock twice.
clock.runToFrame();
clock.runToFrame();
td.verify(mockAdapter.addClass(cssClasses.OPEN), {times: 0});
});

test('#open adds the opening class to start an animation, and removes it after the animation is done', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();
Expand Down