Skip to content

Commit

Permalink
Add subscription API introduced in json-git 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBressan authored Mar 15, 2017
1 parent bb3a47b commit 2dc806b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 15/03/2017

v0.2.0 - Add subscription API introduced in json-git 0.2.0
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Note that **you must subscribe your component to the head of your repository** i
* `getJSON(repositoryName)` exports a snapshot of a repository
* `getLog(repositoryName)` returns the full history of a repository
* `getTree(repositoryName)` returns the current tree of a repository
* `subscribe(repositoryName, subscriber)` subscribe a subscriber to a repository
* `unsubscribe(repositoryName, subscriber)` unsubscribe a subscriber from a repository

To use them, just import them from `json-git-redux`:

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-git-redux",
"version": "0.1.1",
"version": "0.2.0",
"description": "Official json-git bindings for Redux",
"main": "./lib/index.js",
"scripts": {
Expand Down Expand Up @@ -35,7 +35,7 @@
"mocha": "3.2.0"
},
"peerDependencies": {
"json-git": "~0.1.0",
"json-git": "~0.2.0",
"redux": "~3.6.0"
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const getDiff = createSelector(selectors.getDiff);
export const getLog = createSelector(selectors.getLog);
export const getTree = createSelector(selectors.getTree);
export const getJSON = createSelector(selectors.getJSON);
export const subscribe = createSelector(selectors.subscribe);
export const unsubscribe = createSelector(selectors.unsubscribe);

export function createReducer(repositories) {
Object
Expand Down
12 changes: 12 additions & 0 deletions src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ export function getJSON(registry, repositoryName) {

return registry.get(repositoryName).toJSON();
}

export function subscribe(registry, repositoryName, subscriber) {
ensureRepository(registry, repositoryName);

return registry.get(repositoryName).subscribe(subscriber);
}

export function unsubscribe(registry, repositoryName, subscriber) {
ensureRepository(registry, repositoryName);

return registry.get(repositoryName).unsubscribe(subscriber);
}
28 changes: 28 additions & 0 deletions src/selectorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
getLog,
getTree,
getJSON,
subscribe,
unsubscribe,
} from './selectors';

describe('Selectors', () => {
Expand All @@ -21,7 +23,9 @@ describe('Selectors', () => {
},
diff: expect.createSpy().andReturn('i am a diff'),
apply: expect.createSpy().andReturn('i am a tree'),
subscribe: expect.createSpy().andReturn('subscribed'),
toJSON: expect.createSpy().andReturn('i am a json'),
unsubscribe: expect.createSpy().andReturn('unsubscribed'),
};

registry = {
Expand Down Expand Up @@ -91,4 +95,28 @@ describe('Selectors', () => {
registry.has.andReturn(false);
expect(() => getJSON(registry, 'test')).toThrow(/Repository test not found/);
});

it('should call repository.subscribe() on a repository when subscribe() is called and return the result', () => {
expect(subscribe(registry, 'test', 'subscriber')).toBe('subscribed');
expect(registry.has).toHaveBeenCalledWith('test');
expect(registry.get).toHaveBeenCalledWith('test');
expect(repository.subscribe).toHaveBeenCalledWith('subscriber');
});

it("should throw an error is the repository doesn't exist when subscribe is called", () => {
registry.has.andReturn(false);
expect(() => subscribe(registry, 'test')).toThrow(/Repository test not found/);
});

it('should call repository.unsubscribe() on a repository when unsubscribe() is called and return the result', () => {
expect(unsubscribe(registry, 'test', 'subscriber')).toBe('unsubscribed');
expect(registry.has).toHaveBeenCalledWith('test');
expect(registry.get).toHaveBeenCalledWith('test');
expect(repository.unsubscribe).toHaveBeenCalledWith('subscriber');
});

it("should throw an error is the repository doesn't exist when unsubscribe is called", () => {
registry.has.andReturn(false);
expect(() => unsubscribe(registry, 'test')).toThrow(/Repository test not found/);
});
});

0 comments on commit 2dc806b

Please sign in to comment.