-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(checkbox): remove register/deregisterEventlisteners from foundation #3402
Changes from 5 commits
1a0b854
bc2f93e
4dd7085
65957b4
b148d92
b440ea1
224ac43
0acae16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,17 @@ class MDCCheckbox extends MDCComponent { | |
|
||
/** @private {!MDCRipple} */ | ||
this.ripple_ = this.initRipple_(); | ||
/** @private {!Function} */ | ||
this.handleChange_; | ||
/** @private {!Function} */ | ||
this.handleAnimationEnd_; | ||
} | ||
|
||
initialSyncWithDOM() { | ||
this.handleChange_ = () => this.foundation_.handleChange(); | ||
this.handleAnimationEnd_= () => this.foundation_.handleAnimationEnd(); | ||
this.nativeCb_.addEventListener('change', this.handleChange_); | ||
this.root_.addEventListener(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to |
||
} | ||
|
||
/** | ||
|
@@ -81,11 +92,6 @@ class MDCCheckbox extends MDCComponent { | |
removeClass: (className) => this.root_.classList.remove(className), | ||
setNativeControlAttr: (attr, value) => this.nativeCb_.setAttribute(attr, value), | ||
removeNativeControlAttr: (attr) => this.nativeCb_.removeAttribute(attr), | ||
registerAnimationEndHandler: | ||
(handler) => this.root_.addEventListener(getCorrectEventName(window, 'animationend'), handler), | ||
deregisterAnimationEndHandler: | ||
(handler) => this.root_.removeEventListener(getCorrectEventName(window, 'animationend'), handler), | ||
registerChangeHandler: (handler) => this.nativeCb_.addEventListener('change', handler), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to remove |
||
deregisterChangeHandler: (handler) => this.nativeCb_.removeEventListener('change', handler), | ||
getNativeControl: () => this.nativeCb_, | ||
forceLayout: () => this.root_.offsetWidth, | ||
|
@@ -140,6 +146,8 @@ class MDCCheckbox extends MDCComponent { | |
|
||
destroy() { | ||
this.ripple_.destroy(); | ||
this.nativeCb_.removeEventListener('change', this.handleChange_); | ||
this.unlisten(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_); | ||
super.destroy(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ import {createMockRaf} from '../helpers/raf'; | |
import {MDCCheckbox} from '../../../packages/mdc-checkbox'; | ||
import {MDCRipple} from '../../../packages/mdc-ripple'; | ||
import {strings} from '../../../packages/mdc-checkbox/constants'; | ||
import {getCorrectEventName} from '../../../packages/mdc-animation'; | ||
import {getMatchesProperty} from '../../../packages/mdc-ripple/util'; | ||
|
||
function getFixture() { | ||
|
@@ -139,6 +138,36 @@ test('get ripple returns a MDCRipple instance', () => { | |
assert.isOk(component.ripple instanceof MDCRipple); | ||
}); | ||
|
||
test('checkbox change event calls #foundation.handleChange', () => { | ||
const {cb, component} = setupTest(); | ||
component.foundation_.handleChange = td.func(); | ||
domEvents.emit(cb, 'change'); | ||
td.verify(component.foundation_.handleChange(), {times: 1}); | ||
}); | ||
|
||
test('root animationend event calls #foundation.handleAnimationEnd', () => { | ||
const {root, component} = setupTest(); | ||
component.foundation_.handleAnimationEnd = td.func(); | ||
domEvents.emit(root, 'animationend'); | ||
td.verify(component.foundation_.handleAnimationEnd(), {times: 1}); | ||
}); | ||
|
||
test('checkbox change event is destroyed on #destroy', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. event handler is destroyed |
||
const {cb, component} = setupTest(); | ||
component.foundation_.handleChange = td.func(); | ||
component.destroy(); | ||
domEvents.emit(cb, 'change'); | ||
td.verify(component.foundation_.handleChange(), {times: 0}); | ||
}); | ||
|
||
test('root animationend event is destroyed on #destroy', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. event handler is destroyed |
||
const {root, component} = setupTest(); | ||
component.foundation_.handleAnimationEnd = td.func(); | ||
component.destroy(); | ||
domEvents.emit(root, 'animationend'); | ||
td.verify(component.foundation_.handleAnimationEnd(), {times: 0}); | ||
}); | ||
|
||
test('adapter#addClass adds a class to the root element', () => { | ||
const {root, component} = setupTest(); | ||
component.getDefaultFoundation().adapter_.addClass('foo'); | ||
|
@@ -165,50 +194,6 @@ test('adapter#removeNativeControlAttr removes an attribute from the input elemen | |
assert.isFalse(cb.hasAttribute('aria-checked')); | ||
}); | ||
|
||
test('adapter#registerAnimationEndHandler adds an animation end event listener on the root element', () => { | ||
const {root, component} = setupTest(); | ||
const handler = td.func('animationEndHandler'); | ||
component.getDefaultFoundation().adapter_.registerAnimationEndHandler(handler); | ||
domEvents.emit(root, getCorrectEventName(window, 'animationend')); | ||
|
||
td.verify(handler(td.matchers.anything())); | ||
}); | ||
|
||
test('adapter#deregisterAnimationEndHandler removes an animation end event listener on the root el', () => { | ||
const {root, component} = setupTest(); | ||
const handler = td.func('animationEndHandler'); | ||
const animEndEvtName = getCorrectEventName(window, 'animationend'); | ||
root.addEventListener(animEndEvtName, handler); | ||
|
||
component.getDefaultFoundation().adapter_.deregisterAnimationEndHandler(handler); | ||
domEvents.emit(root, animEndEvtName); | ||
|
||
td.verify(handler(td.matchers.anything()), {times: 0}); | ||
}); | ||
|
||
test('adapter#registerChangeHandler adds a change event listener to the native checkbox element', () => { | ||
const {root, component} = setupTest(); | ||
const nativeCb = root.querySelector(strings.NATIVE_CONTROL_SELECTOR); | ||
const handler = td.func('changeHandler'); | ||
|
||
component.getDefaultFoundation().adapter_.registerChangeHandler(handler); | ||
domEvents.emit(nativeCb, 'change'); | ||
|
||
td.verify(handler(td.matchers.anything())); | ||
}); | ||
|
||
test('adapter#deregisterChangeHandler adds a change event listener to the native checkbox element', () => { | ||
const {root, component} = setupTest(); | ||
const nativeCb = root.querySelector(strings.NATIVE_CONTROL_SELECTOR); | ||
const handler = td.func('changeHandler'); | ||
nativeCb.addEventListener('change', handler); | ||
|
||
component.getDefaultFoundation().adapter_.deregisterChangeHandler(handler); | ||
domEvents.emit(nativeCb, 'change'); | ||
|
||
td.verify(handler(td.matchers.anything()), {times: 0}); | ||
}); | ||
|
||
test('adapter#getNativeControl returns the native checkbox element', () => { | ||
const {root, component} = setupTest(); | ||
const nativeCb = root.querySelector(strings.NATIVE_CONTROL_SELECTOR); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Capitalize
The