Skip to content

Commit 5950f50

Browse files
committed
feat: change WrapperArray.wrappers and WrapperArray.length to read-only
1 parent b801c25 commit 5950f50

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

Diff for: docs/api/wrapper-array/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ A `WrapperArray` is an object that contains an array of [`Wrappers`](../wrapper/
44

55
## Properties
66

7-
### `wrappers`
7+
### `wrappers`
88

9-
`array`: the `Wrappers` contained in the `WrapperArray`
9+
`array` (read-only): the `Wrappers` contained in the `WrapperArray`
1010

11-
### `length`
11+
### `length`
1212

13-
`number`: the number of `Wrappers` contained in the `WrapperArray`
13+
`number` (read-only): the number of `Wrappers` contained in the `WrapperArray`
1414

1515
## Methods
1616

Diff for: docs/api/wrapper/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ A `Wrapper` is an object that contains a mounted component or vnode and methods
1414

1515
`HTMLElement` (read-only): the root DOM node of the wrapper
1616

17-
### `options`
17+
### `options`
1818

1919
#### `options.attachedToDocument`
2020

2121
`Boolean` (read-only): True if `attachedToDocument` in mounting options was `true`
2222

23-
#### `options.sync`
23+
#### `options.sync`
2424

2525
`Boolean` (read-only): True if `sync` in mounting options was not `false`
2626

Diff for: packages/test-utils/src/wrapper-array.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ import type VueWrapper from './vue-wrapper'
55
import { throwError, warn } from 'shared/util'
66

77
export default class WrapperArray implements BaseWrapper {
8-
wrappers: Array<Wrapper | VueWrapper>;
9-
length: number;
8+
+wrappers: Array<Wrapper | VueWrapper>;
9+
+length: number;
1010

1111
constructor (wrappers: Array<Wrapper | VueWrapper>) {
12-
this.wrappers = wrappers || []
13-
this.length = this.wrappers.length
12+
const length = wrappers.length
13+
// $FlowIgnore
14+
Object.defineProperty(this, 'wrappers', {
15+
get: () => wrappers,
16+
set: () => {}
17+
})
18+
// $FlowIgnore
19+
Object.defineProperty(this, 'length', {
20+
get: () => length,
21+
set: () => {}
22+
})
1423
}
1524

1625
at (index: number): Wrapper | VueWrapper {

Diff for: packages/test-utils/src/wrapper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ export default class Wrapper implements BaseWrapper {
814814
*/
815815
destroy () {
816816
if (!this.isVueInstance()) {
817-
throwError(`wrapper.destroy() can only be called on a Vue ` + `instance`)
817+
throwError(`wrapper.destroy() can only be called on a Vue instance`)
818818
}
819819

820820
if (this.element.parentNode) {

Diff for: test/specs/wrapper-array.spec.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => {
77
const wrapper = mountingMethod(compiled)
88
const wrapperArray = wrapper.findAll('p')
99
expect(wrapperArray.constructor.name).to.equal('WrapperArray')
10-
if (wrappers) {
11-
wrapperArray.wrappers = wrappers
12-
wrapperArray.length = wrappers.length
13-
}
14-
return wrapperArray
10+
return wrappers ? new wrapperArray.constructor(wrappers) : wrapperArray
1511
}
1612

1713
it('returns class with length equal to length of wrappers passed in constructor', () => {
@@ -67,8 +63,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => {
6763
if (method === 'at') {
6864
return
6965
}
70-
const wrapperArray = getWrapperArray()
71-
wrapperArray.wrappers = []
66+
const wrapperArray = getWrapperArray([])
7267
const message = `[vue-test-utils]: ${method} cannot be called on 0 items`
7368
expect(() => wrapperArray[method]())
7469
.to.throw()
@@ -99,8 +94,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => {
9994
) {
10095
return
10196
}
102-
const wrapperArray = getWrapperArray()
103-
wrapperArray.wrappers = [1, 2, 3]
97+
const wrapperArray = getWrapperArray([1, 2, 3])
10498
const message = `[vue-test-utils]: ${method} must be called on a single wrapper, use at(i) to access a wrapper`
10599
expect(() => wrapperArray[method]())
106100
.to.throw()

0 commit comments

Comments
 (0)