1/**
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import expect from 'expect';
17import sinon from 'sinon';
18
19import {
20  getTestState,
21  setupTestPageAndContextHooks,
22  setupTestBrowserHooks,
23  itFailsFirefox,
24} from './mocha-utils'; // eslint-disable-line import/extensions
25
26describe('Page.Events.Dialog', function () {
27  setupTestBrowserHooks();
28  setupTestPageAndContextHooks();
29
30  it('should fire', async () => {
31    const { page } = getTestState();
32
33    const onDialog = sinon.stub().callsFake((dialog) => {
34      dialog.accept();
35    });
36    page.on('dialog', onDialog);
37
38    await page.evaluate(() => alert('yo'));
39
40    expect(onDialog.callCount).toEqual(1);
41    const dialog = onDialog.firstCall.args[0];
42    expect(dialog.type()).toBe('alert');
43    expect(dialog.defaultValue()).toBe('');
44    expect(dialog.message()).toBe('yo');
45  });
46
47  it('should allow accepting prompts', async () => {
48    const { page } = getTestState();
49
50    const onDialog = sinon.stub().callsFake((dialog) => {
51      dialog.accept('answer!');
52    });
53    page.on('dialog', onDialog);
54
55    const result = await page.evaluate(() => prompt('question?', 'yes.'));
56
57    expect(onDialog.callCount).toEqual(1);
58    const dialog = onDialog.firstCall.args[0];
59    expect(dialog.type()).toBe('prompt');
60    expect(dialog.defaultValue()).toBe('yes.');
61    expect(dialog.message()).toBe('question?');
62
63    expect(result).toBe('answer!');
64  });
65  it('should dismiss the prompt', async () => {
66    const { page } = getTestState();
67
68    page.on('dialog', (dialog) => {
69      dialog.dismiss();
70    });
71    const result = await page.evaluate(() => prompt('question?'));
72    expect(result).toBe(null);
73  });
74});
75