Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from adamwatters/incidents
added incidents menu and notifications
- Loading branch information
Showing
12 changed files
with
599 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '11' | ||
- '10' | ||
os: osx | ||
script: npm run test:ci && npm run release |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const firstResponse = { | ||
items: [ | ||
{ link: 'id=1', content: 'not updated' }, | ||
{ link: 'id=2', content: 'not updated' } | ||
] | ||
}; | ||
const secondResponse = { | ||
items: [ | ||
{ link: 'id=1', content: 'not updated' }, | ||
{ link: 'id=2', content: 'not updated' }, | ||
{ link: 'id=3', content: 'not updated' } | ||
] | ||
}; | ||
const thirdResponse = { | ||
items: [ | ||
{ link: 'id=1', content: 'updated' }, | ||
{ link: 'id=2', content: 'not updated' }, | ||
{ link: 'id=3', content: 'not updated' } | ||
] | ||
}; | ||
const fourthResponse = { | ||
items: [ | ||
{ link: 'id=1', content: 'updated' }, | ||
{ link: 'id=2', content: 'not updated' }, | ||
{ link: 'id=3', content: 'not updated' } | ||
] | ||
}; | ||
|
||
jest.doMock( | ||
'rss-parser', | ||
() => | ||
// has to use function keyword to be called with new keyword (ie act as a constructor) | ||
/* tslint:disable-line only-arrow-functions */ function() { | ||
return { | ||
parseURL: jest | ||
.fn() | ||
.mockReturnValueOnce(firstResponse) | ||
.mockReturnValueOnce(secondResponse) | ||
.mockReturnValueOnce(thirdResponse) | ||
.mockReturnValueOnce(fourthResponse) | ||
}; | ||
} | ||
); | ||
|
||
import IncidentFeed from './incidentFeed'; | ||
const incidentFeed = new IncidentFeed(); | ||
|
||
describe('IncidentFeed', () => { | ||
test('before :update is called, :getFeed returns an empty array', () => { | ||
expect(incidentFeed.getFeed()).toMatchObject([]); | ||
expect(incidentFeed.getFeed()).not.toMatchObject(['some value']); | ||
}); | ||
test('first update', async () => { | ||
await incidentFeed.update(); | ||
expect(incidentFeed.getFeed()).toBe(firstResponse.items); | ||
expect(incidentFeed.newIncidents()).toMatchObject([]); | ||
expect(incidentFeed.updatedIncidents()).toMatchObject([]); | ||
}); | ||
test('second update', async () => { | ||
await incidentFeed.update(); | ||
expect(incidentFeed.getFeed()).toBe(secondResponse.items); | ||
expect(incidentFeed.newIncidents()).toMatchObject([ | ||
{ link: 'id=3', content: 'not updated' } | ||
]); | ||
expect(incidentFeed.updatedIncidents()).toMatchObject([]); | ||
}); | ||
test('third update', async () => { | ||
await incidentFeed.update(); | ||
expect(incidentFeed.getFeed()).toBe(thirdResponse.items); | ||
expect(incidentFeed.newIncidents()).toMatchObject([]); | ||
expect(incidentFeed.updatedIncidents()).toMatchObject([ | ||
{ link: 'id=1', content: 'updated' } | ||
]); | ||
}); | ||
test('fourth update', async () => { | ||
await incidentFeed.update(); | ||
expect(incidentFeed.getFeed()).toBe(fourthResponse.items); | ||
expect(incidentFeed.newIncidents()).toMatchObject([]); | ||
expect(incidentFeed.updatedIncidents()).toMatchObject([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Parser from 'rss-parser'; | ||
|
||
const FEED_URL = 'https://www.netlifystatus.com/history.rss'; | ||
|
||
export interface IFeedItem { | ||
title: string; | ||
pubDate: string; | ||
content: string; | ||
link: string; | ||
} | ||
|
||
export default class IncidentFeed { | ||
private parser: { parseURL(feedUrl: string) }; | ||
private currentFeed: IFeedItem[]; | ||
private previousFeed: IFeedItem[]; | ||
|
||
constructor() { | ||
this.parser = new Parser(); | ||
this.currentFeed = []; | ||
this.previousFeed = []; | ||
} | ||
|
||
public async update(): Promise<any> { | ||
const fetchedFeed: IFeedItem[] = await this.fetchAndParseFeed(); | ||
this.previousFeed = this.currentFeed; | ||
this.currentFeed = fetchedFeed; | ||
} | ||
|
||
public newIncidents(): ReadonlyArray<IFeedItem> { | ||
if (this.previousFeed.length === 0) { | ||
return []; | ||
} | ||
return this.currentFeed.filter(currentItem => { | ||
return !this.previousFeed.some(previousItem => { | ||
return previousItem.link === currentItem.link; | ||
}); | ||
}); | ||
} | ||
|
||
public updatedIncidents(): ReadonlyArray<IFeedItem> { | ||
return this.currentFeed.filter(currentItem => { | ||
return this.previousFeed.find(previousItem => { | ||
return ( | ||
previousItem.link === currentItem.link && | ||
previousItem.content !== currentItem.content | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
public getFeed(): ReadonlyArray<IFeedItem> { | ||
return this.currentFeed as ReadonlyArray<IFeedItem>; | ||
} | ||
|
||
private async fetchAndParseFeed(): Promise<IFeedItem[]> { | ||
const response = await this.parser.parseURL(FEED_URL); | ||
return response.items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.