Skip to content

Commit bcecda2

Browse files
committed
fix issues in controlled mode, add cypress tests
1 parent 2c7982e commit bcecda2

File tree

8 files changed

+283
-161
lines changed

8 files changed

+283
-161
lines changed

vuu-ui/cypress/support/component/assertions.ts

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ declare global {
147147
```
148148
* */
149149
(chainer: "not.be.activeDescendant"): Chainable<Subject>;
150+
151+
/**
152+
* Checks if there is a text selection in effect that
153+
* matches the expectation.
154+
*
155+
* @example
156+
```
157+
cy.findByTestId('test-input).should('have.selectionRange',0, 2)
158+
```
159+
* */
160+
(
161+
chainer: "have.selectionRange",
162+
from: number,
163+
to: number,
164+
): Chainable<Subject>;
150165
}
151166
}
152167
}
@@ -163,7 +178,7 @@ const isHighlighted: ChaiPlugin = (_chai) => {
163178
// make sure it's an Element
164179
new _chai.Assertion(
165180
root.nodeType,
166-
`Expected an Element but got '${String(root)}'`
181+
`Expected an Element but got '${String(root)}'`,
167182
).to.equal(1);
168183

169184
const className = this._obj.attr("class");
@@ -173,7 +188,7 @@ const isHighlighted: ChaiPlugin = (_chai) => {
173188
`expected root to include CSS class #{exp}, got #{act} instead.`,
174189
`expected root not to have class #{exp}.`,
175190
"saltHighlighted",
176-
className
191+
className,
177192
);
178193
}
179194

@@ -195,7 +210,7 @@ const hasFocusVisible: ChaiPlugin = (_chai) => {
195210
// make sure it's an Element
196211
new _chai.Assertion(
197212
root.nodeType,
198-
`Expected an Element but got '${String(root)}'`
213+
`Expected an Element but got '${String(root)}'`,
199214
).to.equal(1);
200215

201216
const className = this._obj.attr("class");
@@ -205,7 +220,7 @@ const hasFocusVisible: ChaiPlugin = (_chai) => {
205220
`expected root to include CSS class #{exp}, got #{act} instead.`,
206221
`expected root not to have class #{exp}.`,
207222
"saltFocusVisible",
208-
className
223+
className,
209224
);
210225
}
211226

@@ -227,7 +242,7 @@ const hasAriaSelected: ChaiPlugin = (_chai) => {
227242
// make sure it's an Element
228243
new _chai.Assertion(
229244
root.nodeType,
230-
`Expected an Element but got '${String(root)}'`
245+
`Expected an Element but got '${String(root)}'`,
231246
).to.equal(1);
232247

233248
const ariaSelected = this._obj.attr("aria-selected");
@@ -237,7 +252,7 @@ const hasAriaSelected: ChaiPlugin = (_chai) => {
237252
`expected root to have aria-selected #{exp}, got #{act} instead.`,
238253
`expected root to have aria-selected = #{exp}, got #{act} instead`,
239254
"true",
240-
ariaSelected
255+
ariaSelected,
241256
);
242257
}
243258

@@ -259,7 +274,7 @@ const isInTheViewport: ChaiPlugin = (_chai, utils) => {
259274
// make sure it's an Element
260275
new _chai.Assertion(
261276
root.nodeType,
262-
`Expected an Element but got '${String(root)}'`
277+
`Expected an Element but got '${String(root)}'`,
263278
).to.equal(1);
264279

265280
const viewportHeight = Cypress.config(`viewportHeight`);
@@ -269,7 +284,7 @@ const isInTheViewport: ChaiPlugin = (_chai, utils) => {
269284
!(rect.bottom < 0 || rect.top - viewportHeight >= 0),
270285
`expected \n${elementToString(root)} to be in the viewport.`,
271286
`expected \n${elementToString(root)} to not be in the viewport`,
272-
null
287+
null,
273288
);
274289
}
275290

@@ -293,7 +308,7 @@ const isViewportSize: ChaiPlugin = (_chai, utils) => {
293308
// make sure it's an Element
294309
new _chai.Assertion(
295310
root.nodeType,
296-
`Expected an Element but got '${String(root)}'`
311+
`Expected an Element but got '${String(root)}'`,
297312
).to.equal(1);
298313

299314
const viewportHeight = Cypress.config(`viewportHeight`);
@@ -303,12 +318,12 @@ const isViewportSize: ChaiPlugin = (_chai, utils) => {
303318
this.assert(
304319
rect.height === viewportHeight && rect.width === viewportWidth,
305320
`expected \n${elementToString(
306-
root
321+
root,
307322
)} to be sized to fill viewport (${viewportWidth} x ${viewportHeight}), actual height ${
308323
rect.height
309324
}, width: ${rect.width} .`,
310325
`expected \n${elementToString(root)} to not be sized to fill viewport.`,
311-
null
326+
null,
312327
);
313328
}
314329

@@ -331,7 +346,7 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
331346
// make sure it's an Element
332347
new _chai.Assertion(
333348
root.nodeType,
334-
`Expected an Element but got '${String(root)}'`
349+
`Expected an Element but got '${String(root)}'`,
335350
).to.equal(1);
336351

337352
const id = root.id;
@@ -340,7 +355,7 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
340355
$focused.attr("aria-activedescendant") === id,
341356
"expected #{this} to be #{exp}",
342357
"expected #{this} not to be #{exp}",
343-
"active descendant"
358+
"active descendant",
344359
);
345360
});
346361
}
@@ -351,4 +366,39 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
351366
// registers our assertion function "isFocused" with Chai
352367
chai.use(isActiveDescendant);
353368

369+
/**
370+
* Checks if the element text includes a text selection range
371+
*
372+
* @example
373+
* cy.findByTestId('test-input).should('have.selectionRange',0,3)
374+
*/
375+
const hasSelectionRange: ChaiPlugin = (_chai, utils) => {
376+
function assertHasSelectedRange(
377+
this: AssertionStatic,
378+
rangeFrom: number,
379+
rangeTo: number,
380+
) {
381+
const root = this._obj.get(0);
382+
// make sure it's an Element
383+
new _chai.Assertion(
384+
root.nodeType,
385+
`Expected an Element but got '${String(root)}'`,
386+
).to.equal(1);
387+
388+
const { selectionStart, selectionEnd } = root;
389+
390+
this.assert(
391+
rangeFrom === selectionStart && rangeTo === selectionEnd,
392+
`expected root to have selectionRange from:${rangeFrom} to: ${rangeTo}, got from: ${selectionStart} to: ${selectionEnd} instead.`,
393+
`expected root not to have selectionRange from:${rangeFrom} to: ${rangeTo}`,
394+
"selectedRange",
395+
);
396+
}
397+
398+
_chai.Assertion.addMethod("selectionRange", assertHasSelectedRange);
399+
};
400+
401+
// registers our assertion function "hasSelectedRange" with Chai
402+
chai.use(hasSelectionRange);
403+
354404
export {};

vuu-ui/packages/vuu-ui-controls/src/__tests__/__component__/time-input/TimeInput.cy.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ describe("TimeInput", () => {
2929
cy.findByTestId("pre-timeinput").find("input").focus();
3030
cy.realPress("Tab");
3131
cy.findByTestId("timeinput").should("be.focused");
32+
cy.findByTestId("timeinput").should("have.selectionRange", 0, 2);
33+
});
34+
describe("WHEN left/right arrow keys used", () => {
35+
it("THEN right arrow key shifts selection right", () => {
36+
cy.mount(<TestTimeInput defaultValue="00:00:00" />);
37+
cy.findByTestId("pre-timeinput").find("input").focus();
38+
cy.realPress("Tab");
39+
cy.realPress("ArrowRight");
40+
cy.findByTestId("timeinput").should("have.selectionRange", 3, 5);
41+
cy.realPress("ArrowRight");
42+
cy.findByTestId("timeinput").should("have.selectionRange", 6, 8);
43+
cy.realPress("ArrowRight");
44+
cy.findByTestId("timeinput").should("have.selectionRange", 6, 8);
45+
});
46+
it("THEN left arrow key shifts selection left", () => {
47+
cy.mount(<TestTimeInput defaultValue="00:00:00" />);
48+
cy.findByTestId("pre-timeinput").find("input").focus();
49+
cy.realPress("Tab");
50+
cy.realPress("ArrowRight");
51+
cy.realPress("ArrowRight");
52+
cy.findByTestId("timeinput").should("have.selectionRange", 6, 8);
53+
cy.realPress("ArrowLeft");
54+
cy.findByTestId("timeinput").should("have.selectionRange", 3, 5);
55+
cy.realPress("ArrowLeft");
56+
cy.findByTestId("timeinput").should("have.selectionRange", 0, 2);
57+
cy.realPress("ArrowLeft");
58+
cy.findByTestId("timeinput").should("have.selectionRange", 0, 2);
59+
});
3260
});
3361
});
3462
});

0 commit comments

Comments
 (0)