Skip to content

Commit

Permalink
feat(rooms): implemented rooms JSON adapter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akoushke committed Sep 10, 2019
1 parent 34fdf43 commit 7d30e10
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/adapters/RoomsJSONAdapter.js
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();
});
}
}
111 changes: 111 additions & 0 deletions src/adapters/RoomsJSONAdapter.test.js
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;
});
});
1 change: 1 addition & 0 deletions src/data/index.js
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';
8 changes: 8 additions & 0 deletions src/data/rooms.json
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": []
}

0 comments on commit 7d30e10

Please sign in to comment.