diff --git a/README.md b/README.md index 8424d61..13ae3ab 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 +``` + +#### 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 diff --git a/package.json b/package.json index ea80844..caf1ec7 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 58e4d61..0e14717 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ const SECOND = 1000 export const dom = { - defaultTimeout: (5 * SECOND), + DEFAULT_TIMEOUT: (5 * SECOND), getComputedStyle, isElementVisible, waitForText, @@ -97,7 +97,7 @@ function waitForText (args:{ element:Element, multipleTags?:boolean, regex?:RegExp -}) { +}):Promise { return waitFor({ timeout: args.timeout }, () => { @@ -175,12 +175,12 @@ function waitFor (args:{ selector?:string, visible?:boolean, timeout?:number -}, lambda?:() => Element|null) { +}, lambda?:() => Element|null):Promise { return new Promise((resolve, reject) => { const { selector, visible = true, - timeout = dom.defaultTimeout + timeout = dom.DEFAULT_TIMEOUT } = args if (!lambda && selector) { diff --git a/test/index.ts b/test/index.ts index c2275f3..a23b624 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,6 +1,3 @@ -// @ts-check -'use strict' - const { test } = require('@nichoth/tapzero') const dom = require('../dist/index.cjs').dom const { Terminal } = require('xterm') @@ -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', diff --git a/vite.config.js b/vite.config.js deleted file mode 100644 index fbe1de5..0000000 --- a/vite.config.js +++ /dev/null @@ -1,43 +0,0 @@ -import { defineConfig } from 'vite' -import preact from '@preact/preset-vite' -import postcssNesting from 'postcss-nesting' - -// https://vitejs.dev/config/ -export default defineConfig({ - define: { - global: 'globalThis' - }, - root: 'example', - plugins: [ - preact({ - devtoolsInProd: false, - prefreshEnabled: true, - babel: { - sourceMaps: 'both' - } - }) - ], - // https://github.com/vitejs/vite/issues/8644#issuecomment-1159308803 - esbuild: { - logOverride: { 'this-is-undefined-in-esm': 'silent' } - }, - publicDir: '_public', - css: { - postcss: { - plugins: [ - postcssNesting - ], - }, - }, - server: { - port: 8888, - host: true, - open: true, - }, - build: { - minify: false, - outDir: '../public', - emptyOutDir: true, - sourcemap: 'inline' - } -})