Skip to content

Commit 4c0be1a

Browse files
committed
tests(web): add tests for plots and app component
1 parent 0e1f56d commit 4c0be1a

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { SocketService } from '../../services/socket.service';
4+
import { AppComponent } from './app.component';
5+
6+
describe('AppComponent', () => {
7+
let fixture: ComponentFixture<AppComponent>;
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [AppComponent],
12+
imports: [],
13+
providers: [{ provide: SocketService, useValue: {} }],
14+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
15+
}).compileComponents();
16+
17+
fixture = TestBed.createComponent(AppComponent);
18+
});
19+
20+
it('should create', () => {
21+
expect(fixture).toBeTruthy();
22+
});
23+
});

apps/web/src/app/components/plots/plots.component.spec.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, Input } from '@angular/core';
12
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';
37
import { PlotsComponent } from './plots.component';
48

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+
534
describe('PlotsComponent', () => {
635
let component: PlotsComponent;
736
let fixture: ComponentFixture<PlotsComponent>;
37+
let plotsServiceMock: PlotsService;
38+
let plots$: Subject<PlotData[]>;
839

940
beforeEach(async () => {
41+
plots$ = new Subject();
42+
plotsServiceMock = {
43+
plots$,
44+
} as unknown as PlotsService;
45+
1046
await TestBed.configureTestingModule({
11-
declarations: [PlotsComponent],
47+
declarations: [PlotsComponent, PlotComponent],
48+
providers: [{ provide: PlotsService, useValue: plotsServiceMock }],
49+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
1250
}).compileComponents();
1351
});
1452

@@ -21,4 +59,28 @@ describe('PlotsComponent', () => {
2159
it('should create', () => {
2260
expect(component).toBeTruthy();
2361
});
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+
});
2486
});

0 commit comments

Comments
 (0)