Skip to content

Commit

Permalink
feat(wrapper array): Allow negative indices to be passed to .at()
Browse files Browse the repository at this point in the history
Passing a negative index to .at() lets you retrieve elements by starting from the end of the wrapper
array (i.e. last item is at index -1)
  • Loading branch information
paulgv committed Oct 31, 2019
1 parent ad602fc commit 996d3e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion docs/api/wrapper-array/at.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## at

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).

- **Arguments:**

Expand All @@ -16,6 +17,10 @@ import Foo from './Foo.vue'

const wrapper = shallowMount(Foo)
const divArray = wrapper.findAll('div')

const secondDiv = divArray.at(1)
expect(secondDiv.is('p')).toBe(true)
expect(secondDiv.is('div')).toBe(true)

const lastDiv = divArray.at(-1)
expect(lastDiv.is('div')).toBe(true)
```
9 changes: 6 additions & 3 deletions packages/test-utils/src/wrapper-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ export default class WrapperArray implements BaseWrapper {
}

at(index: number): Wrapper | VueWrapper {
if (index > this.length - 1) {
throwError(`no item exists at ${index}`)
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[index]
return this.wrappers[normalizedIndex]
}

attributes(): void {
Expand Down
28 changes: 28 additions & 0 deletions test/specs/wrapper-array/at.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ describeWithShallowAndMount('at', mountingMethod => {
expect(p.classes()).to.contain('index-1')
})

it('returns Wrapper at index from the end when index is negative', () => {
const TestComponent = {
template: '<div><p class="index-first" /><p class="index-last"/></div>'
}
const all = mountingMethod(TestComponent).findAll('p')
const last = all.at(-1)
const first = all.at(-2)
expect(last.vnode).to.be.an('object')
expect(last.classes()).to.contain('index-last')
expect(first.vnode).to.be.an('object')
expect(first.classes()).to.contain('index-first')
})

it('throws error if no item exists at index', () => {
const index = 2
const TestComponent = {
Expand All @@ -27,4 +40,19 @@ describeWithShallowAndMount('at', mountingMethod => {
.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)
})
})

0 comments on commit 996d3e5

Please sign in to comment.