Skip to content

Commit

Permalink
Get initial tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallswain committed Mar 13, 2017
1 parent cf95f9f commit 46115ee
Show file tree
Hide file tree
Showing 24 changed files with 748 additions and 319 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"plugins": [ "add-module-exports" ],
"presets": [ "es2015", "stage-2" ]
"presets": [ "es2015", "stage-2"]
}
1 change: 1 addition & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--compilers js:babel-core/register
--debug-brk
test/index.test.js
39 changes: 28 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"release:major": "npm version major && npm publish",
"changelog": "github_changelog_generator && git add CHANGELOG.md && git commit -am \"Updating changelog\"",
"compile": "shx rm -rf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"watch": "shx rm -rf lib/ && babel --watch -d lib/ src/",
"lint": "standard src/**/*.js test/**/*.js --fix",
"mocha": "mocha --opts mocha.opts",
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --opts mocha.opts",
Expand All @@ -46,13 +46,28 @@
"mocha"
]
},
"steal": {
"map": {
"assert": "chai/chai"
},
"meta": {
"chai/chai": {
"format": "global",
"exports": "chai.assert"
}
},
"plugins": [
"chai"
]
},
"directories": {
"lib": "lib"
},
"dependencies": {
"debug": "^2.6.1",
"deep-assign": "^2.0.0",
"feathers-errors": "^2.5.0",
"rubberduck": "^1.1.1",
"standard": "^9.0.0"
},
"devDependencies": {
Expand All @@ -63,21 +78,23 @@
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"body-parser": "^1.16.1",
"chai": "^3.5.0",
"feathers": "^2.0.3",
"body-parser": "^1.17.0",
"chai": "3.4.1",
"feathers": "^2.1.1",
"feathers-authentication": "pre",
"feathers-authentication-client": "^0.1.8",
"feathers-authentication-client": "^0.1.10",
"feathers-authentication-jwt": "^0.3.1",
"feathers-hooks": "^1.7.1",
"feathers-hooks": "^1.8.1",
"feathers-memory": "^1.1.0",
"feathers-rest": "^1.6.0",
"feathers-socketio": "^1.4.2",
"feathers-rest": "^1.7.1",
"feathers-socketio": "^1.5.2",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.2.0",
"shx": "^0.2.2",
"socket.io-client": "^1.7.2",
"vue": "^2.2.0-beta.1",
"vuex": "^2.1.2"
"socket.io-client": "^1.7.3",
"steal": "^1.2.10",
"steal-mocha": "^1.0.0",
"vue": "^2.2.1",
"vuex": "^2.2.1"
}
}
Empty file added src/auth-module/auth-module.js
Empty file.
Empty file added src/auth.js
Empty file.
4 changes: 2 additions & 2 deletions src/feathers-module/feathers-module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function setupFeathersModule (store, options) {
return feathers => store.registerModule(options.feathersModuleName, {
namespaced: true,
getters: {
services: () => feathers.services
state: {
services: feathers.services
}
})
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import rubberduck from 'rubberduck'
import rubberduck from 'rubberduck/dist/rubberduck'
import setupServiceModule from './service-module/service-module'
import setupFeathersModule from './feathers-module/feathers-module'
import deepAssign from 'deep-assign'
import { normalizePath } from './utils'

const defaultOptions = {
idField: 'id',
Expand Down Expand Up @@ -41,6 +42,7 @@ export default function (clientOrStore, options = {}, modules = {}) {
const addVuexMethod = function (service, options, modules) {
if (typeof service.vuex !== 'function') {
service.vuex = function (moduleOptions) {
normalizePath(service)
// options passed to .vuex() will overwrite the previous options.
deepAssign(modules[service.path], moduleOptions)

Expand All @@ -65,6 +67,7 @@ export default function (clientOrStore, options = {}, modules = {}) {
global: options,
modules: modules
}
normalizePath(service)
// Make any service-specific config available on the service.
if (modules[service.path]) {
service.vuexOptions.module = modules[service.path]
Expand Down
34 changes: 27 additions & 7 deletions src/service-module/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ export default function mapActions (service) {
const { vuexOptions } = service
const idField = vuexOptions.module.idField || vuexOptions.global.idField
return {
find ({ dispatch }, params) {
service.find(params)
find ({ commit, dispatch }, params) {
commit('setPending')
return service.find(params)
.then(response => {
commit('unsetPending')
let data = response.data || response
data.map(item => dispatch('addOrUpdate', item))
return response
})
},

Expand All @@ -19,31 +22,48 @@ export default function mapActions (service) {
id = params
params = undefined
}
service.get(id, params)
commit('setPending')
return service.get(id, params)
.then(item => {
commit('unsetPending')
dispatch('addOrUpdate', item)
commit('setCurrent', item)
return item
})
},

create ({ commit, dispatch }, data) {
service.create(data)
commit('setPending')
return service.create(data)
.then(item => {
commit('unsetPending')
dispatch('addOrUpdate', item)
commit('setCurrent', item)
return item
})
},

update ({ dispatch }, id, data) {
update ({ commit, dispatch }, id, data) {
commit('setPending')
commit('unsetPending')
console.log(id)
console.log(data)
},

patch () {},
remove () {},
patch ({ commit, dispatch }) {
commit('setPending')
commit('unsetPending')
},
remove ({ commit, dispatch }) {
commit('setPending')
commit('unsetPending')
},

addOrUpdate ({ state, commit }, item) {
let id = item[idField]
if (id === undefined) {
throw new Error('No id found for item. Did you set the idField?', item)
}
let existing = state.keyedById[id]
existing ? commit('updateItem', item) : commit('addItem', item)
}
Expand Down
10 changes: 7 additions & 3 deletions src/service-module/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ export default function mapMutations (service) {
state.currentId = id
state.copy = deepAssign({}, payload)
},

clearCurrent (state) {
state.currentId = undefined
state.copy = undefined
},

// Deep assigns current to copy
rejectCopy (state) {
let current = state.keyedById[state.currentId]
deepAssign(state.copy, current)
},

// Deep assigns copy to current
commitCopy (state) {
let current = state.keyedById[state.currentId]
deepAssign(current, state.copy)
},

setPending (state) {
state.isPending = true
},
unsetPending (state) {
state.isPending = false
}
}
}
8 changes: 7 additions & 1 deletion src/service-module/service-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function setupServiceModule (store) {
// update the name
deepAssign(service.vuexOptions, { module: {name} })
vuexOptions.modules[service.path] = vuexOptions.module
const idField = (vuexOptions.module && vuexOptions.module.idField) || vuexOptions.global.idField

// Setup or re-setup the module if .vuex() was called manually.
if (!store.state[name] || force) {
Expand All @@ -33,7 +34,12 @@ export default function setupServiceModule (store) {
ids: [],
keyedById: {},
currentId: undefined,
copy: undefined
copy: undefined,
service,
idField,
isPending: false,
isError: false,
error: undefined
},
getters: mapGetters(service),
mutations: mapMutations(service),
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ export function stripSlashes (name) {
return name.replace(/^(\/*)|(\/*)$/g, '')
}

export function normalizePath (service) {
service.path = service.path || service.name
return service
}

export function upperCaseFirst (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
Expand Down
10 changes: 5 additions & 5 deletions test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('feathers-vuex:auth - Reducer', () => {
it('Has defaults', () => {
const state = undefined
const defaultState = {
isLoading: false,
isPending: false,
isError: false,
isSignedIn: false,
accessToken: undefined,
Expand All @@ -105,7 +105,7 @@ describe('feathers-vuex:auth - Reducer', () => {
}
}
const expectedState = {
isLoading: true,
isPending: true,
isError: false,
isSignedIn: false,
accessToken: undefined,
Expand All @@ -123,7 +123,7 @@ describe('feathers-vuex:auth - Reducer', () => {
data: { accessToken }
}
const expectedState = {
isLoading: false,
isPending: false,
isError: false,
isSignedIn: true,
accessToken: accessToken,
Expand All @@ -141,7 +141,7 @@ describe('feathers-vuex:auth - Reducer', () => {
error
}
const expectedState = {
isLoading: false,
isPending: false,
isError: true,
isSignedIn: false,
accessToken: undefined,
Expand All @@ -157,7 +157,7 @@ describe('feathers-vuex:auth - Reducer', () => {
type: actionTypes.FEATHERS_AUTH_LOGOUT
}
const expectedState = {
isLoading: false,
isPending: false,
isError: false,
isSignedIn: false,
accessToken: undefined,
Expand Down
20 changes: 20 additions & 0 deletions test/feathers-module/feathers-module.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from 'chai/chai'
import feathersVuex from '~/src/index'
import makeStore from '../fixtures/store'
import { makeFeathersRestClient } from '../fixtures/feathers-client'

describe('Feathers Module', () => {
it('The feathers module gets added to the store', () => {
const store = makeStore()
makeFeathersRestClient().configure(feathersVuex(store))
assert.deepEqual({services: {}}, store.state.feathers)
})

it('populates with new services', () => {
const store = makeStore()
const feathersClient = makeFeathersRestClient()
.configure(feathersVuex(store))
var todoService = feathersClient.service('api/todos')
assert(store.state.feathers.services['api/todos'] === todoService)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import socketio from 'feathers-socketio/client'
import rest from 'feathers-rest/client'
import axios from 'axios'
import auth from 'feathers-authentication-client'
import io from 'socket.io-client'
import io from 'socket.io-client/dist/socket.io'

const baseUrl = 'http://localhost:3030'

Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions test/fixtures/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default function makeStore () {
return new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
}
11 changes: 11 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Feathers-Vuex tests</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="../node_modules/steal/steal.js" mocha="bdd" main="test/index.test"></script>
</body>
</html>
Loading

0 comments on commit 46115ee

Please sign in to comment.