Skip to content

Commit e4887ae

Browse files
committed
Added new waitFor helpers
1 parent 97399e4 commit e4887ae

File tree

4 files changed

+113
-37
lines changed

4 files changed

+113
-37
lines changed

test/helpers/docsify-init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global jestPlaywright page */
22
import mock, { proxy } from 'xhr-mock';
3-
import { waitForSelector } from './wait-for-selector';
3+
import { waitForSelector } from './wait-for';
44

55
const axios = require('axios');
66
const prettier = require('prettier');

test/helpers/wait-for-selector.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/helpers/wait-for.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const defaults = {
2+
delay: 100,
3+
timeout: 4000,
4+
};
5+
6+
/**
7+
* Waits for specified function to resolve to a truthy value.
8+
*
9+
* @param {Function} fn function to be evaluated until truthy
10+
* @param {*} arg optional argument to pass to `fn`
11+
* @param {Object} options optional parameters
12+
* @returns {Promise} promise which resolves to function result
13+
*/
14+
export function waitForFunction(fn, arg, options = {}) {
15+
const settings = {
16+
...defaults,
17+
...options,
18+
};
19+
20+
return new Promise((resolve, reject) => {
21+
let timeElapsed = 0;
22+
23+
const int = setInterval(() => {
24+
const result = fn(arg);
25+
26+
if (result) {
27+
clearInterval(int);
28+
resolve(result);
29+
}
30+
31+
timeElapsed += settings.delay;
32+
33+
if (timeElapsed >= settings.timeout) {
34+
reject(
35+
`waitForFunction() did not return a truthy value\n\n${fn.toString()}\n\n`
36+
);
37+
}
38+
}, settings.delay);
39+
});
40+
}
41+
42+
/**
43+
* Waits for specified CSS selector to be located in the DOM
44+
*
45+
* @param {String} cssSelector CSS selector to query for
46+
* @param {Object} options optional parameters
47+
* @returns {Promise} promise which resolves to first matching element
48+
*/
49+
export function waitForSelector(cssSelector, options = {}) {
50+
const settings = {
51+
...defaults,
52+
...options,
53+
};
54+
55+
return new Promise((resolve, reject) => {
56+
let timeElapsed = 0;
57+
58+
const int = setInterval(() => {
59+
const elm = document.querySelector(cssSelector);
60+
if (elm) {
61+
clearInterval(int);
62+
resolve(elm);
63+
}
64+
65+
timeElapsed += settings.delay;
66+
67+
if (timeElapsed >= settings.timeout) {
68+
reject(
69+
`waitForSelector() was unable to find CSS selector ${cssSelector}`
70+
);
71+
}
72+
}, settings.delay);
73+
});
74+
}
75+
76+
/**
77+
* Waits for specified CSS selector to contain text content
78+
*
79+
* @param {String} cssSelector CSS selector to query for
80+
* @param {String} text text to match
81+
* @param {Object} options optional parameters
82+
* @returns {Promise} promise which resolves to first matching element that contains specified text
83+
*/
84+
export function waitForText(cssSelector, text, options = {}) {
85+
const settings = {
86+
...defaults,
87+
...options,
88+
};
89+
90+
return new Promise((resolve, reject) => {
91+
waitForSelector(cssSelector, settings)
92+
.then(elm => {
93+
const isMatch = elm.textContent.includes(text);
94+
95+
if (isMatch) {
96+
resolve(elm);
97+
} else {
98+
reject(
99+
`waitForText() did not find "${text}" in CSS selector ${cssSelector}`
100+
);
101+
}
102+
})
103+
.catch(() => {
104+
reject(`waitForText() was unable to find CSS selector ${cssSelector}`);
105+
});
106+
});
107+
}

test/integration/example.test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { waitForSelector } from '../helpers/wait-for-selector';
1+
import { waitForFunction, waitForText } from '../helpers/wait-for';
22

33
const docsifyInit = require('../helpers/docsify-init');
44

@@ -55,8 +55,6 @@ describe('Example Tests', function() {
5555
# Test Page
5656
5757
This is a custom route.
58-
59-
<div id="test">Hello</div>
6058
`,
6159
'/data-test-scripturls.js': `
6260
document.body.setAttribute('data-test-scripturls', 'pass');
@@ -144,10 +142,9 @@ describe('Example Tests', function() {
144142

145143
// Verify docsify navigation and docsifyInitConfig.routes
146144
document.querySelector('a[href="#/test"]').click();
147-
await waitForSelector('#test');
148-
expect(window.location.href).toMatch(/\/test$/);
149-
expect(document.querySelector('#main').textContent).toContain(
150-
'This is a custom route'
151-
);
145+
expect(
146+
await waitForFunction(() => /#\/test$/.test(window.location.href))
147+
).toBeTruthy();
148+
expect(await waitForText('#main', 'This is a custom route')).toBeTruthy();
152149
});
153150
});

0 commit comments

Comments
 (0)