Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wrapper array): Allow negative indices to be passed to .at() #1244

Merged
merged 1 commit into from
Jan 16, 2020
Merged
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
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)
})
})