Skip to content

Commit 4ad1c42

Browse files
author
Ben Monro
committed
fix: handle count > 1 for within
1 parent d5f179b commit 4ad1c42

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export const within = async selector => {
6767
}
6868

6969
if (selector.constructor.name === SELECTOR_TYPE) {
70+
const count = await selector.count;
71+
if (count > 1) {
72+
throw new Error(`within() requires a single element, found ${count}`);
73+
}
7074
const el = await selector;
7175
const withinQueryName = el.getAttribute('data-tctl-queryname');
7276

tests/testcafe/within.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,20 @@ test('works with nested selector from "All" query with index - function', async
7070

7171
await t.expect(nested.getByText('Button Text').exists).ok();
7272
});
73+
74+
75+
test('should throw error if count > 1', async t => {
76+
const nestedDivs = getAllByTestId(/nested/);
77+
78+
await t.expect(nestedDivs.count).eql(2);
79+
let didThrow = false;
80+
try {
81+
await within(nestedDivs);
82+
} catch (e) {
83+
didThrow = true;
84+
}
85+
await t.expect(didThrow).ok();
86+
});
87+
88+
89+

0 commit comments

Comments
 (0)