-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(collections): Add mock + tests for simple collections
- Loading branch information
Showing
6 changed files
with
245 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function createSnapshot (doc) { | ||
// defaults everything to false, so no need to set | ||
return Object.defineProperty(doc.data(), 'id', { | ||
value: doc.id, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import test from 'ava' | ||
import Vuefire from '../src' | ||
import { | ||
createSnapshot | ||
} from '../src/utils' | ||
import { | ||
db, | ||
tick, | ||
Vue | ||
} from './helpers' | ||
|
||
Vue.use(Vuefire) | ||
|
||
test.beforeEach(async t => { | ||
t.context.collection = db.collection() | ||
t.context.vm = new Vue({ | ||
render (h) { | ||
return h('ul', this.items.map( | ||
item => h('li', [item]) | ||
)) | ||
}, | ||
data: () => ({ items: null }), | ||
firestore: { | ||
items: t.context.collection | ||
} | ||
}).$mount() | ||
await tick() | ||
}) | ||
|
||
test('initialise the array', t => { | ||
t.deepEqual(t.context.vm.items, []) | ||
}) | ||
|
||
test('add elements', async t => { | ||
await t.context.collection.add({ text: 'foo' }) | ||
t.deepEqual(t.context.vm.items, [{ text: 'foo' }]) | ||
await t.context.collection.add({ text: 'bar' }) | ||
t.deepEqual(t.context.vm.items, [{ text: 'foo' }, { text: 'bar' }]) | ||
}) | ||
|
||
test('delets items', async t => { | ||
await t.context.collection.add({ text: 'foo' }) | ||
await t.context.collection.doc(t.context.vm.items[0].id).delete() | ||
t.deepEqual(t.context.vm.items, []) | ||
}) | ||
|
||
test('update items', async t => { | ||
const doc = await t.context.collection.add({ text: 'foo' }) | ||
await doc.update({ text: 'bar' }) | ||
t.deepEqual(t.context.vm.items[0], { text: 'bar' }) | ||
}) | ||
|
||
test('add properties', async t => { | ||
const doc = await t.context.collection.add({ text: 'foo' }) | ||
await doc.update({ other: 'bar' }) | ||
t.deepEqual(t.context.vm.items[0], { text: 'foo', other: 'bar' }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
import test from 'ava' | ||
import Vuefire from '../src' | ||
import { tick, Vue } from './helpers' | ||
import { collection } from './helpers' | ||
import { | ||
createSnapshot | ||
} from '../src/utils' | ||
import { | ||
db, | ||
tick, | ||
Vue | ||
} from './helpers' | ||
|
||
Vue.use(Vuefire) | ||
|
||
const vm = new Vue({ | ||
render (h) { | ||
return h('ul', this.items.map( | ||
item => h('li', [item]) | ||
)) | ||
}, | ||
data: () => ({ items: [] }), | ||
firestore: { | ||
items: collection | ||
} | ||
}).$mount() | ||
|
||
Vue.use(Vuefire) | ||
|
||
test('foo', async t => { | ||
test.beforeEach(async t => { | ||
t.context.collection = db.collection() | ||
t.context.document = t.context.collection.doc() | ||
t.context.vm = new Vue({ | ||
render (h) { | ||
return h('ul', this.items.map( | ||
item => h('li', [item]) | ||
)) | ||
}, | ||
// purposely set items as null | ||
// but it's a good practice to set it to an empty array | ||
data: () => ({ items: null }), | ||
firestore: { | ||
items: t.context.collection, | ||
item: t.context.document | ||
} | ||
}).$mount() | ||
await tick() | ||
t.deepEqual(vm.items, []) | ||
t.pass() | ||
}) | ||
|
||
test('bar', async t => { | ||
const bar = Promise.resolve('bar') | ||
test('detects collections', async t => { | ||
await t.context.document.update({ foo: 'foo' }) | ||
t.deepEqual(t.context.vm.item, { foo: 'foo' }) | ||
}) | ||
|
||
test('detects documents', async t => { | ||
await t.context.collection.add({ foo: 'foo' }) | ||
t.deepEqual(t.context.vm.items, [{ foo: 'foo' }]) | ||
}) | ||
|
||
t.is(await bar, 'bar') | ||
test('does nothing with no firestore', t => { | ||
const vm = new Vue({ | ||
render (h) { | ||
return h('ul', this.items.map( | ||
item => h('li', [item]) | ||
)) | ||
}, | ||
data: () => ({ items: null }), | ||
}) | ||
t.deepEqual(t.context.vm.items, null) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import test from 'ava' | ||
import { | ||
createSnapshot, | ||
} from '../src/utils' | ||
import { | ||
collection, | ||
Key, | ||
id, | ||
DocumentSnapshot | ||
} from './helpers' | ||
|
||
test.beforeEach(t => { | ||
t.context.id = id | ||
t.context.doc = new DocumentSnapshot(null, new Key(), { | ||
n: 42, | ||
is: true, | ||
items: [{ text: 'foo' }] | ||
}) | ||
t.context.snapshot = createSnapshot(t.context.doc) | ||
}) | ||
|
||
test('createSnapshot adds an id', t => { | ||
t.is(t.context.snapshot.id, t.context.id) | ||
}) | ||
|
||
test('id is not enumerable', t => { | ||
t.false(Object.keys(t.context.snapshot).includes('id')) | ||
}) | ||
|
||
test('contains all the data', t => { | ||
t.deepEqual(t.context.snapshot, { | ||
n: 42, | ||
is: true, | ||
items: [{ text: 'foo' }] | ||
}) | ||
}) |