Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
feat(dialog): basic test spec
Browse files Browse the repository at this point in the history
 - re-export various dialog parts from dialog.ts to avoid needing to import a bunch of files to get the types you need for working with dialogs.
 - rename various parts _dialog_name.ts to indicate that they're internal. (???)
  • Loading branch information
justindujardin committed Dec 26, 2015
1 parent 159fa6d commit c8d52e8
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 14 deletions.
4 changes: 1 addition & 3 deletions examples/components/dialog/basic_usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {MATERIAL_DIRECTIVES,MdDialog} from 'ng2-material/all';
import {ElementRef} from "angular2/core";
import {Input} from "angular2/core";
import {DOM} from "angular2/src/platform/dom/dom_adapter";
import {MdDialogConfig} from "ng2-material/components/dialog/dialog_config";
import {MdDialogBasic} from "ng2-material/components/dialog/dialog_basic";
import {MdDialogRef} from "ng2-material/components/dialog/dialog_ref";
import {MdDialogConfig, MdDialogBasic, MdDialogRef} from "ng2-material/components/dialog/dialog";


function hasMedia(size: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import {NgIf} from "angular2/common";
import {MdButton} from "ng2-material/components/button/button";
import {MdButton} from "../button/button";
import {View} from "angular2/core";
import {Component} from "angular2/core";
import {IDialogComponent} from "./dialog";
import {MdDialogRef} from "ng2-material/components/dialog/dialog_ref";
import {MdDialogRef} from "./_dialog_ref";
import {Input} from "angular2/core";

@Component({selector: 'md-dialog-basic'})
@View({
template: `
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


import {MdDialogRef} from "./dialog_ref";
import {MdDialogRef} from "./_dialog_ref";

/** Configuration for a dialog to be opened. */
export class MdDialogConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ViewEncapsulation} from "angular2/core";
import {View} from "angular2/core";
import {Component} from "angular2/core";
import {ElementRef} from "angular2/core";
import {MdDialogRef} from "./dialog_ref";
import {MdDialogRef} from "./_dialog_ref";
import {KeyCodes} from "../../core/key_codes";
import {forwardRef} from "angular2/core";
import {Directive} from "angular2/core";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export class MdDialogRef {
/** Closes the dialog. This operation is asynchronous. */
close(result: any = null): Promise<void> {
return Animate.leave(this.containerRef.location.nativeElement, 'md-active').then(() => {
this._backdropRef.instance.hide();
if (this._backdropRef) {
this._backdropRef.instance.hide();
}
return this.contentRefDeferred.promise.then((_) => {
if (!this.isClosed) {
this.isClosed = true;
Expand Down
15 changes: 10 additions & 5 deletions ng2-material/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {

import {Promise} from 'angular2/src/facade/async';
import {isPresent, Type} from 'angular2/src/facade/lang';
import {MdDialogRef} from './dialog_ref';
import {MdDialogConfig} from './dialog_config';
import {MdDialogContainer} from './dialog_container';
import {MdDialogRef} from './_dialog_ref';
import {MdDialogConfig} from './_dialog_config';
import {MdDialogContainer} from './_dialog_container';
import {MdDialogBasic} from './_dialog_basic';
import {MdBackdrop} from "../backdrop/backdrop";
import {DOM} from "angular2/src/platform/dom/dom_adapter";
import {Renderer} from "angular2/core";
import {Animate} from '../../core/util/animate';

export * from './_dialog_config';
export * from './_dialog_container';
export * from './_dialog_ref';
export * from './_dialog_basic';

// TODO(jelbourn): body scrolling is disabled while dialog is open.
// TODO(jelbourn): Don't manually construct and configure a DOM element. See #1402
Expand Down Expand Up @@ -75,9 +80,9 @@ export class MdDialog {
// Create a DOM node to serve as a physical host element for the dialog.
var dialogElement = containerRef.location.nativeElement;

DOM.appendChild(config.container || this._defaultContainer, dialogElement);
this.renderer.setElementClass(containerRef.location, 'md-dialog-absolute', !!config.container);

this.renderer.setElementClass(containerRef.location, 'md-dialog-absolute', !!options.container);
DOM.appendChild(config.container || this._defaultContainer, dialogElement);

if (isPresent(config.width)) {
this.renderer.setElementStyle(containerRef.location, 'width', config.width);
Expand Down
168 changes: 168 additions & 0 deletions test/components/dialog/dialog_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import {
AsyncTestCompleter,
TestComponentBuilder,
beforeEach,
beforeEachProviders,
describe,
expect,
inject,
it,
} from 'angular2/testing_internal';
import {DebugElement} from 'angular2/src/core/debug/debug_element';
import {Component, View, provide} from 'angular2/core';
import {UrlResolver} from 'angular2/compiler';
import {TestUrlResolver} from '../../test_url_resolver';
import {MATERIAL_PROVIDERS} from '../../../ng2-material/all';
import {ComponentFixture} from "angular2/testing";
import {findChildByTag} from "../../util";
import {findChildById} from "../../util";
import {MdDialogRef, MdDialogConfig, MdDialog, MdDialogBasic} from "../../../ng2-material/components/dialog/dialog";
import {TimerWrapper} from "angular2/src/facade/async";
import {DOM} from "angular2/src/platform/dom/dom_adapter";


export function main() {

interface IDialogFixture {
fixture:ComponentFixture;
debug:DebugElement;
}

@Component({selector: 'test-app'})
@View({
directives: [MdDialogBasic],
template: `<div></div>`
})
class TestComponent {
}

describe('Dialog', () => {
let builder: TestComponentBuilder;
let dialog: MdDialog;

function setup(): Promise<IDialogFixture> {
return builder.createAsync(TestComponent)
.then((fixture: ComponentFixture) => {
fixture.detectChanges();
let debug = findChildByTag(fixture.debugElement, 'div');
return {
fixture: fixture,
debug: debug
};
})
.catch(console.error.bind(console));
}


beforeEachProviders(() => [
MATERIAL_PROVIDERS,
provide(UrlResolver, {useValue: new TestUrlResolver()}),
]);
beforeEach(inject([TestComponentBuilder, MdDialog], (tcb, mdDialog) => {
builder = tcb;
dialog = mdDialog;
}));

describe('MdDialog', () => {
describe('open', () => {
it('should resolve with a reference to the dialog component instance', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
let config = new MdDialogConfig();
dialog.open(MdDialogBasic, api.debug.elementRef, config)
.then((ref: MdDialogRef) => {
expect(ref.instance).toBeAnInstanceOf(MdDialogBasic);
ref.close().then(() => async.done());
});
});
}));
it('should initialize default config if not specified', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
dialog.open(MdDialogBasic, api.debug.elementRef)
.then((ref: MdDialogRef) => {
return ref.close();
})
.then(() => async.done());
});
}));
});
describe('close', () => {
it('should return a promise that resolves once the dialog is closed', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
dialog.open(MdDialogBasic, api.debug.elementRef)
.then((ref: MdDialogRef) => ref.close())
.then(() => async.done());
});
}));
it('should accept a value to resolve whenClosed with', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
dialog.open(MdDialogBasic, api.debug.elementRef)
.then((ref: MdDialogRef) => {
ref.whenClosed.then((result) => {
expect(result).toBe(1337);
async.done();
});
ref.close(1337);
});
});
}));
});
});

describe('MdDialogBasic', () => {
it('should open and close with promises', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
let config = new MdDialogConfig();
dialog.open(MdDialogBasic, api.debug.elementRef, config)
.then((ref: MdDialogRef) => {
ref.close().then(() => {
async.done();
});
});
});
}));
});
describe('MdDialogConfig', () => {
it('can set parent container', inject([AsyncTestCompleter], (async) => {
setup().then(() => {
let config = new MdDialogConfig().parent(DOM.query('body'));
expect(config.container).toBeAnInstanceOf(HTMLElement);
expect(config.container.tagName.toLowerCase()).toBe('body');
async.done();
});
}));
it('can set targetEvent to specify dialog origin point', inject([AsyncTestCompleter], (async) => {
setup().then(() => {
let ev = DOM.createMouseEvent('click');
let config = new MdDialogConfig().targetEvent(ev);
expect(config.sourceEvent).toBe(ev);
async.done();
});
}));
it('should bind content options to component instance', inject([AsyncTestCompleter], (async) => {
setup().then((api: IDialogFixture) => {
let config = new MdDialogConfig()
.textContent('Content')
.title('Title')
.ariaLabel('Aria')
.ok('Ok')
.cancel('Cancel')
.clickOutsideToClose(false);
dialog.open(MdDialogBasic, api.debug.elementRef, config)
.then((ref: MdDialogRef) => {
let instance: any = ref.instance;
expect(instance.textContent).toBe('Content');
expect(instance.title).toBe('Title');
expect(instance.ariaLabel).toBe('Aria');
expect(instance.ok).toBe('Ok');
expect(instance.cancel).toBe('Cancel');
async.done();
});
});
}));
});

});


}

0 comments on commit c8d52e8

Please sign in to comment.