From 1513f2163722ee3fea6ee4bd7c5ca6df0e7ab150 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 18 Dec 2015 10:36:01 +0200 Subject: [PATCH] ApiInfo component spec --- lib/components/ApiInfo/api-info.spec.js | 61 +++++++++++++++++++++++++ tests/helpers.js | 11 +++++ tests/schemas/api-info-test.json | 20 ++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/components/ApiInfo/api-info.spec.js create mode 100644 tests/helpers.js create mode 100644 tests/schemas/api-info-test.json diff --git a/lib/components/ApiInfo/api-info.spec.js b/lib/components/ApiInfo/api-info.spec.js new file mode 100644 index 00000000..7ee3f4b3 --- /dev/null +++ b/lib/components/ApiInfo/api-info.spec.js @@ -0,0 +1,61 @@ +'use strict'; + +import { getChildDebugElement } from 'tests/helpers'; +import {Component, View, provide} from 'angular2/core'; + +import { + TestComponentBuilder, + injectAsync, + beforeEach, + beforeEachProviders, + it +} from 'angular2/testing'; + +import ApiInfo from 'lib/components/ApiInfo/api-info'; +import SchemaManager from 'lib/utils/SchemaManager'; + + +describe('ApiInfo Component', () => { + let builder; + beforeEachProviders(() => [ + provide(SchemaManager, {useValue: new SchemaManager()}) + ]); + beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => { + builder = tcb; + return schemaMgr.load('/tests/schemas/api-info-test.json').then(() => null, (err) => { throw err; }); + })); + + + it('shold init component data', (done) => { + builder.createAsync(TestApp).then(fixture => { + let apiInfoComponent = getChildDebugElement(fixture.debugElement, 'api-info').componentInstance; + expect(apiInfoComponent).not.toBeNull(); + fixture.detectChanges(); + expect(apiInfoComponent.data).not.toBeNull(); + apiInfoComponent.data.title.should.be.equal('Swagger Petstore'); + done(); + }, err => done.fail(err)); + }); + + it('shold render api name and version', (done) => { + builder.createAsync(TestApp).then(fixture => { + let nativeElement = getChildDebugElement(fixture.debugElement, 'api-info').nativeElement; + let headerElement = nativeElement.querySelector('h1'); + fixture.detectChanges(); + expect(headerElement).toHaveText('Swagger Petstore (1.0.0)'); + done(); + }, err => done.fail(err)); + }); +}); + + +/** Test component that contains an MdButton. */ +@Component({selector: 'test-app'}) +@View({ + directives: [ApiInfo], + providers: [SchemaManager], + template: + `` +}) +class TestApp { +} diff --git a/tests/helpers.js b/tests/helpers.js new file mode 100644 index 00000000..0c5bb32e --- /dev/null +++ b/tests/helpers.js @@ -0,0 +1,11 @@ +'use strict'; + +import {BrowserDomAdapter} from 'angular2/platform/browser'; +BrowserDomAdapter.makeCurrent(); + +/** Gets a child DebugElement by tag name. */ +export function getChildDebugElement(parent, tagName) { + return parent.query(debugEl => { + return debugEl.nativeElement.tagName && debugEl.nativeElement.tagName.toLowerCase() === tagName; + }); +} diff --git a/tests/schemas/api-info-test.json b/tests/schemas/api-info-test.json new file mode 100644 index 00000000..1d1cb7df --- /dev/null +++ b/tests/schemas/api-info-test.json @@ -0,0 +1,20 @@ +{ + "swagger": "2.0", + "info": { + "description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.", + "version": "1.0.0", + "title": "Swagger Petstore", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "email": "apiteam@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "host": "petstore.swagger.io", + "basePath": "/v2/", + "schemes": ["http"], + "paths": {} +}