Commit 7e3d3b376034d310fa00a46990dd5189754c2441

Authored by Victor Costa
1 parent d316e303

Add tests for link list component

src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder';
  2 +import {Pipe, Input, provide, Component} from 'ng-forward';
  3 +
  4 +import {LinkListBlock} from './link-list.component';
  5 +
  6 +const tcb = new TestComponentBuilder();
  7 +
  8 +const htmlTemplate: string = '<noosfero-link-list-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-link-list-block>';
  9 +
  10 +
  11 +describe("Link List Block Component", () => {
  12 +
  13 + beforeEach(angular.mock.module("templates"));
  14 +
  15 + it("receives the block and the owner as inputs", done => {
  16 +
  17 + // Creating a container component (BlockContainerComponent) to include
  18 + // the component under test (Block)
  19 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] })
  20 + class BlockContainerComponent {
  21 + block = { type: 'Block' };
  22 + owner = { name: 'profile-name' };
  23 + constructor() {
  24 + }
  25 + }
  26 +
  27 + // uses the TestComponentBuilder instance to initialize the component
  28 + //.overrideView(LinkListBlock, { template: 'asdasdasd', pipes: [NoosferoTemplate] })
  29 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  30 + // and here we can inspect and run the test assertions
  31 + let myComponent: LinkListBlock = fixture.componentInstance;
  32 +
  33 + // assure the block object inside the Block matches
  34 + // the provided through the parent component
  35 + expect(myComponent.block.type).toEqual("Block");
  36 + expect(myComponent.owner.name).toEqual("profile-name");
  37 + done();
  38 + });
  39 + });
  40 +
  41 +
  42 + it("display links stored in block settings", done => {
  43 +
  44 + @Pipe('noosferoTemplateFilter')
  45 + class NoosferoTemplateFilter {
  46 + transform(input: any, changeTo: any) {
  47 + return input;
  48 + }
  49 + }
  50 +
  51 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] })
  52 + class CustomBlockType {
  53 + block: any = { settings: { links: [{ name: 'link1', address: 'address1' }, { name: 'link2', address: 'address2' }] } };
  54 + owner: any = { name: 'profile-name' };
  55 + constructor() {
  56 + }
  57 + }
  58 + tcb.overrideView(LinkListBlock, { templateUrl: "app/components/noosfero-blocks/link-list/link-list.html", pipes: [NoosferoTemplateFilter] }).createAsync(CustomBlockType).then(fixture => {
  59 + expect(fixture.debugElement.queryAll(".link-list-block a").length).toEqual(2);
  60 + done();
  61 + });
  62 + });
  63 +
  64 +});
... ...
src/app/components/noosfero-blocks/link-list/link-list.component.ts
... ... @@ -12,7 +12,9 @@ export class LinkListBlock {
12 12 links: any;
13 13  
14 14 ngOnInit() {
15   - this.links = this.block.settings.links;
  15 + if (this.block && this.block.settings) {
  16 + this.links = this.block.settings.links;
  17 + }
16 18 }
17 19  
18 20 }
... ...