test(input): add unit tests for isTextInput()

This commit is contained in:
Adam Bradley
2016-11-21 08:42:04 -06:00
parent fbe4a7b346
commit 6e7587dea4
2 changed files with 162 additions and 2 deletions

View File

@@ -83,7 +83,7 @@ export function zoneRafFrames(framesToWait: number, callback: Function) {
}
}
export let CSS: {
export const CSS: {
transform?: string,
transition?: string,
transitionDuration?: string,
@@ -255,7 +255,7 @@ export function isTextInput(ele: any) {
}
export function hasFocusedTextInput() {
let ele = <HTMLElement>document.activeElement;
const ele = <HTMLElement>document.activeElement;
if (isTextInput(ele)) {
return (ele.parentElement.querySelector(':focus') === ele);
}

160
src/util/test/dom.spec.ts Normal file
View File

@@ -0,0 +1,160 @@
import * as dom from '../dom';
describe('dom', () => {
describe('isTextInput', () => {
it('should return false if input[type=button]', () => {
const ele = document.createElement('input');
ele.type = 'button';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=checkbox]', () => {
const ele = document.createElement('input');
ele.type = 'checkbox';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=color]', () => {
const ele = document.createElement('input');
ele.type = 'color';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=file]', () => {
const ele = document.createElement('input');
ele.type = 'file';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=image]', () => {
const ele = document.createElement('input');
ele.type = 'image';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=radio]', () => {
const ele = document.createElement('input');
ele.type = 'radio';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=range]', () => {
const ele = document.createElement('input');
ele.type = 'range';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=reset]', () => {
const ele = document.createElement('input');
ele.type = 'reset';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if input[type=submit]', () => {
const ele = document.createElement('input');
ele.type = 'submit';
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return true if input date', () => {
const ele = document.createElement('input');
ele.type = 'date';
expect(dom.isTextInput(ele)).toBe(true);
ele.type = 'datetime';
expect(dom.isTextInput(ele)).toBe(true);
ele.type = 'datetime-local';
expect(dom.isTextInput(ele)).toBe(true);
ele.type = 'month';
expect(dom.isTextInput(ele)).toBe(true);
ele.type = 'time';
expect(dom.isTextInput(ele)).toBe(true);
ele.type = 'week';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=email]', () => {
const ele = document.createElement('input');
ele.type = 'email';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=number]', () => {
const ele = document.createElement('input');
ele.type = 'number';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=number]', () => {
const ele = document.createElement('input');
ele.type = 'number';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=password]', () => {
const ele = document.createElement('input');
ele.type = 'password';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=search]', () => {
const ele = document.createElement('input');
ele.type = 'search';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=tel]', () => {
const ele = document.createElement('input');
ele.type = 'tel';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=url]', () => {
const ele = document.createElement('input');
ele.type = 'url';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input[type=text]', () => {
const ele = document.createElement('input');
ele.type = 'text';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if input and unknown type', () => {
const ele = document.createElement('input');
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if contentEditable', () => {
const ele = document.createElement('div');
ele.contentEditable = 'true';
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return true if textarea', () => {
const ele = document.createElement('textarea');
expect(dom.isTextInput(ele)).toBe(true);
});
it('should return false if a div', () => {
const ele = document.createElement('div');
expect(dom.isTextInput(ele)).toBe(false);
});
it('should return false if blank', () => {
expect(dom.isTextInput(null)).toBe(false);
expect(dom.isTextInput(undefined)).toBe(false);
expect(dom.isTextInput(false)).toBe(false);
});
});
});