-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(select): Add Jasmine component tests. (#5565)
* Project import generated by Copybara. PiperOrigin-RevId: 293360388
- Loading branch information
Showing
7 changed files
with
1,483 additions
and
1,364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import {getFixture as createFixture} from '../../../../testing/dom/index'; | ||
import {MDCSelectHelperText} from '../index'; | ||
|
||
const getFixture = () => createFixture(` | ||
<div class="mdc-select-helper-text"></div> | ||
`); | ||
|
||
describe('MDCSelectHelperText', () => { | ||
it('attachTo returns an MDCSelectHelperText instance', () => { | ||
expect( | ||
MDCSelectHelperText.attachTo(getFixture()) instanceof | ||
MDCSelectHelperText) | ||
.toBeTruthy(); | ||
}); | ||
|
||
function setupTest() { | ||
const root = getFixture(); | ||
const component = new MDCSelectHelperText(root); | ||
return {root, component}; | ||
} | ||
|
||
it('#adapter.addClass adds a class to the element', () => { | ||
const {root, component} = setupTest(); | ||
(component.getDefaultFoundation() as any).adapter_.addClass('foo'); | ||
expect(root.classList.contains('foo')).toBe(true); | ||
}); | ||
|
||
it('#adapter.removeClass removes a class from the element', () => { | ||
const {root, component} = setupTest(); | ||
root.classList.add('foo'); | ||
(component.getDefaultFoundation() as any).adapter_.removeClass('foo'); | ||
expect(root.classList.contains('foo')).toBe(false); | ||
}); | ||
|
||
it('#adapter.hasClass returns whether or not the element contains a certain class', | ||
() => { | ||
const {root, component} = setupTest(); | ||
root.classList.add('foo'); | ||
expect( | ||
(component.getDefaultFoundation() as any).adapter_.hasClass('foo')) | ||
.toBeTruthy(); | ||
root.classList.remove('foo'); | ||
expect( | ||
(component.getDefaultFoundation() as any).adapter_.hasClass('foo')) | ||
.toBeFalsy(); | ||
}); | ||
|
||
it('#adapter.setAttr adds a given attribute to the element', () => { | ||
const {root, component} = setupTest(); | ||
(component.getDefaultFoundation() as any) | ||
.adapter_.setAttr('aria-label', 'foo'); | ||
expect(root.getAttribute('aria-label')).toEqual('foo'); | ||
}); | ||
|
||
it('#adapter.removeAttr removes a given attribute from the element', () => { | ||
const {root, component} = setupTest(); | ||
root.setAttribute('aria-label', 'foo'); | ||
(component.getDefaultFoundation() as any) | ||
.adapter_.removeAttr('aria-label', 'foo'); | ||
expect(root.hasAttribute('aria-label')).toBeFalsy(); | ||
}); | ||
|
||
it('#adapter.setContent sets the text content of the element', () => { | ||
const {root, component} = setupTest(); | ||
(component.getDefaultFoundation() as any).adapter_.setContent('foo'); | ||
expect(root.textContent).toEqual('foo'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import {emitEvent} from '../../../../testing/dom/events'; | ||
import {getFixture as createFixture} from '../../../../testing/dom/index'; | ||
import {MDCSelectIcon, MDCSelectIconFoundation} from '../index'; | ||
|
||
const getFixture = () => createFixture(` | ||
<div class="mdc-select__icon"></div> | ||
`); | ||
|
||
describe('MDCSelectIcon', () => { | ||
it('attachTo returns an MDCSelectIcon instance', () => { | ||
expect(MDCSelectIcon.attachTo(getFixture()) instanceof MDCSelectIcon) | ||
.toBeTruthy(); | ||
}); | ||
|
||
function setupTest() { | ||
const root = getFixture(); | ||
const component = new MDCSelectIcon(root); | ||
return {root, component}; | ||
} | ||
|
||
it('#adapter.getAttr returns the value of a given attribute on the element', | ||
() => { | ||
const {root, component} = setupTest(); | ||
const expectedAttr = 'tabindex'; | ||
const expectedValue = '0'; | ||
root.setAttribute(expectedAttr, expectedValue); | ||
expect((component.getDefaultFoundation() as any) | ||
.adapter_.getAttr(expectedAttr)) | ||
.toEqual(expectedValue); | ||
}); | ||
|
||
it('#adapter.setAttr adds a given attribute to the element', () => { | ||
const {root, component} = setupTest(); | ||
(component.getDefaultFoundation() as any) | ||
.adapter_.setAttr('aria-label', 'foo'); | ||
expect(root.getAttribute('aria-label')).toEqual('foo'); | ||
}); | ||
|
||
it('#adapter.removeAttr removes a given attribute from the element', () => { | ||
const {root, component} = setupTest(); | ||
root.setAttribute('role', 'button'); | ||
(component.getDefaultFoundation() as any).adapter_.removeAttr('role'); | ||
expect(root.hasAttribute('role')).toBe(false); | ||
}); | ||
|
||
it('#adapter.setContent sets the text content of the element', () => { | ||
const {root, component} = setupTest(); | ||
(component.getDefaultFoundation() as any).adapter_.setContent('foo'); | ||
expect(root.textContent).toEqual('foo'); | ||
}); | ||
|
||
it('#adapter.registerInteractionHandler adds event listener for a given event to the element', | ||
() => { | ||
const {root, component} = setupTest(); | ||
const handler = jasmine.createSpy('keydown handler'); | ||
(component.getDefaultFoundation() as any) | ||
.adapter_.registerInteractionHandler('keydown', handler); | ||
emitEvent(root, 'keydown'); | ||
|
||
expect(handler).toHaveBeenCalledWith(jasmine.anything()); | ||
}); | ||
|
||
it('#adapter.deregisterInteractionHandler removes event listener for a given event from the element', | ||
() => { | ||
const {root, component} = setupTest(); | ||
const handler = jasmine.createSpy('keydown handler'); | ||
|
||
root.addEventListener('keydown', handler); | ||
(component.getDefaultFoundation() as any) | ||
.adapter_.deregisterInteractionHandler('keydown', handler); | ||
emitEvent(root, 'keydown'); | ||
|
||
expect(handler).not.toHaveBeenCalledWith(jasmine.anything()); | ||
}); | ||
|
||
it('#adapter.notifyIconAction emits ' + | ||
`${MDCSelectIconFoundation.strings.ICON_EVENT}`, | ||
() => { | ||
const {component} = setupTest(); | ||
const handler = jasmine.createSpy('handler'); | ||
|
||
component.listen(MDCSelectIconFoundation.strings.ICON_EVENT, handler); | ||
(component.getDefaultFoundation() as any).adapter_.notifyIconAction(); | ||
|
||
expect(handler).toHaveBeenCalledWith(jasmine.anything()); | ||
}); | ||
}); |
Oops, something went wrong.