Skip to content

Commit ab5e3b2

Browse files
committed
Add tests for dataService and add filterOptions to exclude start date
1 parent cf4a0bd commit ab5e3b2

File tree

8 files changed

+89
-13
lines changed

8 files changed

+89
-13
lines changed

karma.conf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ module.exports = function (config) {
2222
thresholds: {
2323
emitWarning: false,
2424
global: {
25-
statements: 85,
26-
branches: 85,
27-
functions: 85,
28-
lines: 85,
25+
statements: 90,
26+
branches: 75,
27+
functions: 90,
28+
lines: 90,
2929
},
3030
},
3131
},

src/app.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ describe('AppComponent', () => {
1414
}).compileComponents();
1515
}));
1616

17-
it('should create the app', () => {
17+
it('should create the app.', () => {
1818
const fixture = TestBed.createComponent(AppComponent);
1919
const app = fixture.componentInstance;
2020
expect(app).toBeTruthy();
2121
});
2222

23-
it(`should have as title 'Guide-Doge'`, () => {
23+
it(`should have as title 'Guide-Doge'.`, () => {
2424
const fixture = TestBed.createComponent(AppComponent);
2525
const app = fixture.componentInstance;
2626
expect(app.TITLE).toEqual('Guide-Doge');

src/models/data-cube/filters.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
import { Category, Filter, Row } from './types';
1+
import { Category, Filter, FilterOptions, Row } from './types';
22

33
const millisecondsPerDay = 24 * 60 * 60 * 1000;
44

5-
export function betweenDates(startDate: Date, endDate: Date): Filter {
5+
const defaultOptions: FilterOptions = {
6+
excludeStartDate: false,
7+
excludeEndDate: false,
8+
};
9+
10+
export function betweenDates(startDate: Date, endDate: Date, options: Partial<FilterOptions> = {}): Filter {
11+
const { excludeStartDate, excludeEndDate } = {
12+
...defaultOptions,
13+
...options,
14+
};
615
return (categories: Category[]) => {
716
const nThDayIndex = categories.findIndex(
817
category => category.name === 'nthDay',
918
);
10-
const startIndex = Math.round(
19+
let startIndex = Math.round(
1120
(Date.now() - startDate.getTime()) / millisecondsPerDay,
1221
);
13-
const endIndex = Math.round(
22+
if (excludeStartDate) {
23+
startIndex--;
24+
}
25+
let endIndex = Math.round(
1426
(Date.now() - endDate.getTime()) / millisecondsPerDay,
1527
);
28+
if (excludeEndDate) {
29+
endIndex++;
30+
}
1631
return (row: Row) =>
1732
row.header[nThDayIndex] <= startIndex &&
1833
row.header[nThDayIndex] >= endIndex;

src/models/data-cube/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ export interface ResultRow {
116116
categories: Map<string, string | number>;
117117
values: Map<string, number>;
118118
}
119+
120+
/**
121+
* The options used to filter rows.
122+
*/
123+
export interface FilterOptions {
124+
/** Exclude start date */
125+
excludeStartDate: boolean;
126+
/** Exclude end date */
127+
excludeEndDate: boolean;
128+
}

src/models/melody/melody.model.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Melody', () => {
3030
expect(melody.duration).toBe(noteDuration * values.length);
3131
});
3232

33-
it('should have playhead at the very beginning of the sequence', () => {
33+
it('should have playhead at the very beginning of the sequence.', () => {
3434
expect(melody.currentDatumIndex).toBe(0);
3535
});
3636

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { DataService } from './data.service';
2+
import { mockMeasureName } from '../../utils/mocks.spec';
3+
import { Duration } from 'luxon';
4+
5+
describe('DataService', () => {
6+
let dataService: DataService;
7+
8+
beforeEach(() => {
9+
dataService = new DataService();
10+
});
11+
12+
it('should instantiate.', () => {
13+
expect(dataService).toBeInstanceOf(DataService);
14+
});
15+
16+
it('should get data for the given days.', () => {
17+
for (const days of [7, 14, 30]) {
18+
const data = dataService.getMeasureOverDays(mockMeasureName, days);
19+
expect(data.length).toBe(days);
20+
const firstDate = data[0].date;
21+
const lastDate = data[data.length - 1].date;
22+
const actualDifference = lastDate.getTime() - firstDate.getTime();
23+
const expectedDifference = Duration.fromObject({ days: days - 1 }).as('milliseconds');
24+
expect(actualDifference).toBe(expectedDifference);
25+
}
26+
});
27+
});

src/services/data/data.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../../models/data-cube/presets';
1010
import { betweenDates } from '../../models/data-cube/filters';
1111
import { generateCube } from 'src/models/data-cube/generation';
12+
import { Datum } from '../../d3/xy-chart.d3';
1213

1314
export class DataService {
1415
private dataCube = generateCube(
@@ -24,7 +25,7 @@ export class DataService {
2425
},
2526
);
2627

27-
getMeasureOverDays(measureName: string, days = 30) {
28+
getMeasureOverDays(measureName: string, days = 30): Datum[] {
2829
const categoryName = 'nthDay';
2930
const endDate = DateTime.local();
3031
const startDate = endDate.minus({ day: days });
@@ -33,7 +34,7 @@ export class DataService {
3334
.getDataFor(
3435
[categoryName],
3536
[measureName],
36-
[betweenDates(startDate.toJSDate(), endDate.toJSDate())],
37+
[betweenDates(startDate.toJSDate(), endDate.toJSDate(), { excludeStartDate: true })],
3738
)
3839
.map(row => ({
3940
date: startDate

src/utils/mocks.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AudificationPreference, Preference } from '../services/preference/types';
2+
import { Datum } from '../d3/xy-chart.d3';
3+
import { activeUserMeasure } from '../models/data-cube/presets';
4+
5+
export const mockPreference: Preference = {
6+
enabled: false,
7+
};
8+
9+
export const mockAudificationPreference: AudificationPreference = {
10+
...mockPreference,
11+
highestPitch: 0,
12+
lowestPitch: 0,
13+
noteDuration: 0,
14+
readAfter: false,
15+
readBefore: false,
16+
};
17+
18+
export const mockDatum: Datum = {
19+
date: new Date(),
20+
value: 0,
21+
};
22+
23+
export const mockMeasureName = activeUserMeasure.name;

0 commit comments

Comments
 (0)