Skip to content

Commit 6f9826b

Browse files
author
ijz953
committed
docs: move date range and upcoming vs past logic into event service
1 parent 3b5f1f6 commit 6f9826b

File tree

6 files changed

+262
-83
lines changed

6 files changed

+262
-83
lines changed

projects/ngrx.io/content/marketing/events.json

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,88 @@
2626
"endDate": "10-03-2018"
2727
},
2828
{
29-
"name": "AngularMix - Use the discount code "RYAN" or "ROBERTS" to receive $50 off your registration!",
29+
"name": "AngularMix - Use the discount code \"RYAN\" or \"ROBERTS\" to receive $50 off your registration!",
3030
"url": "http://www.angularmix.com",
3131
"location": "Orlando, Florida",
3232
"startDate": "10-10-2018",
3333
"endDate": "10-12-2018"
3434
},
3535
{
36-
"name": "AngularConnect", "url": "http://www.angularconnect.com", "location": "Excel, London", "startDate": "11-06-2018", "endDate": "11-07-2018"},
36+
"name": "AngularConnect",
37+
"url": "http://www.angularconnect.com",
38+
"location": "Excel, London",
39+
"startDate": "11-06-2018",
40+
"endDate": "11-07-2018"
41+
},
3742
{
38-
"name": "ngAtlanta", "url": "http://ng-atl.org/", "location": "Atlanta, Georgia", "startDate": "01-09-2019", "endDate": "01-12-2019"},
43+
"name": "ngAtlanta",
44+
"url": "http://ng-atl.org/",
45+
"location": "Atlanta, Georgia",
46+
"startDate": "01-09-2019",
47+
"endDate": "01-12-2019"
48+
},
3949
{
40-
"name": "ng-India", "url": "https://www.ng-ind.com", "location": "Gurgaon, India", "endDate": "02-23-2019"},
50+
"name": "ng-India",
51+
"url": "https://www.ng-ind.com",
52+
"location": "Gurgaon, India",
53+
"endDate": "02-23-2019"
54+
},
4155
{
42-
"name": "Open Source 101", "url": "https://opensource101.com/", "location": "Columbia, South Carolina", "endDate": "04-18-2019"},
56+
"name": "Open Source 101",
57+
"url": "https://opensource101.com/",
58+
"location": "Columbia, South Carolina",
59+
"endDate": "04-18-2019"
60+
},
4361
{
44-
"name": "NG-Conf", "url": "https://www.ng-conf.org", "location": "Salt Lake City, Utah", "startDate": "05-01-2019", "endDate": "05-03-2019"},
62+
"name": "NG-Conf",
63+
"url": "https://www.ng-conf.org",
64+
"location": "Salt Lake City, Utah",
65+
"startDate": "05-01-2019", "endDate": "05-03-2019"},
4566
{
46-
"name": "ngVikings", "url": "https://ngvikings.org/", "location": "Copenhagen, Denmark", "startDate": "05-26-2019", "endDate": "05-28-2019"},
67+
"name": "ngVikings",
68+
"url": "https://ngvikings.org/",
69+
"location": "Copenhagen, Denmark",
70+
"startDate": "05-26-2019",
71+
"endDate": "05-28-2019"
72+
},
4773
{
48-
"name": "REFACTR.TECH", "url": "https://refactr.tech/", "location": "Atlanta, Georgia", "startDate": "06-05-2019", "endDate": "06-07-2019"},
74+
"name": "REFACTR.TECH",
75+
"url": "https://refactr.tech/",
76+
"location": "Atlanta, Georgia",
77+
"startDate": "06-05-2019",
78+
"endDate": "06-07-2019"
79+
},
4980
{
50-
"name": "AngularUP", "url": "https://angular-up.com/", "location": "Tel Aviv, Israel", "endDate": "06-12-2019"},
81+
"name": "AngularUP",
82+
"url": "https://angular-up.com/",
83+
"location": "Tel Aviv, Israel",
84+
"endDate": "06-12-2019"
85+
},
5186
{
52-
"name": "NG-MY", "url": "https://ng-my.org/", "location": "Kuala Lumpur, Malaysia", "startDate": "07-06-2019", "endDate": "07-07-2019"},
87+
"name": "NG-MY",
88+
"url": "https://ng-my.org/",
89+
"location": "Kuala Lumpur, Malaysia",
90+
"startDate": "07-06-2019",
91+
"endDate": "07-07-2019"
92+
},
5393
{
54-
"name": "ngDenver", "url": "http://angulardenver.com/", "location": "Denver, Colorado", "startDate": "08-01-2019", "endDate": "08-02-2019"},
94+
"name": "ngDenver",
95+
"url": "http://angulardenver.com/",
96+
"location": "Denver, Colorado",
97+
"startDate": "08-01-2019",
98+
"endDate": "08-02-2019"
99+
},
55100
{
56-
"name": "AngularConnect", "url": "https://www.angularconnect.com/", "location": "London, United Kingdom", "startDate": "09-19-2019", "endDate": "09-20-2019"},
101+
"name": "AngularConnect",
102+
"url": "https://www.angularconnect.com/",
103+
"location": "London, United Kingdom",
104+
"startDate": "09-19-2019",
105+
"endDate": "09-20-2019"
106+
},
57107
{
58-
"name": "NG-Rome", "url": "https://ngrome.io/", "location": "Rome, Italy", "endDate": "10-07-2019"}
59-
]
108+
"name": "NG-Rome",
109+
"url": "https://ngrome.io/",
110+
"location": "Rome, Italy",
111+
"endDate": "10-07-2019"
112+
}
113+
]

projects/ngrx.io/src/app/custom-elements/events/event-list.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReflectiveInjector } from '@angular/core';
22
import { EventListComponent } from './event-list.component';
3-
import { Event } from './event.model';
3+
import { EventResponse } from './event.model';
44

55
// Testing the component class behaviors, independent of its template
66
// Let e2e tests verify how it displays.
@@ -18,7 +18,7 @@ describe('EventListComponent', () => {
1818
it('should put each event into the correct bucket and correctly format the date range string', () => {
1919
component = getComponent();
2020
component.currentDate = new Date('01-02-2019');
21-
const mockEvents: Event[] = [
21+
const mockEvents: EventResponse[] = [
2222
{
2323
name: 'conf1',
2424
url: '',
Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component } from '@angular/core';
2-
import { DisplayEvent } from './event.model';
2+
import { Event } from './event.model';
33
import { EventService } from './event.service';
4+
import { Observable } from 'rxjs';
45

56
@Component({
67
selector: `aio-event-list`,
@@ -15,7 +16,7 @@ import { EventService } from './event.service';
1516
</tr>
1617
</thead>
1718
<tbody>
18-
<tr *ngFor="let upcomingEvent of upcomingEvents">
19+
<tr *ngFor="let upcomingEvent of upcomingEvents$ | async">
1920
<th><a [href]="upcomingEvent.url" [title]="upcomingEvent.name">{{upcomingEvent.name}}</a></th>
2021
<td>{{upcomingEvent.location}}</td>
2122
<td>{{upcomingEvent.dateRangeString}}</td>
@@ -32,7 +33,7 @@ import { EventService } from './event.service';
3233
</tr>
3334
</thead>
3435
<tbody>
35-
<tr *ngFor="let pastEvent of pastEvents">
36+
<tr *ngFor="let pastEvent of pastEvents$ | async">
3637
<th><a [href]="pastEvent.url" [title]="pastEvent.name">{{pastEvent.name}}</a></th>
3738
<td>{{pastEvent.location}}</td>
3839
<td>{{pastEvent.dateRangeString}}</td>
@@ -42,56 +43,8 @@ import { EventService } from './event.service';
4243
`
4344
})
4445
export class EventListComponent {
45-
upcomingEvents: DisplayEvent[] = [];
46-
pastEvents: DisplayEvent[] = [];
47-
currentDate = new Date();
46+
upcomingEvents$: Observable<Event[]> = this.eventService.upcomingEvents;
47+
pastEvents$: Observable<Event[]> = this.eventService.pastEvents;
4848

49-
/**
50-
* The date range string for the two given dates
51-
* '01-01-2019' until '01-01-2019' -> 'January 1, 2019'
52-
* '01-01-2019' until '01-02-2019' -> 'January 1 - 2, 2019'
53-
* '01-28-2019' until '02-01-2019' -> 'January 28 - February 1, 2019'
54-
* '12-31-2018' until '01-01-2019' -> 'December 31, 2018 - January 1, 2019'
55-
* @param startDate
56-
* @param endDate
57-
*/
58-
private static getDateRange(startDate: Date | undefined, endDate: Date): string {
59-
if (!startDate || startDate.getTime() === endDate.getTime()) {
60-
return endDate.toLocaleDateString('en-us', { year: 'numeric', month: 'long', day: 'numeric' });
61-
} else {
62-
if (startDate.getMonth() === endDate.getMonth()) {
63-
return startDate.toLocaleString('en-us', { month: 'long' })
64-
+ ' ' + startDate.getUTCDate() + ' - ' + endDate.getUTCDate()
65-
+ ', ' + startDate.getUTCFullYear();
66-
} else if (startDate.getUTCFullYear() === endDate.getUTCFullYear()) {
67-
return startDate.toLocaleString('en-us', { month: 'long' })
68-
+ ' ' + startDate.getUTCDate()
69-
+ ' - ' + endDate.toLocaleString('en-us', { month: 'long' })
70-
+ ' ' + endDate.getUTCDate()
71-
+ ', ' + startDate.getUTCFullYear();
72-
} else {
73-
return startDate.toLocaleDateString('en-us', { year: 'numeric', month: 'long', day: 'numeric' })
74-
+ ' - ' + endDate.toLocaleDateString('en-us', { year: 'numeric', month: 'long', day: 'numeric' });
75-
}
76-
}
77-
}
78-
79-
constructor(private eventService: EventService) {
80-
console.log('hello2');
81-
this.eventService.events.subscribe(value => {
82-
console.log('hello3' + value);
83-
const displayEvents: DisplayEvent[] = value.map(event => {
84-
const startDate = event.startDate ? new Date(event.startDate) : undefined;
85-
const endDate = new Date(event.endDate);
86-
return {
87-
...event,
88-
startDate,
89-
endDate,
90-
dateRangeString: EventListComponent.getDateRange(startDate, endDate)
91-
};
92-
});
93-
this.upcomingEvents = displayEvents.filter(event => event.endDate >= this.currentDate);
94-
this.pastEvents = displayEvents.filter(event => event.endDate < this.currentDate);
95-
});
96-
}
49+
constructor(private eventService: EventService) { }
9750
}

projects/ngrx.io/src/app/custom-elements/events/event.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export interface Event {
1+
export interface EventResponse {
22
name: string;
33
url: string;
44
location: string;
55
startDate?: string;
66
endDate: string;
77
}
88

9-
export interface DisplayEvent {
9+
export interface Event {
1010
name: string;
1111
url: string;
1212
location: string;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import {
2+
HttpClientTestingModule,
3+
HttpTestingController,
4+
} from '@angular/common/http/testing';
5+
import { Injector } from '@angular/core';
6+
import { TestBed } from '@angular/core/testing';
7+
import { EventService } from './event.service';
8+
import { Event, EventResponse } from './event.model';
9+
10+
describe('EventService', () => {
11+
let injector: Injector;
12+
let eventService: EventService;
13+
let httpMock: HttpTestingController;
14+
15+
beforeEach(() => {
16+
injector = TestBed.configureTestingModule({
17+
imports: [HttpClientTestingModule],
18+
providers: [EventService],
19+
});
20+
21+
eventService = injector.get<EventService>(EventService);
22+
httpMock = injector.get(HttpTestingController);
23+
});
24+
25+
afterEach(() => httpMock.verify());
26+
27+
it('should make a single connection to the server', () => {
28+
const req = httpMock.expectOne({});
29+
expect(req.request.url).toBe('generated/events.json');
30+
});
31+
32+
describe('#events', () => {
33+
let events: Event[];
34+
let testData: EventResponse[];
35+
36+
beforeEach(() => {
37+
testData = getTestEventResponse();
38+
httpMock.expectOne({}).flush(testData);
39+
eventService.upcomingEvents.subscribe(results => (events = results));
40+
eventService.pastEvents.subscribe(results => (events = results));
41+
});
42+
43+
it('upcomingEvents observable should complete', () => {
44+
let completed = false;
45+
eventService.upcomingEvents.subscribe(
46+
undefined,
47+
undefined,
48+
() => (completed = true)
49+
);
50+
expect(completed).toBe(true, 'observable completed');
51+
});
52+
53+
it('pastEvents observable should complete', () => {
54+
let completed = false;
55+
eventService.pastEvents.subscribe(
56+
undefined,
57+
undefined,
58+
() => (completed = true)
59+
);
60+
expect(completed).toBe(true, 'observable completed');
61+
});
62+
63+
// it('should reshape the contributor json to expected result', () => {
64+
// const groupNames = contribs.map(g => g.name).join(',');
65+
// expect(groupNames).toEqual('Angular,GDE');
66+
// });
67+
68+
// it('should have expected "GDE" contribs in order', () => {
69+
// const gde = contribs[1];
70+
// const actualAngularNames = gde.contributors.map(l => l.name).join(',');
71+
// const expectedAngularNames = [testData.jeffcross, testData.kapunahelewong]
72+
// .map(l => l.name)
73+
// .join(',');
74+
// expect(actualAngularNames).toEqual(expectedAngularNames);
75+
// });
76+
});
77+
78+
it('should do WHAT(?) if the request fails');
79+
});
80+
81+
function getTestEventResponse(): EventResponse[] {
82+
return [
83+
{
84+
name: 'conf1',
85+
url: '',
86+
location: '',
87+
startDate: '06-28-2018',
88+
endDate: '07-01-2018'
89+
},
90+
{
91+
name: 'conf2',
92+
url: '',
93+
location: '',
94+
startDate: '12-25-2018',
95+
endDate: '01-01-2019'
96+
},
97+
{
98+
name: 'conf3',
99+
url: '',
100+
location: '',
101+
startDate: '01-01-2019',
102+
endDate: '01-03-2019'
103+
},
104+
{
105+
name: 'conf4',
106+
url: '',
107+
location: '',
108+
endDate: '04-01-2019'
109+
},
110+
{
111+
name: 'conf5',
112+
url: '',
113+
location: '',
114+
startDate: '04-02-2019',
115+
endDate: '04-02-2019'
116+
},
117+
];
118+
}

0 commit comments

Comments
 (0)