Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Dec 21, 2023
1 parent df081f5 commit 58db797
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 56 deletions.
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![module](https://img.shields.io/badge/module-ESM%2FCJS-blue)](README.md)
[![license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)

Helpers for working with the DOM, mostly for testing.
Helpers for working with the DOM.

## install
```sh
Expand All @@ -14,20 +14,65 @@ npm i -D @nichoth/dom
## use

### import

```js
import { dom } from '@nichoth/dom'
```

### require

```js
const dom = require('@nichoth/dom')
const dom = require('@nichoth/dom').dom
```

## API

### dom.waitFor
Look for a DOM element by slector. Default timeout is 5 seconds.

```ts
function waitFor (args:{
selector?:string,
visible?:boolean,
timeout?:number
}, lambda?:() => Element|null)
```

#### example
```js
const foundElement = await dom.waitFor({
selector: 'p'
})
```

### dom.waitForText
Look for an element containing the given text. Default timeout is 5 seconds.

```ts
function waitForText (args:{
text?:string,
timeout?:number,
element:Element,
multipleTags?:boolean,
regex?:RegExp
}):Promise<Element>
```

#### example
```js
const el = await dom.waitForText({
element: document.body,
regex: /bar/
})
```

Pass in a parent element and timeout.

```js
const found = await dom.waitForText({
element: dom.qs('#test-two'),
multipleTags: true,
text: 'bbb',
timeout: 1000
})
```

## credits
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"tap-arc": "^1.2.2",
"tape-run": "^11.0.0",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"xterm": "^5.3.0"
},
"exports": {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const SECOND = 1000

export const dom = {
defaultTimeout: (5 * SECOND),
DEFAULT_TIMEOUT: (5 * SECOND),
getComputedStyle,
isElementVisible,
waitForText,
Expand Down Expand Up @@ -97,7 +97,7 @@ function waitForText (args:{
element:Element,
multipleTags?:boolean,
regex?:RegExp
}) {
}):Promise<Element> {
return waitFor({
timeout: args.timeout
}, () => {
Expand Down Expand Up @@ -175,12 +175,12 @@ function waitFor (args:{
selector?:string,
visible?:boolean,
timeout?:number
}, lambda?:() => Element|null) {
}, lambda?:() => Element|null):Promise<Element> {
return new Promise((resolve, reject) => {
const {
selector,
visible = true,
timeout = dom.defaultTimeout
timeout = dom.DEFAULT_TIMEOUT
} = args

if (!lambda && selector) {
Expand Down
4 changes: 0 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @ts-check
'use strict'

const { test } = require('@nichoth/tapzero')
const dom = require('../dist/index.cjs').dom
const { Terminal } = require('xterm')
Expand Down Expand Up @@ -198,7 +195,6 @@ test('another case for text + tags', async t => {

try {
const found = await dom.waitForText({
// @ts-ignore
element: dom.qs('#test-two'),
multipleTags: true,
text: 'bbb',
Expand Down
43 changes: 0 additions & 43 deletions vite.config.js

This file was deleted.

0 comments on commit 58db797

Please sign in to comment.