Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Commit

Permalink
🐛 Fix mutations in namespaced modules
Browse files Browse the repository at this point in the history
Closes #72

Signed-off-by: Eduardo San Martin Morote <[email protected]>
  • Loading branch information
posva committed May 14, 2017
1 parent 98afc75 commit 844e080
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 138 deletions.
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Object.keys(types).forEach(key => {
}
})

const commitOptions = { root: true }

function bindAsObject ({
key,
source,
Expand All @@ -32,7 +34,7 @@ function bindAsObject ({
key,
record: createRecord(snapshot),
state,
})
}, commitOptions)
}, cancelCallback)

// return the listeners that have been setup
Expand All @@ -51,7 +53,7 @@ function bindAsArray ({
type: types.VUEXFIRE_ARRAY_INITIALIZE,
state,
key,
})
}, commitOptions)
const onAdd = source.on('child_added', function (snapshot, prevKey) {
const array = state[key]
const index = prevKey ? indexForKey(array, prevKey) + 1 : 0
Expand All @@ -61,7 +63,7 @@ function bindAsArray ({
key,
index,
record: createRecord(snapshot),
})
}, commitOptions)
}, cancelCallback)

const onRemove = source.on('child_removed', function (snapshot) {
Expand All @@ -72,7 +74,7 @@ function bindAsArray ({
state,
key,
index,
})
}, commitOptions)
}, cancelCallback)

const onChange = source.on('child_changed', function (snapshot) {
Expand All @@ -84,7 +86,7 @@ function bindAsArray ({
key,
index,
record: createRecord(snapshot),
})
}, commitOptions)
}, cancelCallback)

const onMove = source.on('child_moved', function (snapshot, prevKey) {
Expand All @@ -100,7 +102,7 @@ function bindAsArray ({
index,
newIndex,
record: createRecord(snapshot),
})
}, commitOptions)
}, cancelCallback)

// return the listeners that have been setup
Expand Down
45 changes: 36 additions & 9 deletions test/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,37 @@ test.before(t => {
Vue.use(Vuex)
})

const itemsActions = {
setItemsRef: firebaseAction(({ bindFirebaseRef }, ref) => {
bindFirebaseRef('items', ref)
}),
unbindItemsRef: firebaseAction(({ unbindFirebaseRef }) => {
unbindFirebaseRef('items')
}),
}

test.beforeEach(t => {
t.context.store = new Vuex.Store({
mutations: firebaseMutations,
modules: {
named: {
namespaced: true,
state: { items: [] },
actions: itemsActions,
},
todos: {
state: {
items: [],
options: null,
},
actions: {
setItemsRef: firebaseAction(({ bindFirebaseRef }, ref) => {
bindFirebaseRef('items', ref)
}),
actions: Object.assign({
setOptionsRef: firebaseAction(({ bindFirebaseRef }, ref) => {
bindFirebaseRef('options', ref)
}),
unbindItemsRef: firebaseAction(({ unbindFirebaseRef }) => {
unbindFirebaseRef('items')
}),
unbindOptionsRef: firebaseAction(({ unbindFirebaseRef }) => {
unbindFirebaseRef('options')
}),
},
mutations: firebaseMutations,
}, itemsActions),
},
},
})
Expand Down Expand Up @@ -65,3 +73,22 @@ test('binds an array to a module', t => {
t.context.ref.flush()
t.deepEqual(t.context.store.state.todos.items[0].index, 3)
})

test('works on namespaced modules', t => {
t.context.store.dispatch('named/setItemsRef', t.context.ref)
t.context.ref.set({
first: { index: 0 },
second: { index: 1 },
third: { index: 2 },
})
t.context.ref.flush()

t.deepEqual(t.context.store.state.named.items, [
{ '.key': 'first', index: 0 },
{ '.key': 'second', index: 1 },
{ '.key': 'third', index: 2 },
])
t.context.ref.child('first').child('index').set(3)
t.context.ref.flush()
t.deepEqual(t.context.store.state.named.items[0].index, 3)
})
Loading

0 comments on commit 844e080

Please sign in to comment.