Skip to content

Commit

Permalink
test(select): Add Jasmine component tests. (#5565)
Browse files Browse the repository at this point in the history
* Project import generated by Copybara.

PiperOrigin-RevId: 293360388
  • Loading branch information
joyzhong authored Feb 5, 2020
1 parent 1eb2949 commit 4c7154b
Show file tree
Hide file tree
Showing 7 changed files with 1,483 additions and 1,364 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@
"test:sass": "jasmine --config=jasmine-node.json",
"test:dependency": "./scripts/dependency-test.sh",
"test:site": "npm run clean:site && ./scripts/site-generator-test.sh",
"test:unit": "npm run test:jasmineunit; jasmine_exit_code=$?; npm run test:mochaunit && exit \"$jasmine_exit_code\"",
"test:jasmineunit": "USE_JASMINE=true karma start --single-run",
"test:jasminewatch": "USE_JASMINE=true karma start --auto-watch",
"test:mochaunit": "karma start --single-run",
"test:mochawatch": "karma start --auto-watch",
"test:unit": "USE_JASMINE=true karma start --single-run",
"test:watch": "USE_JASMINE=true karma start --auto-watch",
"version": "cat lerna.json | grep -e '^ \"version\": ' | awk '{print $2}' | sed 's/[\",]//g'"
},
"devDependencies": {
Expand Down
91 changes: 91 additions & 0 deletions packages/mdc-select/helper-text/test/component.test.ts
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');
});
});
110 changes: 110 additions & 0 deletions packages/mdc-select/icon/test/component.test.ts
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());
});
});
Loading

0 comments on commit 4c7154b

Please sign in to comment.