Skip to content

Commit b7344ef

Browse files
committed
feat: Support .get() alias
1 parent bbc9bc1 commit b7344ef

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ under the hood.
6767
page.input()
6868
```
6969

70+
You can also use the `.get()` method as an alias for calling the method
71+
directly. This helps with readability when using dynamic methods which are
72+
described later.
73+
74+
```javascript
75+
page.input.get()
76+
page.button("name").get()
77+
```
78+
7079
If you want to use the `queryBy*` method (useful for testing element's do not
7180
exist), you can use `.query()`.
7281

src/Collection.test.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { describe, it, expect, beforeEach } from "vitest"
2-
import { Collection } from "./Collection.js"
1+
/* eslint-disable vitest/no-conditional-tests, vitest/no-conditional-in-test */
32
import { cleanup, render } from "@testing-library/react"
4-
import { useState, useEffect } from "react"
3+
import { useEffect, useState } from "react"
4+
import { beforeEach, describe, expect, it } from "vitest"
5+
import { Collection } from "./Collection.js"
56

67
class Page extends Collection {
78
wrapper = this.byTestId("wrapper")
@@ -10,7 +11,7 @@ class Page extends Collection {
1011

1112
const page = new Page()
1213

13-
describe("Collection", () => {
14+
describe("collection", () => {
1415
beforeEach(() => cleanup())
1516

1617
it("should support dynamic methods", () => {
@@ -24,6 +25,17 @@ describe("Collection", () => {
2425
expect(page.name()).toHaveValue("foo")
2526
})
2627

28+
it("should support get as an alias for calling directly", () => {
29+
render(
30+
<div data-testid="wrapper">
31+
<input aria-label="Name" value="foo" onChange={() => {}} />
32+
</div>,
33+
)
34+
35+
expect(page.wrapper.get()).toBeInTheDocument()
36+
expect(page.name.get()).toHaveValue("foo")
37+
})
38+
2739
it("should support query", () => {
2840
render(<div data-testid="wrapper"></div>)
2941

@@ -51,7 +63,7 @@ describe("Collection", () => {
5163
render(<Component />)
5264
expect(page.wrapper()).toBeInTheDocument()
5365
expect(page.name.query()).not.toBeInTheDocument()
54-
expect(await page.name.find()).toBeInTheDocument()
66+
await expect(page.name.find()).resolves.toBeInTheDocument()
5567
})
5668

5769
it("should support all", async () => {
@@ -82,7 +94,7 @@ describe("Collection", () => {
8294
expect(page.wrapper.all()).toHaveLength(2)
8395
expect(page.wrapper.query.all()).toHaveLength(2)
8496
expect(page.name.query.all()).toHaveLength(0)
85-
expect(await page.name.find.all()).toHaveLength(3)
97+
await expect(page.name.find.all()).resolves.toHaveLength(3)
8698
})
8799

88100
it("should support dynamic methods", () => {

src/Collection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type EnhancedQuery<T extends Query> = (
1616
find: (() => ReturnType<Screen[`find${T}`]>) & {
1717
all: () => ReturnType<Screen[`findAll${T}`]>
1818
}
19+
get: (() => ReturnType<Screen[`get${T}`]>) & {
20+
all: () => ReturnType<Screen[`getAll${T}`]>
21+
}
1922
query: (() => ReturnType<Screen[`query${T}`]>) & {
2023
all: () => ReturnType<Screen[`queryAll${T}`]>
2124
}
@@ -57,6 +60,10 @@ export class Collection {
5760
const enhanced: any = () => this.#screen(enhanced, `get${query}`, args)
5861
enhanced.all = () => this.#screen(enhanced.all, `getAll${query}`, args)
5962

63+
enhanced.get = () => this.#screen(enhanced.get, `get${query}`, args)
64+
enhanced.get.all = () =>
65+
this.#screen(enhanced.get.all, `getAll${query}`, args)
66+
6067
enhanced.query = () => this.#screen(enhanced.find, `query${query}`, args)
6168
enhanced.query.all = () =>
6269
this.#screen(enhanced.query.all, `queryAll${query}`, args)

0 commit comments

Comments
 (0)