Skip to content

Commit 7064ab0

Browse files
committed
chore: Move enableAutoDestroy() to separate module
1 parent 7dd4d1d commit 7064ab0

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @flow
2+
3+
import { throwError } from 'shared/util'
4+
import Wrapper from './wrapper'
5+
6+
let wrapperInstances: Array<Wrapper>
7+
8+
export function enableAutoDestroy(hook: (() => void) => void) {
9+
if (wrapperInstances) {
10+
throwError('enableAutoDestroy cannot be called more than once')
11+
}
12+
13+
wrapperInstances = []
14+
15+
hook(() => {
16+
wrapperInstances.forEach((wrapper: Wrapper) => {
17+
wrapper.destroy()
18+
})
19+
wrapperInstances = []
20+
})
21+
}
22+
23+
export function trackInstance(wrapper: Wrapper) {
24+
if (!wrapperInstances) return
25+
26+
wrapperInstances.push(wrapper)
27+
}

packages/test-utils/src/create-wrapper.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
import Vue from 'vue'
44
import Wrapper from './wrapper'
55
import VueWrapper from './vue-wrapper'
6+
import { trackInstance } from './auto-destroy'
67

78
export default function createWrapper(
89
node: VNode | Component,
910
options: WrapperOptions = {}
1011
): VueWrapper | Wrapper {
1112
const componentInstance = node.child
1213
if (componentInstance) {
13-
return new VueWrapper(componentInstance, options)
14+
const wrapper = new VueWrapper(componentInstance, options)
15+
trackInstance(wrapper)
16+
return wrapper
1417
}
15-
return node instanceof Vue
16-
? new VueWrapper(node, options)
17-
: new Wrapper(node, options)
18+
const wrapper =
19+
node instanceof Vue
20+
? new VueWrapper(node, options)
21+
: new Wrapper(node, options)
22+
trackInstance(wrapper)
23+
return wrapper
1824
}

packages/test-utils/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import shallowMount from './shallow-mount'
22
import mount from './mount'
3+
import { enableAutoDestroy } from './auto-destroy'
34
import createLocalVue from './create-local-vue'
45
import RouterLinkStub from './components/RouterLinkStub'
56
import createWrapper from './create-wrapper'
6-
import Wrapper, { enableAutoDestroy } from './wrapper'
7+
import Wrapper from './wrapper'
78
import WrapperArray from './wrapper-array'
89
import config from './config'
910
import { warn } from 'shared/util'

packages/test-utils/src/wrapper.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export default class Wrapper implements BaseWrapper {
2525
isFunctionalComponent: boolean
2626
rootNode: VNode | Element
2727
selector: Selector | void
28-
static _instances: ?Array<Wrapper>
2928

3029
constructor(
3130
node: VNode | Element,
@@ -69,10 +68,6 @@ export default class Wrapper implements BaseWrapper {
6968
) {
7069
this.isFunctionalComponent = true
7170
}
72-
73-
if (Wrapper._instances) {
74-
Wrapper._instances.push(this)
75-
}
7671
}
7772

7873
at(): void {
@@ -608,23 +603,3 @@ export default class Wrapper implements BaseWrapper {
608603
this.element.dispatchEvent(event)
609604
}
610605
}
611-
612-
export const enableAutoDestroy = (hook: Function) => {
613-
if (Wrapper._instances) {
614-
throwError('enableAutoDestroy cannot be called more than once')
615-
}
616-
617-
Wrapper._instances = []
618-
619-
hook(() => {
620-
const { _instances } = Wrapper
621-
if (!_instances) {
622-
return
623-
}
624-
625-
while (_instances.length > 0) {
626-
const wrapper = _instances.pop()
627-
wrapper.destroy()
628-
}
629-
})
630-
}

test/specs/wrapper.spec.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describeWithShallowAndMount } from '~resources/utils'
2-
import { Wrapper, enableAutoDestroy } from '~vue/test-utils'
2+
import { enableAutoDestroy } from '~vue/test-utils'
33

44
describeWithShallowAndMount('Wrapper', mountingMethod => {
55
;['vnode', 'element', 'vm', 'options'].forEach(property => {
@@ -20,10 +20,6 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
2020
describe('enableAutoDestroy', () => {
2121
const sandbox = sinon.createSandbox()
2222

23-
beforeEach(() => {
24-
Wrapper._instances = undefined
25-
})
26-
2723
it('calls the hook function', () => {
2824
const hookSpy = sandbox.spy()
2925

0 commit comments

Comments
 (0)