Skip to content

Commit

Permalink
refactor(test): move helper functions for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 26, 2017
1 parent 64c6c9e commit 8585086
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 57 deletions.
53 changes: 52 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
export * from './wait-for-update'
import Vue from 'vue'

Vue.config.productionTip = false
export { Vue }

export * from './mock'

export function spyUnbind (ref) {
const spy = jest.fn()
const onSnapshot = ref.onSnapshot.bind(ref)
ref.onSnapshot = fn => {
const unbind = onSnapshot(fn)
return () => {
spy()
unbind()
}
}
return spy
}

export function spyOnSnapshot (ref) {
const onSnapshot = ref.onSnapshot.bind(ref)
return (ref.onSnapshot = jest.fn((...args) => onSnapshot(...args)))
}

export function spyOnSnapshotCallback (ref) {
const onSnapshot = ref.onSnapshot.bind(ref)
const spy = jest.fn()
ref.onSnapshot = fn => onSnapshot((...args) => {
spy()
fn(...args)
})
return spy
}

// This makes sure some tests fail by delaying callbacks
export function delayUpdate (ref, time = 0) {
const onSnapshot = ref.onSnapshot.bind(ref)
ref.onSnapshot = fn => onSnapshot(async (...args) => {
await delay(time)
fn(...args)
})
}

export function tick () {
return new Promise((resolve, reject) => {
Vue.nextTick(resolve)
})
}

export function delay (time) {
return new Promise(resolve => setTimeout(resolve, time))
}
19 changes: 0 additions & 19 deletions test/helpers/wait-for-update.js

This file was deleted.

10 changes: 1 addition & 9 deletions test/refs-collections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
db,
tick,
delay,
delayUpdate,
Vue
} from './helpers'

Expand All @@ -26,15 +27,6 @@ beforeEach(async () => {
await tick()
})

// This makes sure some tests fail by delaying callbacks
function delayUpdate (ref, time = 0) {
const onSnapshot = ref.onSnapshot.bind(ref)
ref.onSnapshot = fn => onSnapshot(async (...args) => {
await delay(time)
fn(...args)
})
}

test('binds refs on collections', async () => {
await vm.$bind('items', collection)

Expand Down
31 changes: 3 additions & 28 deletions test/refs-documents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
db,
tick,
delay,
spyUnbind,
spyOnSnapshot,
spyOnSnapshotCallback,
Vue
} from './helpers'

Expand Down Expand Up @@ -40,34 +43,6 @@ beforeEach(async () => {
await delay(5)
})

function spyUnbind (ref) {
const spy = jest.fn()
const onSnapshot = ref.onSnapshot.bind(ref)
ref.onSnapshot = fn => {
const unbind = onSnapshot(fn)
return () => {
spy()
unbind()
}
}
return spy
}

function spyOnSnapshot (ref) {
const onSnapshot = ref.onSnapshot.bind(ref)
return (ref.onSnapshot = jest.fn((...args) => onSnapshot(...args)))
}

function spyOnSnapshotCallback (ref) {
const onSnapshot = ref.onSnapshot.bind(ref)
const spy = jest.fn()
ref.onSnapshot = fn => onSnapshot((...args) => {
spy()
fn(...args)
})
return spy
}

test('binds refs on documents', async () => {
// create an empty doc and update using the ref instead of plain data
const c = collection.doc()
Expand Down

0 comments on commit 8585086

Please sign in to comment.