@rvoh/psychic-spec-helpers 0.2.0 → 0.3.1-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/esm/src/feature/helpers/launchBrowser.js +1 -1
  2. package/dist/esm/src/feature/helpers/providePuppeteerViteMatchers.js +24 -24
  3. package/dist/esm/src/feature/helpers/visit.js +1 -3
  4. package/dist/esm/src/feature/matchers/toCheck.js +15 -20
  5. package/dist/esm/src/feature/matchers/toClick.js +14 -21
  6. package/dist/esm/src/feature/matchers/toClickButton.js +14 -21
  7. package/dist/esm/src/feature/matchers/toClickLink.js +14 -21
  8. package/dist/esm/src/feature/matchers/toClickSelector.js +14 -21
  9. package/dist/esm/src/feature/matchers/toFill.js +14 -17
  10. package/dist/esm/src/feature/matchers/toHaveChecked.js +13 -14
  11. package/dist/esm/src/feature/matchers/toHaveLink.js +14 -22
  12. package/dist/esm/src/feature/matchers/toHavePath.js +3 -1
  13. package/dist/esm/src/feature/matchers/toHaveSelector.js +17 -11
  14. package/dist/esm/src/feature/matchers/toHaveUnchecked.js +13 -14
  15. package/dist/esm/src/feature/matchers/toMatchTextContent.js +19 -13
  16. package/dist/esm/src/feature/matchers/toNotHaveSelector.js +7 -5
  17. package/dist/esm/src/feature/matchers/toNotMatchTextContent.js +3 -1
  18. package/dist/esm/src/feature/matchers/toUncheck.js +22 -19
  19. package/dist/types/src/feature/helpers/visit.d.ts +2 -4
  20. package/dist/types/src/feature/matchers/toCheck.d.ts +6 -3
  21. package/dist/types/src/feature/matchers/toClick.d.ts +6 -3
  22. package/dist/types/src/feature/matchers/toClickButton.d.ts +6 -3
  23. package/dist/types/src/feature/matchers/toClickLink.d.ts +6 -3
  24. package/dist/types/src/feature/matchers/toClickSelector.d.ts +6 -3
  25. package/dist/types/src/feature/matchers/toFill.d.ts +3 -3
  26. package/dist/types/src/feature/matchers/toHaveChecked.d.ts +6 -3
  27. package/dist/types/src/feature/matchers/toHaveLink.d.ts +3 -3
  28. package/dist/types/src/feature/matchers/toHaveSelector.d.ts +3 -3
  29. package/dist/types/src/feature/matchers/toHaveUnchecked.d.ts +3 -3
  30. package/dist/types/src/feature/matchers/toMatchTextContent.d.ts +5 -3
  31. package/dist/types/src/feature/matchers/toNotHaveSelector.d.ts +1 -1
  32. package/dist/types/src/feature/matchers/toUncheck.d.ts +8 -2
  33. package/dist/types/src/index.d.ts +3 -1
  34. package/package.json +2 -1
  35. package/src/feature/helpers/launchBrowser.ts +1 -1
  36. package/src/feature/helpers/providePuppeteerViteMatchers.ts +29 -25
  37. package/src/feature/helpers/visit.ts +1 -5
  38. package/src/feature/matchers/toCheck.ts +20 -30
  39. package/src/feature/matchers/toClick.ts +20 -26
  40. package/src/feature/matchers/toClickButton.ts +20 -26
  41. package/src/feature/matchers/toClickLink.ts +20 -26
  42. package/src/feature/matchers/toClickSelector.ts +20 -26
  43. package/src/feature/matchers/toFill.ts +21 -25
  44. package/src/feature/matchers/toHaveChecked.ts +23 -22
  45. package/src/feature/matchers/toHaveLink.ts +19 -27
  46. package/src/feature/matchers/toHavePath.ts +3 -1
  47. package/src/feature/matchers/toHaveSelector.ts +22 -18
  48. package/src/feature/matchers/toHaveUnchecked.ts +23 -22
  49. package/src/feature/matchers/toMatchTextContent.ts +23 -19
  50. package/src/feature/matchers/toNotHaveSelector.ts +7 -5
  51. package/src/feature/matchers/toNotMatchTextContent.ts +3 -1
  52. package/src/feature/matchers/toUncheck.ts +28 -29
  53. package/src/index.ts +3 -1
@@ -3,7 +3,7 @@ export default async function launchBrowser(opts) {
3
3
  return await puppeteer.launch({
4
4
  browser: 'firefox',
5
5
  dumpio: process.env.DEBUG === '1',
6
- headless: true,
6
+ headless: process.env.HEADLESS !== '0',
7
7
  ...opts,
8
8
  });
9
9
  }
@@ -18,32 +18,32 @@ import toFill from '../matchers/toFill.js';
18
18
  export default function providePuppeteerViteMatchers() {
19
19
  ;
20
20
  global.expect.extend({
21
- async toMatchTextContent(page, text) {
22
- return await toMatchTextContent(page, text);
21
+ async toMatchTextContent(page, text, opts) {
22
+ return await toMatchTextContent(page, text, opts);
23
23
  },
24
24
  async toNotMatchTextContent(page, text) {
25
25
  return await toNotMatchTextContent(page, text);
26
26
  },
27
- async toHaveSelector(page, cssSelector) {
28
- return await toHaveSelector(page, cssSelector);
27
+ async toHaveSelector(page, cssSelector, opts) {
28
+ return await toHaveSelector(page, cssSelector, opts);
29
29
  },
30
30
  async toNotHaveSelector(page, cssSelector) {
31
31
  return await toNotHaveSelector(page, cssSelector);
32
32
  },
33
- async toCheck(page, text) {
34
- return await toCheck(page, text);
33
+ async toCheck(page, text, opts) {
34
+ return await toCheck(page, text, opts);
35
35
  },
36
- async toClick(page, text) {
37
- return await toClick(page, text);
36
+ async toClick(page, text, opts) {
37
+ return await toClick(page, text, opts);
38
38
  },
39
- async toClickLink(page, text) {
40
- return await toClickLink(page, text);
39
+ async toClickLink(page, text, opts) {
40
+ return await toClickLink(page, text, opts);
41
41
  },
42
- async toClickButton(page, text) {
43
- return await toClickButton(page, text);
42
+ async toClickButton(page, text, opts) {
43
+ return await toClickButton(page, text, opts);
44
44
  },
45
- async toClickSelector(page, cssSelector) {
46
- return await toClickSelector(page, cssSelector);
45
+ async toClickSelector(page, cssSelector, opts) {
46
+ return await toClickSelector(page, cssSelector, opts);
47
47
  },
48
48
  async toHavePath(page, path) {
49
49
  return await toHavePath(page, path);
@@ -51,20 +51,20 @@ export default function providePuppeteerViteMatchers() {
51
51
  async toHaveUrl(page, url) {
52
52
  return await toHaveUrl(page, url);
53
53
  },
54
- async toHaveChecked(page, text) {
55
- return await toHaveChecked(page, text);
54
+ async toHaveChecked(page, text, opts) {
55
+ return await toHaveChecked(page, text, opts);
56
56
  },
57
- async toHaveUnchecked(page, checked) {
58
- return await toHaveUnchecked(page, checked);
57
+ async toHaveUnchecked(page, checked, opts) {
58
+ return await toHaveUnchecked(page, checked, opts);
59
59
  },
60
- async toHaveLink(page, text) {
61
- return await toHaveLink(page, text);
60
+ async toHaveLink(page, text, opts) {
61
+ return await toHaveLink(page, text, opts);
62
62
  },
63
- async toFill(page, cssSelector, text) {
64
- return await toFill(page, cssSelector, text);
63
+ async toFill(page, cssSelector, text, opts) {
64
+ return await toFill(page, cssSelector, text, opts);
65
65
  },
66
- async toUncheck(page, text) {
67
- return await toUncheck(page, text);
66
+ async toUncheck(page, text, opts) {
67
+ return await toUncheck(page, text, opts);
68
68
  },
69
69
  async toEvaluate(argumentPassedToExpect, evaluationFn, opts) {
70
70
  return await evaluateWithRetryAndTimeout(argumentPassedToExpect, evaluationFn, opts);
@@ -1,6 +1,4 @@
1
- import launchPage from './launchPage.js';
2
- export default async function visit(path, { page, baseUrl = 'http://localhost:3000' } = {}) {
3
- page ||= await launchPage();
1
+ export default async function visit(path, { baseUrl = 'http://localhost:3000' } = {}) {
4
2
  await page.goto(`${baseUrl}/${path.replace(/^\//, '')}`);
5
3
  return page;
6
4
  }
@@ -1,23 +1,18 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import evaluationFailure from '../internal/evaluationFailure.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toCheck(page, expectedText) {
5
- return await evaluateWithRetryAndTimeout(page, async () => {
6
- requirePuppeteerPage(page);
7
- const checkbox = await page.$(`input[type="checkbox"][value="${expectedText}"]`);
8
- if (!checkbox)
9
- return evaluationFailure(`A checkbox was not found with "${expectedText}"`);
10
- const isChecked = await page.evaluate(checkbox => checkbox.checked, checkbox);
11
- if (isChecked)
12
- return evaluationFailure(`A checkbox was found with "${expectedText}", but it is already checked`);
13
- await checkbox.click();
14
- const isCheckedNow = await page.evaluate(checkbox => checkbox.checked, checkbox);
1
+ export default async function toCheck(page, expectedText, opts) {
2
+ try {
3
+ const el = await page.waitForSelector(`label::-p-text(${expectedText}`, opts);
4
+ await el.click();
15
5
  return {
16
- pass: isCheckedNow,
17
- actual: expectedText,
6
+ pass: true,
7
+ message: () => {
8
+ throw new Error('Cannot negate toCheck');
9
+ },
18
10
  };
19
- }, {
20
- successText: r => `Expected page to have checkable checkbox with text: "${expectedText}"`,
21
- failureText: r => `Expected page not to have checkable checkbox with text: "${expectedText}"`,
22
- });
11
+ }
12
+ catch (error) {
13
+ return {
14
+ pass: false,
15
+ message: `Expected page to have checkable element with text: "${expectedText}"`,
16
+ };
17
+ }
23
18
  }
@@ -1,25 +1,18 @@
1
- import { TimeoutError } from 'puppeteer';
2
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
3
- import evaluationFailure from '../internal/evaluationFailure.js';
4
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
5
- export default async function toClick(page, expectedText) {
6
- return await evaluateWithRetryAndTimeout(page, async () => {
7
- requirePuppeteerPage(page);
8
- try {
9
- const el = await page.locator(`::-p-text(${expectedText})`).setTimeout(5000).wait();
10
- await el.click();
11
- }
12
- catch (err) {
13
- if (err instanceof TimeoutError)
14
- return evaluationFailure(expectedText);
15
- throw err;
16
- }
1
+ export default async function toClick(page, expectedText, opts) {
2
+ try {
3
+ const el = await page.waitForSelector(`*::-p-text(${expectedText})`, opts);
4
+ await el.click();
17
5
  return {
18
6
  pass: true,
19
- actual: expectedText,
7
+ message: () => {
8
+ throw new Error('Cannot negate toClick');
9
+ },
20
10
  };
21
- }, {
22
- successText: () => `Expected page to have clickable element with text: "${expectedText}"`,
23
- failureText: () => `Expected page not to have clickable element with text: "${expectedText}"`,
24
- });
11
+ }
12
+ catch (error) {
13
+ return {
14
+ pass: false,
15
+ message: `Expected page to have clickable element with text: "${expectedText}"`,
16
+ };
17
+ }
25
18
  }
@@ -1,25 +1,18 @@
1
- import { TimeoutError } from 'puppeteer';
2
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
3
- import evaluationFailure from '../internal/evaluationFailure.js';
4
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
5
- export default async function toClickButton(page, expectedText) {
6
- return await evaluateWithRetryAndTimeout(page, async () => {
7
- requirePuppeteerPage(page);
8
- try {
9
- const el = await page.locator(`button ::-p-text(${expectedText})`).setTimeout(10).wait();
10
- await el.click();
11
- }
12
- catch (err) {
13
- if (err instanceof TimeoutError)
14
- return evaluationFailure(expectedText);
15
- throw err;
16
- }
1
+ export default async function toClickButton(page, expectedText, opts) {
2
+ try {
3
+ const el = await page.waitForSelector('button::-p-text(Submit)', opts);
4
+ await el.click();
17
5
  return {
18
6
  pass: true,
19
- actual: expectedText,
7
+ message: () => {
8
+ throw new Error('Cannot negate toClickLink');
9
+ },
20
10
  };
21
- }, {
22
- successText: () => `Expected page to have clickable button with text: "${expectedText}"`,
23
- failureText: () => `Expected page not to have clickable button with text: "${expectedText}"`,
24
- });
11
+ }
12
+ catch (error) {
13
+ return {
14
+ pass: false,
15
+ message: `Expected page to have clickable link with matching text: "${expectedText}"`,
16
+ };
17
+ }
25
18
  }
@@ -1,25 +1,18 @@
1
- import { TimeoutError } from 'puppeteer';
2
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
3
- import evaluationFailure from '../internal/evaluationFailure.js';
4
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
5
- export default async function toClickLink(page, expectedText) {
6
- return await evaluateWithRetryAndTimeout(page, async () => {
7
- requirePuppeteerPage(page);
8
- try {
9
- const el = await page.locator(`a ::-p-text(${expectedText})`).setTimeout(5000).wait();
10
- await el.click();
11
- }
12
- catch (err) {
13
- if (err instanceof TimeoutError)
14
- return evaluationFailure(expectedText);
15
- throw err;
16
- }
1
+ export default async function toClickLink(page, expectedText, opts) {
2
+ try {
3
+ const el = await page.waitForSelector(`a ::-p-text(${expectedText})`, opts);
4
+ await el.click();
17
5
  return {
18
6
  pass: true,
19
- actual: expectedText,
7
+ message: () => {
8
+ throw new Error('Cannot negate toClickLink');
9
+ },
20
10
  };
21
- }, {
22
- successText: () => `Expected page to have clickable link with text: "${expectedText}"`,
23
- failureText: () => `Expected page not to have clickable link with text: "${expectedText}"`,
24
- });
11
+ }
12
+ catch (error) {
13
+ return {
14
+ pass: false,
15
+ message: `Expected page to have clickable link with matching text: "${expectedText}"`,
16
+ };
17
+ }
25
18
  }
@@ -1,25 +1,18 @@
1
- import { TimeoutError } from 'puppeteer';
2
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
3
- import evaluationFailure from '../internal/evaluationFailure.js';
4
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
5
- export default async function toClickSelector(page, cssSelector) {
6
- return await evaluateWithRetryAndTimeout(page, async () => {
7
- requirePuppeteerPage(page);
8
- try {
9
- const el = await page.locator(cssSelector).setTimeout(10).wait();
10
- await el.click();
11
- }
12
- catch (err) {
13
- if (err instanceof TimeoutError)
14
- return evaluationFailure(cssSelector);
15
- throw err;
16
- }
1
+ export default async function toClickSelector(page, cssSelector, opts) {
2
+ try {
3
+ const el = await page.waitForSelector(cssSelector, opts);
4
+ await el.click();
17
5
  return {
18
6
  pass: true,
19
- actual: cssSelector,
7
+ message: () => {
8
+ throw new Error('Cannot negate toNotMatchTextContent, use toMatchTextContent instead');
9
+ },
20
10
  };
21
- }, {
22
- successText: () => `Expected page to have clickable selector with text: "${cssSelector}"`,
23
- failureText: () => `Expected page not to have clickable selector with text: "${cssSelector}"`,
24
- });
11
+ }
12
+ catch (error) {
13
+ return {
14
+ pass: false,
15
+ message: `Expected page to have clickable element with matching selector: "${cssSelector}"`,
16
+ };
17
+ }
25
18
  }
@@ -1,21 +1,18 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import evaluationFailure from '../internal/evaluationFailure.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toFill(page, cssSelector, text) {
5
- return await evaluateWithRetryAndTimeout(page, async () => {
6
- requirePuppeteerPage(page);
7
- try {
8
- await page.type(cssSelector, text);
9
- }
10
- catch {
11
- return evaluationFailure(text);
12
- }
1
+ export default async function toFill(page, cssSelector, text, opts) {
2
+ try {
3
+ await page.waitForSelector(cssSelector, opts);
4
+ await page.type(cssSelector, text);
13
5
  return {
14
6
  pass: true,
15
- actual: text,
7
+ message: () => {
8
+ throw new Error('cannot negate toFill');
9
+ },
16
10
  };
17
- }, {
18
- successText: () => `Expected page to have clickable link with text: "${text}"`,
19
- failureText: () => `Expected page not to have clickable link with text: "${text}"`,
20
- });
11
+ }
12
+ catch {
13
+ return {
14
+ pass: false,
15
+ message: () => `failed to fill input matching selector ${cssSelector} with text "text"`,
16
+ };
17
+ }
21
18
  }
@@ -1,19 +1,18 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import evaluationFailure from '../internal/evaluationFailure.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toHaveChecked(page, expectedText) {
5
- return await evaluateWithRetryAndTimeout(page, async () => {
6
- requirePuppeteerPage(page);
7
- const checkbox = await page.$(`input[type="checkbox"][value="${expectedText}"]`);
8
- if (!checkbox)
9
- return evaluationFailure(`A checkbox was not found with "${expectedText}"`);
1
+ export default async function toHaveChecked(page, expectedText, opts) {
2
+ try {
3
+ const checkbox = await page.waitForSelector(`input[type="checkbox"][value="${expectedText}"]`, opts);
10
4
  const isChecked = await page.evaluate(checkbox => checkbox.checked, checkbox);
11
5
  return {
12
6
  pass: isChecked,
13
- actual: expectedText,
7
+ message: () => {
8
+ throw new Error('cannot negate toHaveChecked, try toHaveUnchecked');
9
+ },
14
10
  };
15
- }, {
16
- successText: r => `Expected page to have checked checkbox with text: "${expectedText}"`,
17
- failureText: r => `Expected page not to have checked checkbox with text: "${expectedText}"`,
18
- });
11
+ }
12
+ catch {
13
+ return {
14
+ pass: false,
15
+ message: () => `Expected page to have checked checkbox with text: "${expectedText}"`,
16
+ };
17
+ }
19
18
  }
@@ -1,25 +1,17 @@
1
- import { TimeoutError } from 'puppeteer';
2
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
3
- import evaluationFailure from '../internal/evaluationFailure.js';
4
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
5
- export default async function toHaveLink(page, expectedText) {
6
- return await evaluateWithRetryAndTimeout(page, async () => {
7
- requirePuppeteerPage(page);
8
- let el = undefined;
9
- try {
10
- el = await page.locator(`a ::-p-text(${expectedText})`).setTimeout(10).wait();
11
- }
12
- catch (err) {
13
- if (err instanceof TimeoutError)
14
- return evaluationFailure(expectedText);
15
- throw err;
16
- }
1
+ export default async function toHaveLink(page, expectedText, opts) {
2
+ try {
3
+ await page.waitForSelector(`a ::-p-text(${expectedText})`, opts);
17
4
  return {
18
- pass: !!el,
19
- actual: expectedText,
5
+ pass: true,
6
+ message: () => {
7
+ throw new Error('cannot negate toFill');
8
+ },
20
9
  };
21
- }, {
22
- successText: () => `Expected page to have clickable link with text: "${expectedText}"`,
23
- failureText: () => `Expected page not to have clickable link with text: "${expectedText}"`,
24
- });
10
+ }
11
+ catch {
12
+ return {
13
+ pass: false,
14
+ message: () => `Expected page not to have checked checkbox with text: "${expectedText}"`,
15
+ };
16
+ }
25
17
  }
@@ -9,7 +9,9 @@ export default async function toHavePath(page, expectedPath) {
9
9
  actual: expectedPath,
10
10
  };
11
11
  }, {
12
- successText: () => `Expected page to have path: "${expectedPath}"`,
12
+ successText: () => {
13
+ throw new Error('cannot negate toHavePath');
14
+ },
13
15
  failureText: () => `Expected page not to have path: "${expectedPath}"`,
14
16
  });
15
17
  }
@@ -1,14 +1,20 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
3
- export default async function toHaveSelector(page, expectedSelector) {
4
- return await evaluateWithRetryAndTimeout(page, async () => {
5
- requirePuppeteerPage(page);
1
+ export default async function toHaveSelector(page, selector, opts) {
2
+ try {
3
+ await page.waitForSelector(selector, opts);
6
4
  return {
7
- pass: !!(await page.$(expectedSelector)),
8
- actual: expectedSelector,
5
+ pass: true,
6
+ message: () => {
7
+ throw new Error('Cannot negate toHaveSelector, use toNotHaveSelector instead');
8
+ },
9
9
  };
10
- }, {
11
- successText: r => `Expected ${r} to have selector: ${expectedSelector}`,
12
- failureText: r => `Expected ${r} not to have selector: ${expectedSelector}`,
13
- });
10
+ }
11
+ catch {
12
+ return {
13
+ pass: false,
14
+ message: () => `
15
+ expected selector: ${selector}
16
+ but no selector was found
17
+ `,
18
+ };
19
+ }
14
20
  }
@@ -1,19 +1,18 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import evaluationFailure from '../internal/evaluationFailure.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toHaveUnchecked(page, expectedText) {
5
- return await evaluateWithRetryAndTimeout(page, async () => {
6
- requirePuppeteerPage(page);
7
- const checkbox = await page.$(`input[type="checkbox"][value="${expectedText}"]`);
8
- if (!checkbox)
9
- return evaluationFailure(`A checkbox was not found with "${expectedText}"`);
1
+ export default async function toHaveUnchecked(page, expectedText, opts) {
2
+ try {
3
+ const checkbox = await page.waitForSelector(`input[type="checkbox"][value="${expectedText}"]`, opts);
10
4
  const isChecked = await page.evaluate(checkbox => checkbox.checked, checkbox);
11
5
  return {
12
6
  pass: !isChecked,
13
- actual: expectedText,
7
+ message: () => {
8
+ throw new Error('cannot negate toHaveUnchecked, try toHaveChecked');
9
+ },
14
10
  };
15
- }, {
16
- successText: r => `Expected page to have unchecked checkbox with text: "${expectedText}"`,
17
- failureText: r => `Expected page not to have unchecked checkbox with text: "${expectedText}"`,
18
- });
11
+ }
12
+ catch {
13
+ return {
14
+ pass: false,
15
+ message: () => `Expected page not to have checked checkbox with text: "${expectedText}"`,
16
+ };
17
+ }
19
18
  }
@@ -1,16 +1,22 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import getAllTextContentFromPage from '../internal/getAllTextContentFromPage.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toMatchTextContent(argumentPassedToExpect, expected) {
5
- return await evaluateWithRetryAndTimeout(argumentPassedToExpect, async () => {
6
- requirePuppeteerPage(argumentPassedToExpect);
7
- const actual = await getAllTextContentFromPage(argumentPassedToExpect);
1
+ export default async function toMatchTextContent(page, text, { selector = 'body' } = {}) {
2
+ try {
3
+ await page.waitForSelector(`${selector}::-p-text(${text.replace(/"/g, '\\"')})`);
8
4
  return {
9
- pass: actual.includes(expected),
10
- actual,
5
+ pass: true,
6
+ message: () => {
7
+ throw new Error('Cannot negate toMatchTextContent, use toNotMatchTextContent instead');
8
+ },
11
9
  };
12
- }, {
13
- successText: r => `Expected ${r} to match text ${expected}`,
14
- failureText: r => `Expected ${r} not to match text ${expected}`,
15
- });
10
+ }
11
+ catch {
12
+ return {
13
+ pass: false,
14
+ message: () => `
15
+ expected ${selector} with text:
16
+ ${text}
17
+
18
+ but no text was found within that selector
19
+ `,
20
+ };
21
+ }
16
22
  }
@@ -1,14 +1,16 @@
1
1
  import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
2
  import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
3
- export default async function toNotHaveSelector(page, expectedSelector) {
3
+ export default async function toNotHaveSelector(page, selector) {
4
4
  return await evaluateWithRetryAndTimeout(page, async () => {
5
5
  requirePuppeteerPage(page);
6
6
  return {
7
- pass: !(await page.$(expectedSelector)),
8
- actual: expectedSelector,
7
+ pass: !(await page.$(selector)),
8
+ actual: selector,
9
9
  };
10
10
  }, {
11
- successText: r => `Expected ${r} not to have selector: ${expectedSelector}`,
12
- failureText: r => `Expected ${r} to have selector: ${expectedSelector}`,
11
+ successText: () => {
12
+ throw new Error('Cannot negate toNotHaveSelector, use toHaveSelector instead');
13
+ },
14
+ failureText: r => `Expected ${r} to have selector: ${selector}`,
13
15
  });
14
16
  }
@@ -10,7 +10,9 @@ export default async function toNotMatchTextContent(argumentPassedToExpect, expe
10
10
  actual,
11
11
  };
12
12
  }, {
13
- successText: r => `Expected ${r} not to match text ${expected}`,
13
+ successText: () => {
14
+ throw new Error('Cannot negate toNotMatchTextContent, use toMatchTextContent instead');
15
+ },
14
16
  failureText: r => `Expected ${r} to match text ${expected}`,
15
17
  });
16
18
  }
@@ -1,23 +1,26 @@
1
- import evaluateWithRetryAndTimeout from '../internal/evaluateWithRetryAndTimeout.js';
2
- import evaluationFailure from '../internal/evaluationFailure.js';
3
- import requirePuppeteerPage from '../internal/requirePuppeteerPage.js';
4
- export default async function toUncheck(page, expectedText) {
5
- return await evaluateWithRetryAndTimeout(page, async () => {
6
- requirePuppeteerPage(page);
7
- const checkbox = await page.$(`input[type="checkbox"][value="${expectedText}"]`);
8
- if (!checkbox)
9
- return evaluationFailure(`A checkbox was not found with "${expectedText}"`);
10
- const isChecked = await page.evaluate(checkbox => checkbox.checked, checkbox);
1
+ export default async function toUncheck(page, expectedText, opts) {
2
+ try {
3
+ const el = await page.waitForSelector(`label::-p-text(${expectedText}`, opts);
4
+ await el.click();
5
+ const isChecked = await page.evaluate(checkbox => checkbox.checked, el);
11
6
  if (!isChecked)
12
- return evaluationFailure(`A checkbox was found with "${expectedText}", but it is already unchecked`);
13
- await checkbox.click();
14
- const isCheckedNow = await page.evaluate(checkbox => checkbox.checked, checkbox);
7
+ return {
8
+ pass: false,
9
+ message: () => `A checkbox was found with "${expectedText}", but it is already unchecked`,
10
+ };
11
+ await el.click();
12
+ const isUncheckedNow = await page.evaluate(checkbox => checkbox.checked, el);
15
13
  return {
16
- pass: !isCheckedNow,
17
- actual: expectedText,
14
+ pass: isUncheckedNow,
15
+ message: () => {
16
+ throw new Error('Cannot negate toUncheck, try toCheck');
17
+ },
18
18
  };
19
- }, {
20
- successText: r => `Expected page to have uncheckable checkbox with text: "${expectedText}"`,
21
- failureText: r => `Expected page not to have uncheckable checkbox with text: "${expectedText}"`,
22
- });
19
+ }
20
+ catch (error) {
21
+ return {
22
+ pass: false,
23
+ message: `Expected page to have checkable element with text: "${expectedText}"`,
24
+ };
25
+ }
23
26
  }
@@ -1,5 +1,3 @@
1
- import { Page } from 'puppeteer';
2
- export default function visit(path: string, { page, baseUrl }?: {
3
- page?: Page;
1
+ export default function visit(path: string, { baseUrl }?: {
4
2
  baseUrl?: string;
5
- }): Promise<Page>;
3
+ }): Promise<import("puppeteer").Page>;
@@ -1,5 +1,8 @@
1
- import { Page } from 'puppeteer';
2
- export default function toCheck(page: Page, expectedText: string): Promise<{
3
- message: () => string;
1
+ import { Page, WaitForSelectorOptions } from 'puppeteer';
2
+ export default function toCheck(page: Page, expectedText: string, opts?: WaitForSelectorOptions): Promise<{
4
3
  pass: boolean;
4
+ message: () => never;
5
+ } | {
6
+ pass: boolean;
7
+ message: string;
5
8
  }>;