Skip to content

Commit

Permalink
fix client app and weight tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Oct 13, 2023
1 parent 6940824 commit a26df8b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 211 deletions.
78 changes: 78 additions & 0 deletions client/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Component, Directive, Input } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { provideNoopAnimations } from '@angular/platform-browser/animations';
import { Subject, of } from 'rxjs';
import { AppComponent } from './app.component';
import { NotificationService } from './common-components/notification.service';
import { BackupService } from './backup/backup.service';
import { RelativeTimePipe } from './utils/relative-time.pipe';
import { provideHttpClient } from '@angular/common/http';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';

@Directive({
standalone: true,
selector: '[routerLink]',
})
class MockRouterLink {
@Input()
routerLink?: string;
}

@Component({
standalone: true,
selector: 'router-outlet',
template: '',
})
class MockRouterOutlet {}

async function setup() {
const mockBackupService: jasmine.SpyObj<BackupService> = jasmine.createSpyObj(
['getLastBackupTime']
);
mockBackupService.getLastBackupTime.and.returnValue(
of(new Date(Date.now() - 5 * 60 * 1000))
);

await TestBed.configureTestingModule({
imports: [RelativeTimePipe],
providers: [
provideNoopAnimations(),
provideHttpClient(),
NotificationService,
{ provide: BackupService, useValue: mockBackupService },
],
}).compileComponents();

TestBed.overrideComponent(AppComponent, {
remove: {
imports: [RouterOutlet, RouterLink, RouterLinkActive],
},
add: {
imports: [MockRouterOutlet, MockRouterLink],
},
});

const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();

return {
fixture,
element: fixture.nativeElement as HTMLElement,
};
}

describe('AppComponent', () => {
it('should render header', async () => {
const { element } = await setup();
expect(element.querySelector('header h1')?.textContent).toBe('W6');
});

it('renders last backup time', async () => {
const { element, fixture } = await setup();
expect(
Array.from(element.querySelectorAll('a')).find((e) =>
e.textContent?.includes('Last backup')
)?.textContent
).toEqual('Last backup 5 minutes ago');
});
});
134 changes: 0 additions & 134 deletions client/src/app/app.component.spex.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { TestBed } from '@angular/core/testing';

import { Directive, Input } from '@angular/core';
import { By } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { EChartsOption } from 'echarts';
import { NgxEchartsDirective, NgxEchartsModule } from 'ngx-echarts';
import { Subject } from 'rxjs';
import { of } from 'rxjs';
import { NotificationService } from '../common-components/notification.service';
import { WeightMeasurement, WeightService } from '../services/weight.service';
import { WeightComponent } from './weight.component';
import { WeightService } from './weight.service';

@Directive({
standalone: true,
Expand All @@ -21,20 +22,38 @@ class MockECharts {
initOpts?: NgxEchartsDirective['initOpts'];
}

const measurements = [
{ date: new Date('2020-05-05T00:00:00.000Z'), weight: 108.9 },
{ date: new Date('2020-05-07T00:00:00.000Z'), weight: 108.3 },
{ date: new Date('2020-05-10T00:00:00.000Z'), weight: 107.8 },
];

async function setup({ period }: { period?: number } = {}) {
const weightMeasurementSubject = new Subject<WeightMeasurement[]>();
const mockActivatedRoute = { data: of({ period }) };
const mockWeightService: jasmine.SpyObj<WeightService> = jasmine.createSpyObj(
['getWeight', 'getTodayWeight', 'getDiff']
);
mockWeightService.getWeight.and.returnValue(
weightMeasurementSubject.asObservable()
mockWeightService.getWeight.and.returnValue(of(measurements));
mockWeightService.getTodayWeight.and.returnValue(
of({
date: new Date(),
weight: 108.9,
fatMassWeight: 21.3,
fatRatio: 31,
})
);
mockWeightService.getDiff.and.returnValue(
of({
date: new Date(),
weight: -0.00132,
fatMassWeight: 0.05,
fatRatio: 0.07894,
})
);
const mockNotificationService: jasmine.SpyObj<NotificationService> =
jasmine.createSpyObj(['showNotification']);
await TestBed.configureTestingModule({
providers: [
{ provide: WeightService, useValue: mockWeightService },
{ provide: NotificationService, useValue: mockNotificationService },
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
],
}).compileComponents();

Expand All @@ -48,15 +67,12 @@ async function setup({ period }: { period?: number } = {}) {
});

const fixture = TestBed.createComponent(WeightComponent);
fixture.componentInstance.period = period;
fixture.detectChanges();

return {
fixture,
element: fixture.nativeElement as HTMLElement,
mockNotificationService,
mockWeightService,
weightSubject: weightMeasurementSubject,
};
}

Expand All @@ -67,91 +83,30 @@ describe('WeightComponent', () => {
});

it('renders weight', async () => {
const { element, fixture, weightSubject, mockWeightService } =
await setup();
weightSubject.next([]);
mockWeightService.getTodayWeight.and.returnValue({
date: new Date(),
weight: 108.9,
fatMassWeight: 21.3,
fatRatio: 31,
});
fixture.detectChanges();
const { element } = await setup();
const valueElements = element.querySelectorAll('h2 + *');
expect(valueElements[0].textContent?.trim()).toEqual('108.9 kg');
expect(valueElements[1].textContent?.trim()).toEqual('21.3 kg');
expect(valueElements[2].textContent?.trim()).toEqual('31 %');
});

it('renders - if no weight is returned', async () => {
const { element, fixture, weightSubject, mockWeightService } =
await setup();
weightSubject.next([]);
mockWeightService.getTodayWeight.and.returnValue(undefined);
fixture.detectChanges();
const valueElements = element.querySelectorAll('h2 + *');
expect(valueElements[0].textContent?.trim()).toEqual('-');
expect(valueElements[1].textContent?.trim()).toEqual('-');
expect(valueElements[2].textContent?.trim()).toEqual('-');
});

it('renders weight diff', async () => {
const { element, fixture, weightSubject, mockWeightService } =
await setup();
weightSubject.next([]);
mockWeightService.getDiff.and.returnValue({
date: new Date(),
weight: -0.00132,
fatMassWeight: 0.05,
fatRatio: 0.07894,
});
fixture.detectChanges();
const { element } = await setup();
const valueElements = element.querySelectorAll('h2 + * + *');
expect(valueElements[0].textContent?.trim()).toEqual('↓ 0.1 %');
expect(valueElements[1].textContent?.trim()).toEqual('↑ 5 %');
expect(valueElements[2].textContent?.trim()).toEqual('↑ 7.9 %');
});

it('renders - if no weight diff is returned', async () => {
const { element, fixture, weightSubject, mockWeightService } =
await setup();
weightSubject.next([]);
mockWeightService.getDiff.and.returnValue(undefined);
fixture.detectChanges();
const valueElements = element.querySelectorAll('h2 + * + *');
expect(valueElements[0].textContent?.trim()).toEqual('-');
expect(valueElements[1].textContent?.trim()).toEqual('-');
expect(valueElements[2].textContent?.trim()).toEqual('-');
});

it('renders error state', async () => {
const { mockNotificationService, fixture, weightSubject } = await setup();
weightSubject.error({});
fixture.detectChanges();
expect(mockNotificationService.showNotification).toHaveBeenCalledWith(
'Unable to fetch weight',
'error'
);
});

it('fetches weight meausrements with week period', async () => {
const { fixture, weightSubject, mockWeightService } = await setup({
const { mockWeightService } = await setup({
period: 7,
});
weightSubject.next([]);
fixture.detectChanges();
expect(mockWeightService.getWeight).toHaveBeenCalledWith(7);
});

it('renders weight chart', async () => {
const { fixture, weightSubject } = await setup();
const measurements: WeightMeasurement[] = [
{ date: new Date('2020-05-05T00:00:00.000Z'), weight: 108.9 },
{ date: new Date('2020-05-07T00:00:00.000Z'), weight: 108.3 },
{ date: new Date('2020-05-10T00:00:00.000Z'), weight: 107.8 },
];
weightSubject.next(measurements);
fixture.detectChanges();
const { fixture } = await setup();
expect(
fixture.debugElement
.query(By.directive(MockECharts))
Expand Down
4 changes: 3 additions & 1 deletion client/src/app/weight/weight.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ describe('WeightService', () => {
service.getTodayWeight().subscribe();
service.getDiff(1).subscribe();
syncMeasurements.next();
httpTestingController.expectOne('/api/weight?period=1').flush(mockResponse);
const request = httpTestingController.expectOne('/api/weight?period=1');
request.flush(mockResponse);
expect(request.request.method).toBe('GET');
httpTestingController.verify();
});
});

0 comments on commit a26df8b

Please sign in to comment.