Skip to content

Commit

Permalink
fix(select): add tests for select label package (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matty Goo authored Feb 22, 2018
1 parent bc13750 commit b8ae66c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
3 changes: 0 additions & 3 deletions packages/mdc-select/label/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import MDCFoundation from '@material/base/foundation';
import MDCSelectLabelAdapter from './adapter';
import {cssClasses} from './constants';


/**
* @extends {MDCFoundation<!MDCSelectLabelAdapter>}
* @final
Expand Down Expand Up @@ -53,8 +52,6 @@ class MDCSelectLabelFoundation extends MDCFoundation {
/**
* Styles the label to float or defloat as necessary.
* @param {string} value The value of the input.
* @param {boolean} isFocused Whether the input is focused.
* @param {boolean} isBadInput The input's `validity.badInput` value.
*/
styleFloat(value) {
const {LABEL_FLOAT_ABOVE} = MDCSelectLabelFoundation.cssClasses;
Expand Down
4 changes: 2 additions & 2 deletions packages/mdc-select/label/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class MDCSelectLabel extends MDCComponent {
* @return {!MDCSelectLabelFoundation}
*/
getDefaultFoundation() {
return new MDCSelectLabelFoundation(/** @type {!MDCSelectLabelAdapter} */ (Object.assign({
return new MDCSelectLabelFoundation({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
})));
});
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/unit/mdc-select/mdc-select-label-foundation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {assert} from 'chai';
import td from 'testdouble';

import {setupFoundationTest} from '../helpers/setup';
import {verifyDefaultAdapter} from '../helpers/foundation';

import MDCSelectLabelFoundation from '../../../packages/mdc-select/label/foundation';
import {cssClasses} from '../../../packages/mdc-select/label/constants';

function setupTest() {
return setupFoundationTest(MDCSelectLabelFoundation);
}

suite('MDCSelectLabelFoundation');

test('exports cssClasses', () => {
assert.deepEqual(MDCSelectLabelFoundation.cssClasses, cssClasses);
});

test('default adapter returns a complete adapter implementation', () => {
verifyDefaultAdapter(MDCSelectLabelFoundation, [
'addClass', 'removeClass', 'getWidth',
]);
});

test('#styleFloat should add float above class', () => {
const {foundation, mockAdapter} = setupTest();
foundation.styleFloat('selectedValue');
td.verify(mockAdapter.addClass(cssClasses.LABEL_FLOAT_ABOVE));
});

test('#styleFloat should remove float above class', () => {
const {foundation, mockAdapter} = setupTest();
foundation.styleFloat(null);
td.verify(mockAdapter.removeClass(cssClasses.LABEL_FLOAT_ABOVE));
});
64 changes: 64 additions & 0 deletions test/unit/mdc-select/mdc-select-label.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {assert} from 'chai';
import bel from 'bel';
import td from 'testdouble';

import {MDCSelectLabel} from '../../../packages/mdc-select/label';

function getFixture() {
return bel`
<div class="mdc-select__label">Label</div>
`;
}

function setupTest() {
const fixture = getFixture();
const component = new MDCSelectLabel(fixture);

return {component, fixture};
}

suite('MDCSelectLabel');

test('attachTo returns a component instance', () => {
assert.isTrue(MDCSelectLabel.attachTo(getFixture()) instanceof MDCSelectLabel);
});

test('#float should call styleFloat on foundation', () => {
const {component} = setupTest();
const value = 'value';
component.foundation_.styleFloat = td.func();
component.float(value);
td.verify(component.foundation_.styleFloat(value));
});

test('adapter#addClass adds a class to the root element', () => {
const {component, fixture} = setupTest();

component.getDefaultFoundation().adapter_.addClass('foo');
assert.isTrue(fixture.classList.contains('foo'));
});


test('adapter#removeClass removes a class from the root element', () => {
const {component, fixture} = setupTest();

fixture.classList.add('foo');
component.getDefaultFoundation().adapter_.removeClass('foo');
assert.isFalse(fixture.classList.contains('foo'));
});

0 comments on commit b8ae66c

Please sign in to comment.