Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/wrapper-array/at.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Returns `Wrapper` at `index` passed. Uses zero based numbering (i.e. first item is at index 0).
If `index` is negative, indexing starts from the last element (i.e. last item is at index -1).
When none is found, returns an `ErrorWrapper`.

- **Arguments:**

Expand All @@ -23,4 +24,7 @@ expect(secondDiv.is('div')).toBe(true)

const lastDiv = divArray.at(-1)
expect(lastDiv.is('div')).toBe(true)

const nonExistentDiv = divArray.at(1000)
expect(nonExistentDiv.exists()).toBe(false)
```
10 changes: 3 additions & 7 deletions packages/test-utils/src/wrapper-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type Wrapper from './wrapper'
import type VueWrapper from './vue-wrapper'
import { throwError } from 'shared/util'
import ErrorWrapper from './error-wrapper'

export default class WrapperArray implements BaseWrapper {
+wrappers: Array<Wrapper | VueWrapper>
Expand All @@ -23,14 +24,9 @@ export default class WrapperArray implements BaseWrapper {
})
}

at(index: number): Wrapper | VueWrapper {
at(index: number): Wrapper | VueWrapper | ErrorWrapper {
const normalizedIndex = index < 0 ? this.length + index : index
if (normalizedIndex > this.length - 1 || normalizedIndex < 0) {
let error = `no item exists at ${index}`
error += index < 0 ? ` (normalized to ${normalizedIndex})` : ''
throwError(error)
}
return this.wrappers[normalizedIndex]
return this.wrappers[normalizedIndex] || new ErrorWrapper('')
}

attributes(): void {
Expand Down
31 changes: 5 additions & 26 deletions test/specs/wrapper-array/at.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,12 @@ describeWithShallowAndMount('at', mountingMethod => {
expect(first.classes()).to.contain('index-first')
})

it('throws error if no item exists at index', () => {
const index = 2
it('returns ErrorWrapper at index when not found', () => {
const TestComponent = {
template: '<div><p /><p class="index-1"/></div>'
template: '<div></div>'
}
const message = `[vue-test-utils]: no item exists at ${index}`
expect(() =>
mountingMethod(TestComponent)
.findAll('p')
.at(index)
)
.to.throw()
.with.property('message', message)
})

it('throws error if no item exists at negative index', () => {
const index = -3
const TestComponent = {
template: '<div><p /><p class="index-1"/></div>'
}
const message = `[vue-test-utils]: no item exists at -3 (normalized to -1)`
expect(() =>
mountingMethod(TestComponent)
.findAll('p')
.at(index)
)
.to.throw()
.with.property('message', message)
const all = mountingMethod(TestComponent).findAll('p')
const nonExistent = all.at(0)
expect(nonExistent.exists()).to.equal(false)
})
})