summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/test/src/dialog.spec.ts
blob: e137ccf51783815b3a56a25549aeff068c7ecc55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * @license
 * Copyright 2018 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */
import expect from 'expect';
import sinon from 'sinon';

import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Page.Events.Dialog', function () {
  setupTestBrowserHooks();

  it('should fire', async () => {
    const {page} = await getTestState();

    const onDialog = sinon.stub().callsFake(dialog => {
      dialog.accept();
    });
    page.on('dialog', onDialog);

    await page.evaluate(() => {
      return alert('yo');
    });

    expect(onDialog.callCount).toEqual(1);
    const dialog = onDialog.firstCall.args[0]!;
    expect(dialog.type()).toBe('alert');
    expect(dialog.defaultValue()).toBe('');
    expect(dialog.message()).toBe('yo');
  });

  it('should allow accepting prompts', async () => {
    const {page} = await getTestState();

    const onDialog = sinon.stub().callsFake(dialog => {
      dialog.accept('answer!');
    });
    page.on('dialog', onDialog);

    const result = await page.evaluate(() => {
      return prompt('question?', 'yes.');
    });

    expect(onDialog.callCount).toEqual(1);
    const dialog = onDialog.firstCall.args[0]!;
    expect(dialog.type()).toBe('prompt');
    expect(dialog.defaultValue()).toBe('yes.');
    expect(dialog.message()).toBe('question?');

    expect(result).toBe('answer!');
  });
  it('should dismiss the prompt', async () => {
    const {page} = await getTestState();

    page.on('dialog', dialog => {
      void dialog.dismiss();
    });
    const result = await page.evaluate(() => {
      return prompt('question?');
    });
    expect(result).toBe(null);
  });
});