-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rooms): implemented rooms JSON adapter interface
- Loading branch information
Showing
4 changed files
with
202 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {Observable} from 'rxjs'; | ||
|
||
import RoomsAdapter from './RoomsAdapter'; | ||
|
||
/** | ||
* @typedef RoomJSON | ||
* @param {object} datasource An object that contains a set of rooms keyed by ID. | ||
* @example | ||
* { | ||
* "room-1": { | ||
* "ID": "room-1", | ||
* "title": "title", | ||
* "roomType": "group" | ||
* }, | ||
* "room-1-activities": [] | ||
* } | ||
*/ | ||
|
||
/* | ||
* Implements the RoomsAdapter interface with a JSON object as its datasource. See @RoomsJSON | ||
*/ | ||
export default class RoomsJSONAdapter extends RoomsAdapter { | ||
constructor(datasource) { | ||
super(datasource); | ||
this.getRoom = this.getRoom.bind(this); | ||
this.getPreviousRoomActivities = this.getPreviousRoomActivities.bind(this); | ||
this.getRoomActivities = this.getRoomActivities.bind(this); | ||
} | ||
|
||
/** | ||
* Returns an observable that emits room data of the given ID. | ||
* | ||
* @param {string} ID ID of room to get | ||
* @returns {Observable.<Room>} | ||
* @memberof RoomsJSONAdapter | ||
*/ | ||
getRoom(ID) { | ||
return Observable.create((observer) => { | ||
if (this.datasource[ID]) { | ||
observer.next(this.datasource[ID]); | ||
} else { | ||
observer.error(new Error(`Could not find room with ID "${ID}"`)); | ||
} | ||
|
||
observer.complete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns an observable that emits an array of previous activity IDs of the given roomID. | ||
* | ||
* @param {string} ID ID of the room to get. | ||
* @returns {Observable.<Array.<string>>} | ||
* @memberof RoomsAdapter | ||
*/ | ||
getPreviousRoomActivities(ID) { | ||
return Observable.create((observer) => { | ||
const data = !this.datasource[ID] ? [] : this.datasource[ID]; | ||
|
||
observer.next(data); | ||
|
||
observer.complete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns an observable that emits an array of current activity IDs of the given roomID. | ||
* | ||
* @param {string} ID ID of the room to get. | ||
* @returns {Observable.<Array.<string>>} | ||
* @memberof RoomsAdapter | ||
*/ | ||
getRoomActivities(ID) { | ||
return Observable.create((observer) => { | ||
const data = !this.datasource[ID] ? [] : this.datasource[ID]; | ||
|
||
observer.next(data); | ||
|
||
observer.complete(); | ||
}); | ||
} | ||
} |
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,111 @@ | ||
import rooms from './../data/rooms'; | ||
import RoomsJSONAdapter from './RoomsJSONAdapter'; | ||
|
||
describe('Rooms JSON Adapter Interface', () => { | ||
let roomsJSONAdapter, roomID, roomActivitiesID; | ||
|
||
beforeEach(() => { | ||
[roomID, roomActivitiesID] = Object.keys(rooms); | ||
roomsJSONAdapter = new RoomsJSONAdapter(rooms); | ||
}); | ||
|
||
test('getRoom() returns an observable', () => { | ||
expect(rxjs.isObservable(roomsJSONAdapter.getRoom())).toBeTruthy(); | ||
}); | ||
|
||
test('getRoom() returns a person data', (done) => { | ||
roomsJSONAdapter.getRoom(roomID).subscribe((data) => { | ||
expect(data).toEqual(rooms[roomID]); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getRoom() throws a proper error message', (done) => { | ||
const wrongRoomID = 'wrongRoomID'; | ||
|
||
roomsJSONAdapter.getRoom(wrongRoomID).subscribe( | ||
() => {}, | ||
(error) => { | ||
expect(error.message).toBe(`Could not find room with ID "${wrongRoomID}"`); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
test('getRoom() completes the observable', (done) => { | ||
roomsJSONAdapter.getRoom(roomID).subscribe( | ||
() => {}, | ||
() => {}, | ||
() => { | ||
expect(true).toBeTruthy(); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
test('getPreviousRoomActivities() returns an observable', () => { | ||
expect(rxjs.isObservable(roomsJSONAdapter.getPreviousRoomActivities())).toBeTruthy(); | ||
}); | ||
|
||
test('getPreviousRoomActivities() returns an array of previous activity IDs', (done) => { | ||
roomsJSONAdapter.getPreviousRoomActivities(roomActivitiesID).subscribe((data) => { | ||
expect(data).toEqual(rooms[roomActivitiesID]); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getPreviousRoomActivities() returns an observable to an empty array for a given wrong room ID', (done) => { | ||
const wrongRoomActivitiesID = 'wrongRoomActivitiesID'; | ||
|
||
roomsJSONAdapter.getRoomActivities(wrongRoomActivitiesID).subscribe((data) => { | ||
expect(data).toEqual([]); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getPreviousRoomActivities() completes the observable', (done) => { | ||
roomsJSONAdapter.getPreviousRoomActivities(roomID).subscribe( | ||
() => {}, | ||
() => {}, | ||
() => { | ||
expect(true).toBeTruthy(); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
test('getRoomActivities() returns an observable', () => { | ||
expect(rxjs.isObservable(roomsJSONAdapter.getRoomActivities())).toBeTruthy(); | ||
}); | ||
|
||
test('getRoomActivities() returns an array of previous activity IDs', (done) => { | ||
roomsJSONAdapter.getRoomActivities(roomActivitiesID).subscribe((data) => { | ||
expect(data).toEqual(rooms[roomActivitiesID]); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getRoomActivities() returns an observable to an empty array for a given wrong room ID', (done) => { | ||
const wrongRoomActivitiesID = 'wrongRoomActivitiesID'; | ||
|
||
roomsJSONAdapter.getRoomActivities(wrongRoomActivitiesID).subscribe((data) => { | ||
expect(data).toEqual([]); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('getRoomActivities() completes the observable', (done) => { | ||
roomsJSONAdapter.getRoomActivities(roomID).subscribe( | ||
() => {}, | ||
() => {}, | ||
() => { | ||
expect(true).toBeTruthy(); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
roomsJSONAdapter = null; | ||
}); | ||
}); |
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,2 +1,3 @@ | ||
export {default as people} from './people'; | ||
export {default as activities} from './activities'; | ||
export {default as rooms} from './rooms'; |
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,8 @@ | ||
{ | ||
"room-1": { | ||
"ID": "room-1", | ||
"title": "title", | ||
"roomType": "group" | ||
}, | ||
"room-1-activities": [] | ||
} |