fix(ios): respect atIndex in getAttributes (#4912)

The `attributes` property on `Element` was ignoring `self.index`,
causing `element(...).atIndex(N).getAttributes()` to return all
matching elements instead of the one at the specified index.

This aligns the `attributes` property with the existing `view` property,
which already correctly handles `self.index`.

Fixes #4633

Co-authored-by: mark.dev <[email protected]>
This commit is contained in:
vazidmansuri005
2026-05-11 12:57:10 +03:00
committed by GitHub
co-authored by mark.dev
parent 1158066bdb
commit 924d0f2271
2 changed files with 30 additions and 3 deletions
+8 -3
View File
@@ -313,14 +313,19 @@ class Element : NSObject {
@objc
var attributes: [String : Any] {
let views = self.views
if views.count == 1 {
if let index = index {
guard index < views.count else {
dtx_fatalError("Index \(index) beyond bounds \(views.count > 0 ? "[0 .. \(views.count - 1)] " : " ")for "\(self.description)"", viewDescription: failDebugAttributes)
}
return views[index].dtx_attributes
} else if views.count == 1 {
return views.first!.dtx_attributes
} else {
let elements = views.map {
return $0.dtx_attributes
}
return ["elements": elements]
}
}
+22
View File
@@ -220,6 +220,28 @@ describe('Attributes', () => {
});
});
describe('of multiple views with atIndex', () => {
it(':ios: @legacy should return attributes of a single element when using atIndex', async () => {
const result = await element(by.type('RCTView').withAncestor(by.id('attrScrollView'))).atIndex(0).getAttributes();
expect(result).not.toHaveProperty('elements');
expect(result).toMatchObject({
enabled: true,
visible: true,
});
});
it(':ios: @new-arch should return attributes of a single element when using atIndex', async () => {
const result = await element(by.type('RCTViewComponentView')).atIndex(0).getAttributes();
expect(result).not.toHaveProperty('elements');
expect(result).toMatchObject({
enabled: true,
visible: true,
});
});
});
describe('of multiple views', () => {
it(':ios: @legacy should return an object with .elements array', async () => {
await useMatcher(by.type('RCTView').withAncestor(by.id('attrScrollView')));