Skip to content

Commit

Permalink
fix weight service
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Oct 13, 2023
1 parent c917dd0 commit 6940824
Show file tree
Hide file tree
Showing 7 changed files with 539 additions and 295 deletions.
21 changes: 18 additions & 3 deletions client/src/app/ride/ride.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ function setup() {
const mockNotificationService: jasmine.SpyObj<NotificationService> =
jasmine.createSpyObj(['showNotification']);
const syncActivities = new Subject<void>();
const mockStravaService = {
$syncActivities: syncActivities.asObservable(),
};
const mockStravaService: jasmine.SpyObj<StravaService> = jasmine.createSpyObj(
['syncActivities']
);
mockStravaService.syncActivities.and.returnValue(
syncActivities.asObservable()
);
TestBed.configureTestingModule({
providers: [
provideHttpClient(),
Expand Down Expand Up @@ -49,6 +52,18 @@ const mockResponse2: RideStats = {

describe('RideService', () => {
describe('getRideStats', () => {
it('should sync activities first', () => {
const { service, httpTestingController } = setup();
service.getRideStats(30).subscribe((rideStats) => {
expect(rideStats).toEqual(mockResponse);
});
const request = httpTestingController.expectNone(
'/api/ride/stats?period=30'
);
expect(request).toBeUndefined();
httpTestingController.verify();
});

it('should return ride stats for given period', () => {
const { service, httpTestingController, syncActivities } = setup();
service.getRideStats(30).subscribe((rideStats) => {
Expand Down
105 changes: 53 additions & 52 deletions client/src/app/weight/weight.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { EChartsOption } from 'echarts';
import { NgxEchartsDirective, NgxEchartsModule } from 'ngx-echarts';
import { combineLatest, map } from 'rxjs';
import { combineLatest, map, switchMap } from 'rxjs';
import { HeadingComponent } from '../common-components/heading/heading.component';
import { LoaderComponent } from '../common-components/loader/loader.component';
import { TextComponent } from '../common-components/text/text.component';
Expand All @@ -29,64 +29,65 @@ import { ActivatedRoute } from '@angular/router';
styleUrls: ['./weight.component.css'],
})
export class WeightComponent {
initOpts: NgxEchartsDirective['initOpts'] = {
readonly initOpts: NgxEchartsDirective['initOpts'] = {
renderer: 'svg',
};
constructor(
private readonly weightService: WeightService,
route: ActivatedRoute
) {
route.data.subscribe((data) => weightService.selectPeriod(data['period']));
}

$chartOptions = this.weightService.$periodWeight.pipe(
map(
(measurements) =>
({
aria: {
enabled: true,
},
animation: false,
grid: {
top: 10,
right: 10,
bottom: 10,
left: 10,
},
dataset: {
source: [
['date', 'weight'],
...measurements.map(({ date, weight }) => [
new Date(date),
weight,
]),
],
},
xAxis: {
type: 'time',
show: false,
},
yAxis: {
max: 'dataMax',
min: 'dataMin',
show: false,
},
series: [
{
type: 'line',
smooth: true,
showSymbol: false,
},
],
} as EChartsOption)
private readonly route: ActivatedRoute
) {}
private readonly $todayWeight = this.weightService.getTodayWeight();
private readonly $diff = this.route.data.pipe(
switchMap((data) => this.weightService.getDiff(data['period']))
);
private readonly $chartOptions = this.route.data.pipe(
switchMap((data) =>
this.weightService.getWeight(data['period']).pipe(
map(
(measurements) =>
({
aria: {
enabled: true,
},
animation: false,
grid: {
top: 10,
right: 10,
bottom: 10,
left: 10,
},
dataset: {
source: [
['date', 'weight'],
...measurements.map(({ date, weight }) => [
new Date(date),
weight,
]),
],
},
xAxis: {
type: 'time',
show: false,
},
yAxis: {
max: 'dataMax',
min: 'dataMin',
show: false,
},
series: [
{
type: 'line',
smooth: true,
showSymbol: false,
},
],
} as EChartsOption)
)
)
)
);

$vm = combineLatest([
this.weightService.$todayWeight,
this.weightService.$diff,
this.$chartOptions,
]).pipe(
$vm = combineLatest([this.$todayWeight, this.$diff, this.$chartOptions]).pipe(
map(([todayWeight, diff, chartOptions]) => ({
todayWeight,
diff,
Expand Down
Loading

0 comments on commit 6940824

Please sign in to comment.