describe('UserService Service', () => { beforeEachProviders(() => [API_PROVIDERS]); it('should contain persisted model methods', inject([RoomApi], (service: RoomApi) => { expect(service).toBeTruthy(); expect(service.create).toBeTruthy(); expect(service.updateAll).toBeTruthy(); expect(service.updateAttributes).toBeTruthy(); expect(service.find).toBeTruthy(); expect(service.findById).toBeTruthy(); expect(service.findOne).toBeTruthy(); }) ); let room: RoomInterface = new Room(); room.name = Date.now().toString(); it('should create a new room instance', injectAsync([RoomApi], (roomApi: RoomApi) => { return roomApi.create(room) .subscribe((room: RoomInterface) => expect(room.id).toBeTruthy()); }) ); it('should find room instance', injectAsync([RoomApi], (roomApi: RoomApi) => { return roomApi.findOne({ where: room }) .subscribe((room: RoomInterface) => expect(room.id).toBeTruthy()); }) ); });
describe(selector, () => { it('should instantiate component without fail', injectAsync([], () => { return setup() .then(() => promiseWait()); })); it('should destroy component without fail', injectAsync([], () => { return setup() .then((api: ComponentFixture<any>) => api.destroy()) .then(() => promiseWait()); })); });
describe('hide', () => { it('should hide sidenav by name', injectAsync([], () => { return setup() .then(() => service.hide('test')); })); it('should reject with invalid sidenav name', injectAsync([], () => { return setup() .then(() => service.hide('fake')) .catch(() => Promise.resolve()); })); });
describe('md-sidenav-container', () => { let template = ` <md-sidenav-container> <md-sidenav name="menu"></md-sidenav> <md-sidenav name="right" align="right"></md-sidenav> </md-sidenav-container>`; it('should be created and destroyed', injectAsync([], () => { return setup(template).then((api: ITestFixture) => { api.fixture.destroy(); }); })); it('should maintain a query list of its children', injectAsync([], () => { return setup(template).then((api: ITestFixture) => { expect(api.container.children.length).toBe(2); api.fixture.destroy(); }); })); it('should hide any open children when the backdrop is hidden', injectAsync([], () => { return setup(template).then((api: ITestFixture) => { let menu: MdSidenav = service.find('menu'); return menu.show().then(() => { return new Promise((resolve) => { expect(menu.visible).toBe(true); let sub = menu.onHidden.subscribe(() => { expect(menu.visible).toBe(false); sub.unsubscribe(); resolve(); }); menu.backdropRef.onClick(); }); }) .then(() => promiseWait()) .then(() => api.fixture.destroy()); }); })); it('should set isPushed if any child elements are side style', injectAsync([], () => { return setup(template).then((api: ITestFixture) => { expect(api.container.isPushed).toBe(false); let menu: MdSidenav = service.find('menu'); menu.style = SidenavStyle.SIDE; return menu.show() .then(() => { expect(api.container.isPushed).toBe(true); }) .then(() => promiseWait()) .then(() => api.fixture.destroy()); }); })); });
describe('TitleFormComponent', () => { it('should emit submit event with specified title value', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { return tcb.createAsync(TitleFormComponent) .then((fixture) => { // const uut: TitleFormComponent = fixture.componentInstance; // const el: HTMLElement = fixture.nativeElement; // spyOn(uut.onSubmit, 'emit'); // const input = <HTMLInputElement> el.querySelector('input'); // const btn = <HTMLButtonElement> el.querySelector('button'); // input.value = 'foo'; // uut['title'] = 'foo'; // btn.click(); // fixture.detectChanges(); // expect(uut.onSubmit.emit).toHaveBeenCalled(); }); })); });
describe('LoginService', () => { let testResponse = { success: true, message: "logged in" }; beforeEachProviders(() => { return [ HTTP_PROVIDERS, provide(XHRBackend, { useClass: MockBackend }), LoginService ]; }); it('should login', inject([XHRBackend, LoginService], (mockBackend, service) => { mockBackend.connections.subscribe( (connection: MockConnection) => { connection.mockRespond(new Response( new ResponseOptions({ body: testResponse } ))); }); let req = {}; req.username = "******"; req.password = "******"; service.login(req).subscribe((res: ServiceResponse) => { expect(res.success).toBe(true); expect(res.message).toBe("logged in"); }); })); it('should should login async', injectAsync([XHRBackend, LoginService], (mockBackend, service) => { return new Promise((pass, fail) => { mockBackend.connections.subscribe( (connection: MockConnection) => { connection.mockRespond(new Response( new ResponseOptions({ body: testResponse } ))); }); service.login().subscribe((res: ServiceResponse) => { expect(res.success).toBe(true); expect(res.message).toBe("logged in"); pass(); }); }); })); });
describe('MyApp', () => { beforeEachProviders(() => [ provide(NavParams, {useClass: MockNavParams}), provide(ViewController, {useClass: MockClass}), provide(Platform, {useClass: MockClass}), Book ]); beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { return tcb .createAsync(EditBookModal) .then((componentFixture: ComponentFixture<Type>) => { editBookModalFixture = componentFixture; editBookModal = componentFixture.componentInstance; editBookModalFixture.detectChanges(); }) .catch((err) => { console.error('ERROR - An error has occurred inside a promise! ' + err); // throw the error out to the console - http://stackoverflow.com/a/30741722 setTimeout(function(): void { throw err; }); }); })); it('initialises', () => { expect(editBookModal).not.toBeNull(); expect(editBookModalFixture).not.toBeNull(); }); });
describe('Page2', () => { beforeEachProviders(() => [ Form, provide(NavController, {useClass: MockClass}), provide(NavParams, {useClass: MockClass}), provide(Config, {useClass: MockClass}), provide(IonicApp, {useClass: MockClass}), provide(Platform, {useClass: MockClass}), ]); beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => { return tcb .createAsync(Page2) .then((componentFixture: ComponentFixture<Page2>) => { page2Fixture = componentFixture; page2 = componentFixture.componentInstance; page2Fixture.detectChanges(); }) .catch(Utils.promiseCatchHandler); })); it('initialises', () => { expect(page2).not.toBeNull(); expect(page2Fixture).not.toBeNull(); }); });
describe('Directive: MaterializeCollection', () => { let fixture:ComponentFixture<any>; beforeEachProviders(() => [TestComponentBuilder]); beforeEach(injectAsync([TestComponentBuilder], tcb => { return tcb .createAsync(Container) .then((f:ComponentFixture<any>) => { f.detectChanges(); fixture = f; }); })); it('Initialization should be ok', () => { expect(fixture).toBeDefined(); }); it('Click event should add active class', () => { let directive:MaterializeCollection = fixture.componentInstance.children.first; expect(directive).toBeDefined(); $(directive.el.nativeElement).click(); expect($(directive.el.nativeElement).find('li.active').length).toBe(0); }); });
describe('Component: Modal Content', () => { let builder: TestComponentBuilder; beforeEachProviders(() => [RioModalContent]); beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) { builder = tcb; })); it('should inject the component', inject([RioModalContent], (component: RioModalContent) => { expect(component).toBeTruthy(); })); it('should create the component', injectAsync([], () => { return builder.createAsync(RioModalContentTestController) .then((fixture: ComponentFixture<any>) => { fixture.autoDetectChanges(); let query = fixture.debugElement .query(By.directive(RioModalContent)); expect(query).toBeTruthy(); expect(query.componentInstance).toBeTruthy(); }); })); });