Skip to content

Commit a4db5fb

Browse files
authored
test(form-field): Add Jasmine component unit tests (material-components#5462)
1 parent c38d84e commit a4db5fb

File tree

2 files changed

+158
-134
lines changed

2 files changed

+158
-134
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google Inc.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
import {MDCFormField} from '../../mdc-form-field/index';
25+
import {emitEvent} from '../../../testing/dom/events';
26+
27+
function getFixture() {
28+
const wrapper = document.createElement('div');
29+
wrapper.innerHTML = `
30+
<div class="mdc-form-field">
31+
<input type="radio" id="radio" checked name="radio">
32+
<label for="radio">Foo</label>
33+
</div>
34+
`;
35+
const el = wrapper.firstElementChild as HTMLElement;
36+
wrapper.removeChild(el);
37+
return el;
38+
}
39+
40+
function setupTest() {
41+
const root = getFixture();
42+
const component = new MDCFormField(root);
43+
return {root, component};
44+
}
45+
46+
describe('MDCFormField', () => {
47+
it('attachTo initializes and returns an MDCFormField instance', () => {
48+
expect(MDCFormField.attachTo(getFixture()) instanceof MDCFormField)
49+
.toBeTruthy();
50+
});
51+
52+
it('get/set input', () => {
53+
const {component} = setupTest();
54+
const input = {
55+
ripple: undefined,
56+
};
57+
58+
component.input = input;
59+
60+
expect(component.input === input).toBeTruthy();
61+
});
62+
63+
it('adapter#registerInteractionHandler adds an event listener to the label element',
64+
() => {
65+
const {root, component} = setupTest();
66+
const handler = jasmine.createSpy('eventHandler');
67+
const label = root.querySelector('label') as HTMLElement;
68+
69+
(component.getDefaultFoundation() as any)
70+
.adapter_.registerInteractionHandler('click', handler);
71+
emitEvent(label, 'click');
72+
73+
expect(handler).toHaveBeenCalledWith(jasmine.anything());
74+
});
75+
76+
it('adapter#deregisterInteractionHandler removes an event listener from the root element',
77+
() => {
78+
const {root, component} = setupTest();
79+
const handler = jasmine.createSpy('eventHandler');
80+
const label = root.querySelector('label') as HTMLElement;
81+
label.addEventListener('click', handler);
82+
83+
(component.getDefaultFoundation() as any)
84+
.adapter_.deregisterInteractionHandler('click', handler);
85+
emitEvent(label, 'click');
86+
87+
expect(handler).not.toHaveBeenCalledWith(jasmine.anything());
88+
});
89+
90+
it('adapter#activateInputRipple calls activate on the input ripple', () => {
91+
const {component} = setupTest();
92+
const ripple = {activate: jasmine.createSpy('activate')} as any;
93+
const input = {ripple};
94+
95+
component.input = input;
96+
(component.getDefaultFoundation() as any).adapter_.activateInputRipple();
97+
98+
expect(ripple.activate).toHaveBeenCalled();
99+
});
100+
101+
it('adapter#activateInputRipple does not throw if there is no input', () => {
102+
const {component} = setupTest();
103+
104+
expect(
105+
() => (component.getDefaultFoundation() as any)
106+
.adapter_.activateInputRipple)
107+
.not.toThrow();
108+
});
109+
110+
it('adapter#activateInputRipple does not throw if the input has no ripple getter',
111+
() => {
112+
const {component} = setupTest();
113+
const input = {ripple: undefined};
114+
115+
component.input = input;
116+
117+
expect(
118+
() => (component.getDefaultFoundation() as any)
119+
.adapter_.activateInputRipple)
120+
.not.toThrow();
121+
});
122+
123+
it('adapter#deactivateInputRipple calls deactivate on the input ripple',
124+
() => {
125+
const {component} = setupTest();
126+
const ripple = {deactivate: jasmine.createSpy('deactivate')} as any;
127+
const input = {ripple};
128+
129+
component.input = input;
130+
(component.getDefaultFoundation() as any)
131+
.adapter_.deactivateInputRipple();
132+
133+
expect(ripple.deactivate).toHaveBeenCalled();
134+
});
135+
136+
it('adapter#deactivateInputRipple does not throw if there is no input',
137+
() => {
138+
const {component} = setupTest();
139+
140+
expect(
141+
() => (component.getDefaultFoundation() as any)
142+
.adapter_.deactivateInputRipple)
143+
.not.toThrow();
144+
});
145+
146+
it('adapter#deactivateInputRipple does not throw if the input has no ripple getter',
147+
() => {
148+
const {component} = setupTest();
149+
const input = {ripple: undefined};
150+
151+
component.input = input;
152+
153+
expect(
154+
() => (component.getDefaultFoundation() as any)
155+
.adapter_.deactivateInputRipple)
156+
.not.toThrow();
157+
});
158+
});

test/unit/mdc-form-field/mdc-form-field.test.js

-134
This file was deleted.

0 commit comments

Comments
 (0)