File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 1+ import { Observable } from 'rxjs' ;
2+
3+ import ActivitiesAdapter from './ActivitiesAdapter' ;
4+
5+ /**
6+ * @typedef ActivitiesJSON
7+ * @param {object } datasource An object that contains a set of activities keyed by ID.
8+ * @example
9+ * {
10+ * "activity-1": {
11+ * "ID": "activity-1",
12+ * "roomID": "roomID",
13+ * "text": "text",
14+ * "personID": "personID",
15+ * "created": "created",
16+ * "displayAuthor": false
17+ * }
18+ * }
19+ */
20+
21+ /*
22+ * Implements the ActivitiesAdapter interface with a JSON object as its datasource. See @ActivitiesJSON
23+ */
24+ export default class ActivitiesJSONAdapter extends ActivitiesAdapter {
25+ constructor ( datasource ) {
26+ super ( datasource ) ;
27+ this . getActivity = this . getActivity . bind ( this ) ;
28+ }
29+
30+ /**
31+ * Returns an observable that emits activity data of the given ID.
32+ *
33+ * @param {string } ID ID of activity to get
34+ * @returns {Observable.<Activity> }
35+ * @memberof ActivityJSONAdapter
36+ */
37+ getActivity ( ID ) {
38+ return Observable . create ( ( observer ) => {
39+ if ( this . datasource [ ID ] ) {
40+ observer . next ( this . datasource [ ID ] ) ;
41+ } else {
42+ observer . error ( new Error ( `Could not find activity with ID "${ ID } "` ) ) ;
43+ }
44+
45+ observer . complete ( ) ;
46+ } ) ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ import activities from './../data/activities' ;
2+ import ActivitiesJSONAdapter from './ActivitiesJSONAdapter' ;
3+
4+ describe ( 'Activities JSON Adapter Interface' , ( ) => {
5+ let activitiesJSONAdapter , activityID ;
6+
7+ beforeEach ( ( ) => {
8+ [ activityID ] = Object . keys ( activities ) ;
9+ activitiesJSONAdapter = new ActivitiesJSONAdapter ( activities ) ;
10+ } ) ;
11+
12+ test ( 'getActivity() returns an observable' , ( ) => {
13+ expect ( rxjs . isObservable ( activitiesJSONAdapter . getActivity ( ) ) ) . toBeTruthy ( ) ;
14+ } ) ;
15+
16+ test ( 'getActivity() returns an activity' , ( done ) => {
17+ activitiesJSONAdapter . getActivity ( activityID ) . subscribe ( ( data ) => {
18+ expect ( data ) . toEqual ( activities [ activityID ] ) ;
19+ done ( ) ;
20+ } ) ;
21+ } ) ;
22+
23+ test ( `getActivity() throws a proper error message when activity doesn't exist` , ( done ) => {
24+ const wrongActivityID = 'wrongActivityID' ;
25+
26+ activitiesJSONAdapter . getActivity ( wrongActivityID ) . subscribe (
27+ ( ) => { } ,
28+ ( error ) => {
29+ expect ( error . message ) . toBe ( `Could not find activity with ID "${ wrongActivityID } "` ) ;
30+ done ( ) ;
31+ }
32+ ) ;
33+ } ) ;
34+
35+ test ( 'getActivity() completes the observable' , ( done ) => {
36+ activitiesJSONAdapter . getActivity ( activityID ) . subscribe (
37+ ( ) => { } ,
38+ ( ) => { } ,
39+ ( ) => {
40+ expect ( true ) . toBeTruthy ( ) ;
41+ done ( ) ;
42+ }
43+ ) ;
44+ } ) ;
45+
46+ afterEach ( ( ) => {
47+ activityID = null ;
48+ activitiesJSONAdapter = null ;
49+ } ) ;
50+ } ) ;
Original file line number Diff line number Diff line change 11export { default as WebexAdapter } from './WebexAdapter' ;
22export { default as PeopleAdapter } from './PeopleAdapter' ;
33export { default as PeopleJSONAdapter } from './PeopleJSONAdapter' ;
4+ export { default as ActivitiesAdapter } from './ActivitiesAdapter' ;
5+ export { default as ActivitiesJSONAdapter } from './ActivitiesJSONAdapter' ;
Original file line number Diff line number Diff line change 1+ {
2+ "activity-1" : {
3+ "ID" : " activity-1" ,
4+ "roomID" : " roomID" ,
5+ "text" : " text" ,
6+ "personID" : " personID" ,
7+ "created" : " created" ,
8+ "displayAuthor" : false
9+ }
10+ }
You can’t perform that action at this time.
0 commit comments