├── .babelrc ├── .editorconfig ├── .eslintrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src ├── action-handlers │ ├── login.js │ └── register.js ├── actions │ ├── create.js │ ├── destroy.js │ ├── find.js │ ├── login.js │ ├── register.js │ └── update.js ├── index.js └── model.js └── test ├── action-handlers ├── login.spec.js └── register.spec.js ├── actions └── create.spec.js └── index.spec.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. 2 | # Requires EditorConfig JetBrains Plugin - http://github.com/editorconfig/editorconfig-jetbrains 3 | 4 | # Set this file as the topmost .editorconfig 5 | # (multiple files can be used, and are applied starting from current document location) 6 | root = true 7 | 8 | # Use bracketed regexp to target specific file types or file locations 9 | [*.{js,json}] 10 | 11 | # Use hard or soft tabs ["tab", "space"] 12 | indent_style = space 13 | 14 | # Size of a single indent [an integer, "tab"] 15 | indent_size = tab 16 | 17 | # Number of columns representing a tab character [an integer] 18 | tab_width = 2 19 | 20 | # Line breaks representation ["lf", "cr", "crlf"] 21 | end_of_line = lf 22 | 23 | # ["latin1", "utf-8", "utf-16be", "utf-16le"] 24 | charset = utf-8 25 | 26 | # Remove any whitespace characters preceding newline characters ["true", "false"] 27 | trim_trailing_whitespace = true 28 | 29 | # Ensure file ends with a newline when saving ["true", "false"] 30 | insert_final_newline = true -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-airbnb", 3 | "env": { 4 | "mocha": true, 5 | "node": true 6 | }, 7 | "globals": { 8 | "expect": true 9 | }, 10 | "rules": { 11 | "padded-blocks": 0, 12 | "no-use-before-define": [2, "nofunc"], 13 | "no-unused-expressions": 0 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | lib 30 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | cache: 4 | directories: 5 | - node_modules 6 | node_js: 7 | - "4" 8 | - "5" 9 | before_install: 10 | - npm install -g babel-cli -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Tim Perry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Please note this was a spike and does not work. 2 | # Redux loopback middleware 3 | 4 | ## Prerequisites 5 | You will need to create a loopback client. The instructions can be found here: 6 | ``` 7 | https://docs.strongloop.com/display/public/LB/LoopBack+in+the+client 8 | ``` 9 | 10 | ## Setup 11 | 12 | As per the instructions from loopback in the client (see above) you should be able to require your client app in like this: 13 | 14 | ``` 15 | import app from 'loopback-app'; 16 | ``` 17 | 18 | You will also need to bring in this module: 19 | 20 | ``` 21 | import loopbackMiddleware from 'redux-loopback-middleware'; 22 | ``` 23 | 24 | You then need a createStoreWithMiddleware function: 25 | ``` 26 | const createStoreWithMiddleware = applyMiddleware( 27 | loopbackMiddleware(app) 28 | )(createStore); 29 | ``` 30 | 31 | The final result should look something like this: 32 | 33 | ``` 34 | import rootReducer from './reducers'; 35 | import { createStore, applyMiddleware } from 'redux'; 36 | import loopbackMiddleware from 'redux-loopback-middleware'; 37 | import app from 'loopback-app'; 38 | 39 | const createStoreWithMiddleware = applyMiddleware( 40 | loopbackMiddleware(app) 41 | )(createStore); 42 | 43 | const store = createStoreWithMiddleware(rootReducer); 44 | 45 | export default store; 46 | ``` 47 | 48 | ## Usage 49 | 50 | Once you are all setup you can dispatch actions. These actions will call various loopback methods and then dispatch other actions that you should act upon. 51 | 52 | ### Dispatching actions 53 | 54 | ``` 55 | import {actions: {create, find, findById, findOne, updateAll, destroyAll, destroyById}} from 'redux-loopback'; 56 | 57 | create('Landlord', {'name': 'Tim Perry'}); 58 | 59 | find('Landlord', {'name': 'Tim Perry'}); 60 | findById('Landlord', 14); 61 | findOne('Landlord', {'name': 'Tim Perry'}); 62 | 63 | updateAll('Landlord', {'name': 'Tim Perry'}, {'some': 'update'}); 64 | 65 | destroyAll('Landlord', {'name': 'Tim Perry'}); 66 | destroyById('Landlord', 12); 67 | ``` 68 | 69 | ### Handling actions 70 | 71 | All of the actions you dispatch will dispatch a pending action. Then once the data is recieved you will get one of two actions being fired: 72 | - success with the payload of the response 73 | - failure with the payload of the response (the actions will also be identified as an error) 74 | 75 | e.g. for login you would need to handle these three actions: 76 | 77 | #### LOOPBACK_LOGIN_PENDING 78 | ``` 79 | { 80 | type: 'LOOPBACK_LOGIN_PENDING', 81 | payload: // will contain the payload you send when you dispatched the login action 82 | } 83 | ``` 84 | 85 | #### LOOPBACK_LOGIN_SUCCESS 86 | ``` 87 | { 88 | type: 'LOOPBACK_LOGIN_SUCCESS', 89 | payload: // the response from the server 90 | } 91 | ``` 92 | 93 | #### LOOPBACK_LOGIN_ERROR 94 | ``` 95 | { 96 | type: 'LOOPBACK_LOGIN_ERROR', 97 | payload: // the error from the server, 98 | error: true 99 | } 100 | ``` 101 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-loopback", 3 | "version": "0.1.0", 4 | "description": "Loopback middleware for redux", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "lib", 8 | "src" 9 | ], 10 | "scripts": { 11 | "build": "babel src --out-dir lib", 12 | "prepublish": "rimraf lib && npm run build", 13 | "test": "mocha --recursive --compilers js:babel-core/register --reporter spec test/" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/TimPerry/redux-loopback.git" 18 | }, 19 | "homepage": "https://github.com/TimPerry/redux-loopback.git", 20 | "keywords": [ 21 | "redux", 22 | "loopback", 23 | "middleware", 24 | "redux-middleware", 25 | "flux" 26 | ], 27 | "author": "Tim Perry", 28 | "license": "MIT", 29 | "devDependencies": { 30 | "babel": "^6.1.18", 31 | "babel-cli": "^6.2.0", 32 | "babel-core": "^6.2.1", 33 | "babel-eslint": "^5.0.0-beta4", 34 | "babel-plugin-add-module-exports": "^0.1.1", 35 | "babel-preset-es2015": "^6.1.18", 36 | "babel-preset-stage-0": "^6.1.18", 37 | "chai": "^3.2.0", 38 | "chai-spies": "^0.7.1", 39 | "eslint": "^1.10.2", 40 | "eslint-config-airbnb": "1.0.2", 41 | "mocha": "^2.2.5", 42 | "rimraf": "^2.4.3" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/action-handlers/login.js: -------------------------------------------------------------------------------- 1 | import {loginPending, loginSuccess, loginError} from '../actions/login'; 2 | 3 | export default function loginHandler(app, store, action) { 4 | store.dispatch(loginPending()); 5 | const loginDetails = action.payload; 6 | const modelName = action.meta.modelName; 7 | return app.models[modelName].login(loginDetails, (err, res) => { 8 | if (!err) { 9 | store.dispatch(loginSuccess(res)); 10 | if(action.meta.successActions) { 11 | action.meta.successActions.forEach(function(action){ 12 | store.dispatch(action); 13 | }); 14 | } 15 | } else { 16 | store.dispatch(loginError(err)); 17 | if(action.meta.errorActions) { 18 | action.meta.errorActions.forEach(function(action){ 19 | store.dispatch(action); 20 | }); 21 | } 22 | } 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /src/action-handlers/register.js: -------------------------------------------------------------------------------- 1 | import {registerPending, registerSuccess, registerError} from '../actions/register'; 2 | 3 | export default function registerHandler(app, store, action) { 4 | store.dispatch(registerPending()); 5 | const registerDetails = action.payload; 6 | const modelName = action.meta.modelName; 7 | return app.models[modelName].create(registerDetails, (err, res) => { 8 | if (!err) { 9 | store.dispatch(registerSuccess(res)); 10 | } else { 11 | store.dispatch(registerError(err)); 12 | } 13 | }); 14 | } 15 | -------------------------------------------------------------------------------- /src/actions/create.js: -------------------------------------------------------------------------------- 1 | export function create(modeName, payload) { 2 | return { 3 | type: 'LOOPBACK_MODEL_CREATE', 4 | payload: payload, 5 | meta: { 6 | modelName: modeName 7 | } 8 | } 9 | } 10 | 11 | export function createSuccess(modeName, payload) { 12 | return { 13 | type: 'LOOPBACK_MODEL_CREATE_SUCCESS', 14 | payload: payload, 15 | meta: { 16 | modelName: modeName 17 | } 18 | } 19 | } 20 | 21 | export function createError(modeName, payload) { 22 | return { 23 | type: 'LOOPBACK_MODEL_CREATE_ERROR', 24 | payload: payload, 25 | error: true, 26 | meta: { 27 | modelName: modeName 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/actions/destroy.js: -------------------------------------------------------------------------------- 1 | export function loopbackDestroyAll(modeName, payload) { 2 | return { 3 | type: 'LOOPBACK_MODEL_DESTROY_ALL', 4 | payload: payload, 5 | meta: { 6 | modelName: modeName 7 | } 8 | } 9 | } 10 | 11 | export function loopbackDestroyById(modeName, payload) { 12 | return { 13 | type: 'LOOPBACK_MODEL_DESTROY_BY_ID', 14 | payload: payload, 15 | meta: { 16 | modelName: modeName 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/actions/find.js: -------------------------------------------------------------------------------- 1 | export function findById(modeName, modelId, payload) { 2 | return { 3 | type: 'LOOPBACK_MODEL_FIND_BY_ID', 4 | payload: payload, 5 | meta: { 6 | modelName: modeName, 7 | modelId: modelId 8 | } 9 | } 10 | } 11 | 12 | export function findOne(modeName, payload) { 13 | return { 14 | type: 'LOOPBACK_MODEL_FIND_ONE', 15 | payload: payload, 16 | meta: { 17 | modelName: modeName 18 | } 19 | } 20 | } 21 | 22 | export function find(modeName, payload) { 23 | return { 24 | type: 'LOOPBACK_MODEL_FIND', 25 | payload: payload, 26 | meta: { 27 | modelName: modeName 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/actions/login.js: -------------------------------------------------------------------------------- 1 | export function login(email, password, modelName='User', successActions, errorActions) { 2 | return { 3 | type: 'LOOPBACK_LOGIN', 4 | payload: { 5 | email: email, 6 | password: password 7 | }, 8 | meta: { 9 | modelName: modelName, 10 | successActions: successActions, 11 | errorActions: errorActions 12 | } 13 | }; 14 | } 15 | 16 | export function loginPending() { 17 | return { 18 | type: 'LOOPBACK_LOGIN_PENDING' 19 | }; 20 | } 21 | 22 | export function loginSuccess(payload) { 23 | return { 24 | type: 'LOOPBACK_LOGIN_SUCCESS', 25 | payload: payload 26 | } 27 | } 28 | 29 | export function loginError(err) { 30 | return { 31 | type: 'LOOPBACK_LOGIN_ERROR', 32 | payload: err, 33 | error: true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/actions/register.js: -------------------------------------------------------------------------------- 1 | export function register(name, email, password, modelName='User') { 2 | return { 3 | type: 'LOOPBACK_REGISTER', 4 | payload: { 5 | name: name, 6 | email: email, 7 | password: password 8 | }, 9 | meta: { 10 | modelName: modelName 11 | } 12 | }; 13 | } 14 | 15 | export function registerSuccess(payload) { 16 | return { 17 | type: 'LOOPBACK_REGISTER_SUCCESS', 18 | payload: payload 19 | }; 20 | }; 21 | 22 | export function registerPending() { 23 | return { 24 | type: 'LOOPBACK_REGISTER_PENDING' 25 | }; 26 | } 27 | 28 | export function registerError(err) { 29 | return { 30 | type: 'LOOPBACK_REGISTER_ERROR', 31 | payload: err, 32 | error: true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/actions/update.js: -------------------------------------------------------------------------------- 1 | export function loopbackUpdateAll(modeName, payload) { 2 | return { 3 | type: 'LOOPBACK_MODEL_UPDATE_ALL', 4 | payload: payload, 5 | meta: { 6 | modelName: modeName 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {create} from './actions/create'; 2 | import {login} from './actions/login'; 3 | import {find, findById} from './actions/find'; 4 | import {register, registerPending, registerSuccess, registerError} from './actions/register'; 5 | import loginHandler from './action-handlers/login'; 6 | import registerHandler from './action-handlers/register'; 7 | import {singleParamModelMethod, doubleParamModelMethod} from './model'; 8 | 9 | export const actions = { 10 | create: create, 11 | login: login, 12 | register: register, 13 | find: find, 14 | findById: findById 15 | }; 16 | 17 | const actionHandlers = { 18 | 'LOOPBACK_LOGIN': loginHandler, 19 | 'LOOPBACK_REGISTER': registerHandler, 20 | 'LOOPBACK_MODEL_FIND': singleParamModelMethod, 21 | 'LOOPBACK_MODEL_FIND_BY_ID': doubleParamModelMethod 22 | }; 23 | 24 | export default function(app, options={}) { 25 | const loopbackMiddleware = store => next => action => { 26 | if (actionHandlers[action.type]) { 27 | if (options.syncAppWithState) { 28 | options.syncAppWithState(app, store.getState()); 29 | } 30 | actionHandlers[action.type](app, store, action); 31 | } 32 | return next(action); 33 | } 34 | return loopbackMiddleware; 35 | } 36 | -------------------------------------------------------------------------------- /src/model.js: -------------------------------------------------------------------------------- 1 | 2 | const _isNumerical = function(obj) { 3 | obj = obj - 0; 4 | return obj === obj; 5 | }; 6 | 7 | const camelize = function(string) { 8 | if (_isNumerical(string)) { 9 | return string; 10 | } 11 | string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) { 12 | return chr ? chr.toUpperCase() : ''; 13 | }); 14 | // Ensure 1st char is always lowercase 15 | return string.substr(0, 1).toLowerCase() + string.substr(1); 16 | }; 17 | 18 | const modelSuccess = function(action, res) { 19 | return { 20 | type: action.type + '_SUCCESS', 21 | payload: res, 22 | meta: { 23 | originalPayload: action.payload 24 | } 25 | } 26 | }; 27 | 28 | const modelPending = function(action) { 29 | return { 30 | type: action.type + '_PENDING', 31 | meta: { 32 | originalPayload: action.payload 33 | } 34 | } 35 | }; 36 | 37 | const modelError = function(action, err) { 38 | return { 39 | type: action.type + '_ERROR', 40 | payload: err, 41 | error: true, 42 | meta: { 43 | originalPayload: action.payload 44 | } 45 | } 46 | }; 47 | 48 | export function doubleParamModelMethod(app, store, action) { 49 | store.dispatch(modelPending(action)); 50 | const method = camelize(action.type.substr(15).toLowerCase()); 51 | const modelName = action.meta.modelName; 52 | return app.models[modelName][method](action.meta.modelId, action.payload, (err, res) => { 53 | if (!err) { 54 | store.dispatch(modelSuccess(action, res)); 55 | } else { 56 | store.dispatch(modelError(action, err)); 57 | } 58 | }); 59 | } 60 | 61 | export function singleParamModelMethod(app, store, action) { 62 | store.dispatch(modelPending(action)); 63 | const method = camelize(action.type.substr(15).toLowerCase()); 64 | const modelName = action.meta.modelName; 65 | return app.models[modelName][method](action.payload, (err, res) => { 66 | if (!err) { 67 | store.dispatch(modelSuccess(action, res)); 68 | } else { 69 | store.dispatch(modelError(action, err)); 70 | } 71 | }); 72 | } 73 | -------------------------------------------------------------------------------- /test/action-handlers/login.spec.js: -------------------------------------------------------------------------------- 1 | import chai from 'chai'; 2 | import spies from 'chai-spies'; 3 | import loginHandler from '../../src/action-handlers/login.js'; 4 | 5 | chai.use(spies); 6 | const expect = chai.expect; 7 | 8 | describe('Login action handler', function(){ 9 | let store, app, payload; 10 | 11 | beforeEach(function(){ 12 | app = {models: {User: {}}}; 13 | store = {dispatch: chai.spy()}; 14 | payload = {payload: {some: 'payload'}, meta: {modelName: 'User'}} 15 | }); 16 | 17 | it('Should dispatch a pending action', function(){ 18 | app.models.User.login = chai.spy(); 19 | loginHandler(app, store, payload); 20 | expect(store.dispatch).to.have.been.called.with({type: 'LOOPBACK_LOGIN_PENDING'}); 21 | }); 22 | 23 | it('Should dispatch a success action when no error is reported', function(){ 24 | app.models.User.login = function(data, callback) { 25 | callback(undefined, {some: 'data'}); 26 | }; 27 | loginHandler(app, store, payload); 28 | expect(store.dispatch).to.have.been.called.with({ type: 'LOOPBACK_LOGIN_SUCCESS', payload: {some: 'data'}}); 29 | }); 30 | 31 | it('Should dispatch a error action when a error is reported', function(){ 32 | app.models.User.login = function(data, callback) { 33 | callback({some: 'error'}, {some: 'errordata'}); 34 | }; 35 | loginHandler(app, store, payload); 36 | expect(store.dispatch).to.have.been.called.with({ type: 'LOOPBACK_LOGIN_ERROR', payload: {some: 'error'}, error: true}); 37 | }); 38 | 39 | }); 40 | -------------------------------------------------------------------------------- /test/action-handlers/register.spec.js: -------------------------------------------------------------------------------- 1 | import chai from 'chai'; 2 | import spies from 'chai-spies'; 3 | import registerHandler from '../../src/action-handlers/register.js'; 4 | 5 | chai.use(spies); 6 | const expect = chai.expect; 7 | 8 | describe('Register action handler', function(){ 9 | let store, app, payload; 10 | 11 | beforeEach(function(){ 12 | app = {models: {User: {}}}; 13 | store = {dispatch: chai.spy()}; 14 | payload = {payload: {some: 'payload'}, meta: {modelName: 'User'}} 15 | }); 16 | 17 | it('Should dispatch a pending action', function(){ 18 | app.models.User.create = chai.spy(); 19 | registerHandler(app, store, payload); 20 | expect(store.dispatch).to.have.been.called.with({type: 'LOOPBACK_REGISTER_PENDING'}); 21 | }); 22 | 23 | it('Should dispatch a success action when no error is reported', function(){ 24 | app.models.User.create = function(data, callback) { 25 | callback(undefined, {some: 'data'}); 26 | }; 27 | registerHandler(app, store, payload); 28 | expect(store.dispatch).to.have.been.called.with({ type: 'LOOPBACK_REGISTER_SUCCESS', payload: {some: 'data'}}); 29 | }); 30 | 31 | it('Should dispatch a error action when a error is reported', function(){ 32 | app.models.User.create = function(data, callback) { 33 | callback({some: 'error'}, {some: 'errordata'}); 34 | }; 35 | registerHandler(app, store, payload); 36 | expect(store.dispatch).to.have.been.called.with({ type: 'LOOPBACK_REGISTER_ERROR', payload: {some: 'error'}, error: true}); 37 | }); 38 | 39 | }); 40 | -------------------------------------------------------------------------------- /test/actions/create.spec.js: -------------------------------------------------------------------------------- 1 | import {create, createSuccess, createError} from '../../src/actions/create'; 2 | import {expect} from 'chai'; 3 | 4 | describe('Create Actions Creators', function() { 5 | 6 | describe('create', function() { 7 | 8 | const payload = { 9 | some: 'data' 10 | } 11 | const result = create('modelName', payload); 12 | 13 | it('should set the payload type to "LOOPBACK_MODEL_CREATE"', function() { 14 | expect(result.type).to.not.be.undefined; 15 | expect(result.type).to.equal('LOOPBACK_MODEL_CREATE'); 16 | }); 17 | 18 | it('should set the attach the provided modelName to the metadata', function() { 19 | expect(result.meta).to.not.be.undefined; 20 | expect(result.meta.modelName).to.equal('modelName'); 21 | }); 22 | 23 | it('should set the actions payload to the given payload', function() { 24 | expect(result.payload).to.not.be.undefined; 25 | expect(result.payload).to.deep.equal(payload); 26 | }); 27 | 28 | }); 29 | 30 | describe('createSuccess', function() { 31 | 32 | const payload = { 33 | some: 'data' 34 | } 35 | const result = createSuccess('modelName', payload); 36 | 37 | it('should set the payload type to "LOOPBACK_MODEL_CREATE_SUCCESS"', function() { 38 | expect(result.type).to.not.be.undefined; 39 | expect(result.type).to.equal('LOOPBACK_MODEL_CREATE_SUCCESS'); 40 | }); 41 | 42 | it('should set the attach the provided modelName to the metadata', function() { 43 | expect(result.meta).to.not.be.undefined; 44 | expect(result.meta.modelName).to.equal('modelName'); 45 | }); 46 | 47 | it('should set the actions payload to the given payload', function() { 48 | expect(result.payload).to.not.be.undefined; 49 | expect(result.payload).to.deep.equal(payload); 50 | }); 51 | 52 | }); 53 | 54 | describe('createError', function() { 55 | 56 | const payload = { 57 | some: 'data' 58 | } 59 | const result = createError('modelName', payload); 60 | 61 | it('should set the payload type to "LOOPBACK_MODEL_CREATE_ERROR"', function() { 62 | expect(result.type).to.not.be.undefined; 63 | expect(result.type).to.equal('LOOPBACK_MODEL_CREATE_ERROR'); 64 | }); 65 | 66 | it('should set the attach the provided modelName to the metadata', function() { 67 | expect(result.meta).to.not.be.undefined; 68 | expect(result.meta.modelName).to.equal('modelName'); 69 | }); 70 | 71 | it('should set the actions payload to the given payload', function() { 72 | expect(result.payload).to.not.be.undefined; 73 | expect(result.payload).to.deep.equal(payload); 74 | }); 75 | 76 | it('should set the error to true', function() { 77 | expect(result.error).to.not.be.undefined; 78 | expect(result.error).to.equal(true); 79 | }); 80 | 81 | }); 82 | 83 | }); 84 | -------------------------------------------------------------------------------- /test/index.spec.js: -------------------------------------------------------------------------------- 1 | import loopbackMiddleware from '../src/index.js'; 2 | import {actions} from '../src/index.js'; 3 | import chai from 'chai'; 4 | 5 | const expect = chai.expect; 6 | 7 | describe('Index', function(){ 8 | 9 | describe('has actions', function(){ 10 | 11 | ['create', 'login', 'register'].forEach(function(action){ 12 | 13 | it(action, function(){ 14 | expect(actions[action]).to.not.be.undefined; 15 | }) 16 | 17 | }); 18 | 19 | }); 20 | 21 | }); 22 | --------------------------------------------------------------------------------