From 2dc806bd7b36e07a2ed0f5d3bd27d895fdd8b2ce Mon Sep 17 00:00:00 2001 From: Robin Bressan Date: Wed, 15 Mar 2017 17:37:32 +0100 Subject: [PATCH] Add subscription API introduced in json-git 0.2.0 --- CHANGELOG.md | 5 +++++ README.md | 2 ++ package.json | 4 ++-- src/index.js | 2 ++ src/selectors.js | 12 ++++++++++++ src/selectorsSpec.js | 28 ++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..82778e7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 15/03/2017 + +v0.2.0 - Add subscription API introduced in json-git 0.2.0 diff --git a/README.md b/README.md index 4375c87..9cba900 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/package.json b/package.json index c9aac2b..678ebf5 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -35,7 +35,7 @@ "mocha": "3.2.0" }, "peerDependencies": { - "json-git": "~0.1.0", + "json-git": "~0.2.0", "redux": "~3.6.0" } } diff --git a/src/index.js b/src/index.js index 8ba1c55..08fe25d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/src/selectors.js b/src/selectors.js index 0c49729..f77a60f 100644 --- a/src/selectors.js +++ b/src/selectors.js @@ -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); +} diff --git a/src/selectorsSpec.js b/src/selectorsSpec.js index bdfe43b..c33fd82 100644 --- a/src/selectorsSpec.js +++ b/src/selectorsSpec.js @@ -5,6 +5,8 @@ import { getLog, getTree, getJSON, + subscribe, + unsubscribe, } from './selectors'; describe('Selectors', () => { @@ -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 = { @@ -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/); + }); });