Skip to content

fix cryptic error when md-input-container has no md-input #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/input/input-container-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export class MdInputContainerDuplicatedHintError extends MdError {
super(`A hint was already declared for 'align="${align}"'.`);
}
}


export class MdInputContainerMissingMdInputError extends MdError {
constructor() {
super('md-input-container must contain an md-input directive. Did you forget to add md-input ' +
'to the native input or textarea element?');
}
}
46 changes: 37 additions & 9 deletions src/lib/input/input-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {MdInputModule} from './input';
import {MdInputContainer} from './input-container';
import {MdPlatform} from '../core/platform/platform';
import {PlatformModule} from '../core/platform/index';
import {
MdInputContainerMissingMdInputError,
MdInputContainerPlaceholderConflictError,
MdInputContainerDuplicatedHintError
} from './input-container-errors';


describe('MdInputContainer', function () {
Expand Down Expand Up @@ -35,6 +40,7 @@ describe('MdInputContainer', function () {
MdInputContainerNumberTestController,
MdTextareaWithBindings,
MdInputContainerWithDisabled,
MdInputContainerMissingMdInputTestController
],
});

Expand Down Expand Up @@ -150,25 +156,29 @@ describe('MdInputContainer', function () {
it('validates there\'s only one hint label per side', () => {
let fixture = TestBed.createComponent(MdInputContainerInvalidHintTestController);

expect(() => fixture.detectChanges()).toThrow();
// TODO(jelbourn): .toThrow(new MdInputContainerDuplicatedHintError('start'));
// See https://github.com/angular/angular/issues/8348
expect(() => fixture.detectChanges()).toThrowError(
angularWrappedErrorMessage(new MdInputContainerDuplicatedHintError('start')));
});

it('validates there\'s only one hint label per side (attribute)', () => {
let fixture = TestBed.createComponent(MdInputContainerInvalidHint2TestController);

expect(() => fixture.detectChanges()).toThrow();
// TODO(jelbourn): .toThrow(new MdInputContainerDuplicatedHintError('start'));
// See https://github.com/angular/angular/issues/8348
expect(() => fixture.detectChanges()).toThrowError(
angularWrappedErrorMessage(new MdInputContainerDuplicatedHintError('start')));
});

it('validates there\'s only one placeholder', () => {
let fixture = TestBed.createComponent(MdInputContainerInvalidPlaceholderTestController);

expect(() => fixture.detectChanges()).toThrow();
// TODO(jelbourn): .toThrow(new MdInputContainerPlaceholderConflictError());
// See https://github.com/angular/angular/issues/8348
expect(() => fixture.detectChanges()).toThrowError(
angularWrappedErrorMessage(new MdInputContainerPlaceholderConflictError()));
});

it('validates that md-input child is present', () => {
let fixture = TestBed.createComponent(MdInputContainerMissingMdInputTestController);

expect(() => fixture.detectChanges()).toThrowError(
angularWrappedErrorMessage(new MdInputContainerMissingMdInputError()));
});

it('validates the type', () => {
Expand Down Expand Up @@ -406,3 +416,21 @@ class MdTextareaWithBindings {
cols: number = 8;
wrap: string = 'hard';
}

@Component({
template: `<md-input-container><input></md-input-container>`
})
class MdInputContainerMissingMdInputTestController {}

/**
* Gets a RegExp used to detect an angular wrapped error message.
* See https://github.com/angular/angular/issues/8348
*/
const angularWrappedErrorMessage = (e: Error) =>
new RegExp(`.*caused by: ${regexpEscape(e.message)}$`);

/**
* Escape a string for use inside a RegExp.
* Based on https://github.com/sindresorhus/escape-string-regex
*/
const regexpEscape = (s: string) => s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
7 changes: 6 additions & 1 deletion src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {getSupportedInputTypes} from '../core/platform/features';
import {
MdInputContainerUnsupportedTypeError,
MdInputContainerPlaceholderConflictError,
MdInputContainerDuplicatedHintError
MdInputContainerDuplicatedHintError,
MdInputContainerMissingMdInputError
} from './input-container-errors';


Expand Down Expand Up @@ -218,6 +219,10 @@ export class MdInputContainer implements AfterContentInit {
@ContentChildren(MdHint) _hintChildren: QueryList<MdHint>;

ngAfterContentInit() {
if (!this._mdInputChild) {
throw new MdInputContainerMissingMdInputError();
}

this._validateHints();
this._validatePlaceholders();

Expand Down