Skip to content

Commit 8585086

Browse files
committed
refactor(test): move helper functions for tests
1 parent 64c6c9e commit 8585086

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

test/helpers/index.js

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
1-
export * from './wait-for-update'
1+
import Vue from 'vue'
2+
3+
Vue.config.productionTip = false
4+
export { Vue }
5+
26
export * from './mock'
7+
8+
export function spyUnbind (ref) {
9+
const spy = jest.fn()
10+
const onSnapshot = ref.onSnapshot.bind(ref)
11+
ref.onSnapshot = fn => {
12+
const unbind = onSnapshot(fn)
13+
return () => {
14+
spy()
15+
unbind()
16+
}
17+
}
18+
return spy
19+
}
20+
21+
export function spyOnSnapshot (ref) {
22+
const onSnapshot = ref.onSnapshot.bind(ref)
23+
return (ref.onSnapshot = jest.fn((...args) => onSnapshot(...args)))
24+
}
25+
26+
export function spyOnSnapshotCallback (ref) {
27+
const onSnapshot = ref.onSnapshot.bind(ref)
28+
const spy = jest.fn()
29+
ref.onSnapshot = fn => onSnapshot((...args) => {
30+
spy()
31+
fn(...args)
32+
})
33+
return spy
34+
}
35+
36+
// This makes sure some tests fail by delaying callbacks
37+
export function delayUpdate (ref, time = 0) {
38+
const onSnapshot = ref.onSnapshot.bind(ref)
39+
ref.onSnapshot = fn => onSnapshot(async (...args) => {
40+
await delay(time)
41+
fn(...args)
42+
})
43+
}
44+
45+
export function tick () {
46+
return new Promise((resolve, reject) => {
47+
Vue.nextTick(resolve)
48+
})
49+
}
50+
51+
export function delay (time) {
52+
return new Promise(resolve => setTimeout(resolve, time))
53+
}

test/helpers/wait-for-update.js

-19
This file was deleted.

test/refs-collections.spec.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
db,
44
tick,
55
delay,
6+
delayUpdate,
67
Vue
78
} from './helpers'
89

@@ -26,15 +27,6 @@ beforeEach(async () => {
2627
await tick()
2728
})
2829

29-
// This makes sure some tests fail by delaying callbacks
30-
function delayUpdate (ref, time = 0) {
31-
const onSnapshot = ref.onSnapshot.bind(ref)
32-
ref.onSnapshot = fn => onSnapshot(async (...args) => {
33-
await delay(time)
34-
fn(...args)
35-
})
36-
}
37-
3830
test('binds refs on collections', async () => {
3931
await vm.$bind('items', collection)
4032

test/refs-documents.spec.js

+3-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
db,
44
tick,
55
delay,
6+
spyUnbind,
7+
spyOnSnapshot,
8+
spyOnSnapshotCallback,
69
Vue
710
} from './helpers'
811

@@ -40,34 +43,6 @@ beforeEach(async () => {
4043
await delay(5)
4144
})
4245

43-
function spyUnbind (ref) {
44-
const spy = jest.fn()
45-
const onSnapshot = ref.onSnapshot.bind(ref)
46-
ref.onSnapshot = fn => {
47-
const unbind = onSnapshot(fn)
48-
return () => {
49-
spy()
50-
unbind()
51-
}
52-
}
53-
return spy
54-
}
55-
56-
function spyOnSnapshot (ref) {
57-
const onSnapshot = ref.onSnapshot.bind(ref)
58-
return (ref.onSnapshot = jest.fn((...args) => onSnapshot(...args)))
59-
}
60-
61-
function spyOnSnapshotCallback (ref) {
62-
const onSnapshot = ref.onSnapshot.bind(ref)
63-
const spy = jest.fn()
64-
ref.onSnapshot = fn => onSnapshot((...args) => {
65-
spy()
66-
fn(...args)
67-
})
68-
return spy
69-
}
70-
7146
test('binds refs on documents', async () => {
7247
// create an empty doc and update using the ref instead of plain data
7348
const c = collection.doc()

0 commit comments

Comments
 (0)