1
+ import { Component , CUSTOM_ELEMENTS_SCHEMA , Input } from '@angular/core' ;
1
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2
-
3
+ import { By } from '@angular/platform-browser' ;
4
+ import { PlotData } from '@npl/interfaces' ;
5
+ import { Subject } from 'rxjs' ;
6
+ import { PlotsService } from '../../services/plots.service' ;
3
7
import { PlotsComponent } from './plots.component' ;
4
8
9
+ const PLOTS : PlotData [ ] = [
10
+ {
11
+ id : 1 ,
12
+ data : [ { x : [ 1 , 2 , 3 ] , y : [ 2 , 3 , 4 ] } ] ,
13
+ layout : {
14
+ title : 'Test Plot 1' ,
15
+ } ,
16
+ } ,
17
+ {
18
+ id : 2 ,
19
+ data : [ { x : [ 1 , 2 , 3 ] , y : [ 2 , 3 , 4 ] } ] ,
20
+ layout : {
21
+ title : 'Test Plot 2' ,
22
+ } ,
23
+ } ,
24
+ ] ;
25
+
26
+ @Component ( {
27
+ selector : 'npl-plot' ,
28
+ template : '' ,
29
+ } )
30
+ export class PlotComponent {
31
+ @Input ( ) plotData ! : PlotData ;
32
+ }
33
+
5
34
describe ( 'PlotsComponent' , ( ) => {
6
35
let component : PlotsComponent ;
7
36
let fixture : ComponentFixture < PlotsComponent > ;
37
+ let plotsServiceMock : PlotsService ;
38
+ let plots$ : Subject < PlotData [ ] > ;
8
39
9
40
beforeEach ( async ( ) => {
41
+ plots$ = new Subject ( ) ;
42
+ plotsServiceMock = {
43
+ plots$,
44
+ } as unknown as PlotsService ;
45
+
10
46
await TestBed . configureTestingModule ( {
11
- declarations : [ PlotsComponent ] ,
47
+ declarations : [ PlotsComponent , PlotComponent ] ,
48
+ providers : [ { provide : PlotsService , useValue : plotsServiceMock } ] ,
49
+ schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
12
50
} ) . compileComponents ( ) ;
13
51
} ) ;
14
52
@@ -21,4 +59,28 @@ describe('PlotsComponent', () => {
21
59
it ( 'should create' , ( ) => {
22
60
expect ( component ) . toBeTruthy ( ) ;
23
61
} ) ;
62
+
63
+ it ( 'should render a empty list if there are no emitted values' , ( ) => {
64
+ const plots = fixture . debugElement . queryAll ( By . directive ( PlotComponent ) ) ;
65
+
66
+ expect ( plots . length ) . toBe ( 0 ) ;
67
+ } ) ;
68
+
69
+ it ( 'should render a plot if there is one element' , ( ) => {
70
+ plots$ . next ( [ PLOTS [ 0 ] ] ) ;
71
+
72
+ fixture . detectChanges ( ) ;
73
+
74
+ const plots = fixture . debugElement . queryAll ( By . directive ( PlotComponent ) ) ;
75
+ expect ( plots . length ) . toBe ( 1 ) ;
76
+ } ) ;
77
+
78
+ it ( 'should render several plots' , ( ) => {
79
+ plots$ . next ( PLOTS ) ;
80
+
81
+ fixture . detectChanges ( ) ;
82
+
83
+ const plots = fixture . debugElement . queryAll ( By . directive ( PlotComponent ) ) ;
84
+ expect ( plots . length ) . toBe ( 2 ) ;
85
+ } ) ;
24
86
} ) ;
0 commit comments