Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit a9ae9c8

Browse files
committed
added initial tests for operators component
1 parent a912ee9 commit a9ae9c8

File tree

5 files changed

+110
-126
lines changed

5 files changed

+110
-126
lines changed
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
@import '../../operator-theme';
22

3-
:host {
4-
position: sticky;
5-
top: 0px;
6-
}
73
.operator-name {
84
font-size:30px;
95
}
@@ -17,8 +13,3 @@ mat-toolbar {
1713
color: rgba(255, 255, 255, 0.87);
1814
font-weight: normal;
1915
}
20-
21-
.operator-header {
22-
position: sticky;
23-
top: 0px;
24-
}

src/app/operators/directives/operator-scroll.directive.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/app/operators/operators.component.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { Component, OnInit, AfterViewInit, ChangeDetectionStrategy } from '@angular/core';
1+
import { Component, Inject, InjectionToken, OnInit, AfterViewInit, ChangeDetectionStrategy } from '@angular/core';
22
import { trigger, state, style, animate, transition } from '@angular/animations';
33
import { Router, ActivatedRoute } from '@angular/router';
44
import { BreakpointObserver } from '@angular/cdk/layout';
55
import { Subscription } from 'rxjs/Subscription';
66
import { Observable } from 'rxjs/Observable';
7-
import { ALL_OPERATORS } from '../../operator-docs';
87
import { OperatorDoc } from '../../operator-docs/operator.model';
98

109
const OPERATOR_MENU_GAP_LARGE = 64;
1110
const OPERATOR_MENU_GAP_SMALL = 54;
1211

12+
export const OPERATORS_TOKEN = new InjectionToken<string>('operators');
13+
14+
interface OperatorDocMap {
15+
[key: string]: OperatorDoc[];
16+
}
17+
1318
@Component({
1419
selector: 'app-operators',
1520
templateUrl: './operators.component.html',
@@ -34,19 +39,21 @@ const OPERATOR_MENU_GAP_SMALL = 54;
3439
]
3540
})
3641
export class OperatorsComponent implements OnInit, AfterViewInit {
37-
public operators = ALL_OPERATORS;
38-
public groupedOperators = groupOperatorsByType(ALL_OPERATORS);
39-
public categories = Object.keys(this.groupedOperators);
42+
public groupedOperators: OperatorDocMap;
43+
public categories: string[];
4044

4145
private _subscription: Subscription;
4246

4347
constructor(
4448
private _breakpointObserver: BreakpointObserver,
4549
private _router: Router,
46-
private _activatedRoute: ActivatedRoute
50+
private _activatedRoute: ActivatedRoute,
51+
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[]
4752
) { }
4853

4954
ngOnInit() {
55+
this.groupedOperators = groupOperatorsByType(this.operators);
56+
this.categories = Object.keys(this.groupedOperators);
5057
this._subscription = this._activatedRoute
5158
.fragment
5259
.subscribe(name => this.scrollToOperator(name));
@@ -92,8 +99,8 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
9299

93100
}
94101

95-
export function groupOperatorsByType(operators: OperatorDoc[]) {
96-
return operators.reduce((acc, curr) => {
102+
export function groupOperatorsByType(operators: OperatorDoc[]): OperatorDocMap {
103+
return operators.reduce((acc: OperatorDocMap, curr: OperatorDoc) => {
97104
if (acc[curr.operatorType]) {
98105
return { ...acc, [ curr.operatorType ] : [ ...acc[ curr.operatorType ], curr ] };
99106
}

src/app/operators/operators.module.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
1+
import { NgModule, InjectionToken, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { LayoutModule } from '@angular/cdk/layout';
44
import {
@@ -14,10 +14,10 @@ import {
1414
MatTooltipModule
1515
} from '@angular/material';
1616
import { ClipboardModule } from 'ngx-clipboard';
17+
import { ALL_OPERATORS, OperatorDoc } from '../../operator-docs';
1718

1819
import { OperatorsRoutingModule } from './operators.routing';
19-
20-
import { OperatorsComponent } from './operators.component';
20+
import { OperatorsComponent, OPERATORS_TOKEN } from './operators.component';
2121
import { OperatorComponent } from './components/operator/operator.component';
2222
import { OperatorHeaderComponent } from './components/operator-header/operator-header.component';
2323
import { OperatorParametersComponent } from './components/operator-parameters/operator-parameters.component';
@@ -27,10 +27,7 @@ import { OperatorExtrasComponent } from './components/operator-extras/operator-e
2727
import { AdditionalResourcesComponent } from './components/additional-resources/additional-resources.component';
2828
import { MarbleDiagramComponent } from './components/marble-diagram/marble-diagram.component';
2929
import { WalkthroughComponent } from './components/walkthrough/walkthrough.component';
30-
31-
import { OperatorScrollDirective } from './directives/operator-scroll.directive';
3230
import { HighlightJsDirective } from './directives/highlight-js.directive';
33-
3431
import { SafeUrlPipe } from './pipes/safe-url.pipe';
3532

3633
@NgModule({
@@ -45,7 +42,6 @@ import { SafeUrlPipe } from './pipes/safe-url.pipe';
4542
AdditionalResourcesComponent,
4643
WalkthroughComponent,
4744
MarbleDiagramComponent,
48-
OperatorScrollDirective,
4945
HighlightJsDirective,
5046
SafeUrlPipe
5147
],
@@ -64,6 +60,9 @@ import { SafeUrlPipe } from './pipes/safe-url.pipe';
6460
MatButtonModule,
6561
MatTooltipModule
6662
],
63+
providers: [
64+
{ provide: OPERATORS_TOKEN, useValue: ALL_OPERATORS }
65+
],
6766
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
6867
})
6968
export class OperatorsModule { }
Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,93 @@
1-
import { ALL_OPERATORS } from '../../../operator-docs';
2-
import { groupOperatorsByType } from '../operators.component';
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { RouterTestingModule } from '@angular/router/testing';
3+
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
4+
import { ActivatedRoute } from '@angular/router';
5+
import { LayoutModule, BreakpointObserver } from '@angular/cdk/layout';
6+
import { Observable } from 'rxjs/Observable';
7+
import { OperatorsComponent, OPERATORS_TOKEN, groupOperatorsByType } from '../operators.component';
8+
import { OperatorDoc } from '../../../operator-docs';
9+
10+
const mockActivatedRoute = {
11+
snapshot: {},
12+
fragment: Observable.create(observer => {
13+
observer.next('merge');
14+
observer.complete();
15+
})
16+
};
17+
18+
const mockOperators: OperatorDoc[] = [
19+
{ operatorType: 'transformation' },
20+
{ operatorType: 'utility' },
21+
{ operatorType: 'utility' }
22+
];
23+
24+
const mockBreakPointObserver = {
25+
isMatched: () => {}
26+
};
327

428
describe('Operators', () => {
5-
it('should group operators by operator type', () => {
6-
const result = groupOperatorsByType(ALL_OPERATORS);
7-
expect(Object.keys(result).length).toBe(8);
29+
describe('OperatorsComponent', () => {
30+
let fixture: ComponentFixture<OperatorsComponent>;
31+
let component: OperatorsComponent;
32+
let el;
33+
let breakpointService: BreakpointObserver;
34+
35+
beforeEach(() => {
36+
TestBed.configureTestingModule({
37+
imports: [ RouterTestingModule, LayoutModule ],
38+
declarations: [ OperatorsComponent ],
39+
providers: [
40+
{ provide: OPERATORS_TOKEN, useValue: mockOperators },
41+
{ provide: ActivatedRoute, useValue: mockActivatedRoute }
42+
],
43+
schemas: [ NO_ERRORS_SCHEMA ]
44+
});
45+
});
46+
47+
beforeEach(() => {
48+
fixture = TestBed.createComponent(OperatorsComponent);
49+
component = fixture.componentInstance;
50+
el = fixture.debugElement;
51+
breakpointService = el.injector.get(BreakpointObserver);
52+
});
53+
54+
it('should group operators by operator type', () => {
55+
component.ngOnInit();
56+
57+
expect(component.groupedOperators['transformation'].length).toBe(1);
58+
expect(component.groupedOperators['utility'].length).toBe(2);
59+
});
60+
61+
it('should scroll to initial operator when fragment exists', () => {
62+
spyOn(component, 'scrollToOperator').and.stub();
63+
component.ngOnInit();
64+
65+
expect(component.scrollToOperator).toHaveBeenCalledWith('merge');
66+
});
67+
68+
it('should have a sidenav mode of over when on a small screen', () => {
69+
spyOn(breakpointService, 'isMatched').and.returnValue(true);
70+
71+
expect(component.sidenavMode).toBe('over');
72+
});
73+
74+
it('should have a sidenav mode of side when on a large screen', () => {
75+
spyOn(breakpointService, 'isMatched').and.returnValue(false);
76+
77+
expect(component.sidenavMode).toBe('side');
78+
});
79+
80+
it('should have a top menu gap of 54px when on a small screen', () => {
81+
// small screen
82+
spyOn(breakpointService, 'isMatched').and.returnValue(true);
83+
84+
expect(component.operatorMenuGap).toBe(54);
85+
});
86+
87+
it('should have a top menu gap of 64px when on a large screen', () => {
88+
spyOn(breakpointService, 'isMatched').and.returnValue(false);
89+
90+
expect(component.operatorMenuGap).toBe(64);
91+
});
892
});
993
});

0 commit comments

Comments
 (0)