From 2c0e17cb977a1e8782799b337df8b1583d019906 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Mon, 12 Oct 2015 22:36:01 +0200 Subject: bootstraping react --- src/web/js/stores/MenuStore.js | 56 +++++++++++++++++ src/web/js/stores/MiddlePanelStore.js | 68 ++++++++++++++++++++ src/web/js/stores/__tests__/TodoStore-test.js | 90 +++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 src/web/js/stores/MenuStore.js create mode 100644 src/web/js/stores/MiddlePanelStore.js create mode 100644 src/web/js/stores/__tests__/TodoStore-test.js (limited to 'src/web/js/stores') diff --git a/src/web/js/stores/MenuStore.js b/src/web/js/stores/MenuStore.js new file mode 100644 index 00000000..d7478091 --- /dev/null +++ b/src/web/js/stores/MenuStore.js @@ -0,0 +1,56 @@ +var JarrDispatcher = require('../dispatcher/JarrDispatcher'); +var MenuActionTypes = require('../constants/JarrConstants').MenuActionTypes; +var EventEmitter = require('events').EventEmitter; +var CHANGE_EVENT = 'change_menu'; +var assign = require('object-assign'); + + +var MenuStore = assign({}, EventEmitter.prototype, { + _datas: {filter: 'all', categories: [], all_unread_count: 0}, + getAll: function() { + return this._datas; + }, + setFilter: function(value) { + if(this._datas.filter != value) { + this._datas.filter = value; + this.emitChange(); + } + }, + readFeedArticle: function(feed_id) { + // TODO + }, + emitChange: function() { + this.emit(CHANGE_EVENT); + }, + addChangeListener: function(callback) { + this.on(CHANGE_EVENT, callback); + }, + removeChangeListener: function(callback) { + this.removeListener(CHANGE_EVENT, callback); + }, +}); + + +MenuStore.dispatchToken = JarrDispatcher.register(function(action) { + switch(action.type) { + case MenuActionTypes.RELOAD_MENU: + MenuStore._datas['categories'] = action.categories; + MenuStore._datas['all_unread_count'] = action.all_unread_count; + MenuStore.emitChange(); + break; + case MenuActionTypes.MENU_FILTER_ALL: + MenuStore.setFilter('all'); + break; + case MenuActionTypes.MENU_FILTER_UNREAD: + MenuStore.setFilter('unread'); + break; + case MenuActionTypes.MENU_FILTER_ERROR: + MenuStore.setFilter('error'); + break; + + default: + // do nothing + } +}); + +module.exports = MenuStore; diff --git a/src/web/js/stores/MiddlePanelStore.js b/src/web/js/stores/MiddlePanelStore.js new file mode 100644 index 00000000..d5744e20 --- /dev/null +++ b/src/web/js/stores/MiddlePanelStore.js @@ -0,0 +1,68 @@ +var JarrDispatcher = require('../dispatcher/JarrDispatcher'); +var MiddlePanelActionTypes = require('../constants/JarrConstants').MiddlePanelActionTypes; +var EventEmitter = require('events').EventEmitter; +var CHANGE_EVENT = 'change_middle_panel'; +var assign = require('object-assign'); + + +var MiddlePanelStore = assign({}, EventEmitter.prototype, { + _datas: {filter: 'unread', articles: [], + parent_filter_type: null, parent_filter_id: null}, + getAll: function() { + return this._datas; + }, + setFilter: function(value) { + if(this._datas.filter != value) { + this._datas.filter = value; + this.emitChange(); + } + }, + setParentFilter: function(type, value) { + if(this._datas['parent_filter_id'] != value + || this._datas['parent_filter_type'] != type) { + this._datas['parent_filter_type'] = type; + this._datas['parent_filter_id'] = value; + this.emitChange(); + } + }, + emitChange: function() { + this.emit(CHANGE_EVENT); + }, + addChangeListener: function(callback) { + this.on(CHANGE_EVENT, callback); + }, + removeChangeListener: function(callback) { + this.removeListener(CHANGE_EVENT, callback); + }, +}); + + +MiddlePanelStore.dispatchToken = JarrDispatcher.register(function(action) { + switch(action.type) { + case MiddlePanelActionTypes.RELOAD_MIDDLE_PANEL: + MiddlePanelStore._datas['articles'] = action.articles; + MiddlePanelStore.emitChange(); + break; + // PARENT FILTER + case MiddlePanelActionTypes.MIDDLE_PANEL_PARENT_FILTER: + MiddlePanelStore.setParentFilter(action.parent_type, + action.filter_id); + break; + // FILTER + case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_ALL: + MiddlePanelStore.setFilter('all'); + break; + case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_UNREAD: + MiddlePanelStore.setFilter('unread'); + break; + case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_LIKED: + MiddlePanelStore.setFilter('liked'); + break; + + + default: + // do nothing + } +}); + +module.exports = MiddlePanelStore; diff --git a/src/web/js/stores/__tests__/TodoStore-test.js b/src/web/js/stores/__tests__/TodoStore-test.js new file mode 100644 index 00000000..6da6cd3c --- /dev/null +++ b/src/web/js/stores/__tests__/TodoStore-test.js @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2014-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * TodoStore-test + */ + +jest.dontMock('../../constants/TodoConstants'); +jest.dontMock('../TodoStore'); +jest.dontMock('object-assign'); + +describe('TodoStore', function() { + + var TodoConstants = require('../../constants/TodoConstants'); + var AppDispatcher; + var TodoStore; + var callback; + + // mock actions + var actionTodoCreate = { + actionType: TodoConstants.TODO_CREATE, + text: 'foo' + }; + var actionTodoDestroy = { + actionType: TodoConstants.TODO_DESTROY, + id: 'replace me in test' + }; + + beforeEach(function() { + AppDispatcher = require('../../dispatcher/AppDispatcher'); + TodoStore = require('../TodoStore'); + callback = AppDispatcher.register.mock.calls[0][0]; + }); + + it('registers a callback with the dispatcher', function() { + expect(AppDispatcher.register.mock.calls.length).toBe(1); + }); + + it('should initialize with no to-do items', function() { + var all = TodoStore.getAll(); + expect(all).toEqual({}); + }); + + it('creates a to-do item', function() { + callback(actionTodoCreate); + var all = TodoStore.getAll(); + var keys = Object.keys(all); + expect(keys.length).toBe(1); + expect(all[keys[0]].text).toEqual('foo'); + }); + + it('destroys a to-do item', function() { + callback(actionTodoCreate); + var all = TodoStore.getAll(); + var keys = Object.keys(all); + expect(keys.length).toBe(1); + actionTodoDestroy.id = keys[0]; + callback(actionTodoDestroy); + expect(all[keys[0]]).toBeUndefined(); + }); + + it('can determine whether all to-do items are complete', function() { + var i = 0; + for (; i < 3; i++) { + callback(actionTodoCreate); + } + expect(Object.keys(TodoStore.getAll()).length).toBe(3); + expect(TodoStore.areAllComplete()).toBe(false); + + var all = TodoStore.getAll(); + for (key in all) { + callback({ + actionType: TodoConstants.TODO_COMPLETE, + id: key + }); + } + expect(TodoStore.areAllComplete()).toBe(true); + + callback({ + actionType: TodoConstants.TODO_UNDO_COMPLETE, + id: key + }); + expect(TodoStore.areAllComplete()).toBe(false); + }); + +}); -- cgit