|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const opn = require('opn'); |
| 4 | +const runOpen = require('../../../lib/utils/runOpen'); |
| 5 | + |
| 6 | +jest.mock('opn'); |
| 7 | + |
| 8 | +describe('runOpen util', () => { |
| 9 | + afterEach(() => { |
| 10 | + opn.mockClear(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('should open browser', () => { |
| 14 | + beforeEach(() => { |
| 15 | + opn.mockImplementation(() => Promise.resolve()); |
| 16 | + }); |
| 17 | + |
| 18 | + it('on specify URL', () => { |
| 19 | + return runOpen('https://example.com', {}, console).then(() => { |
| 20 | + expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('on specify URL with page', () => { |
| 25 | + return runOpen( |
| 26 | + 'https://example.com', |
| 27 | + { openPage: '/index.html' }, |
| 28 | + console |
| 29 | + ).then(() => { |
| 30 | + expect(opn.mock.calls[0]).toEqual([ |
| 31 | + 'https://example.com/index.html', |
| 32 | + {}, |
| 33 | + ]); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('on specify URL in Google Chrome', () => { |
| 38 | + return runOpen( |
| 39 | + 'https://example.com', |
| 40 | + { open: 'Google Chrome' }, |
| 41 | + console |
| 42 | + ).then(() => { |
| 43 | + expect(opn.mock.calls[0]).toEqual([ |
| 44 | + 'https://example.com', |
| 45 | + { app: 'Google Chrome' }, |
| 46 | + ]); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('on specify URL with page in Google Chrome ', () => { |
| 51 | + return runOpen( |
| 52 | + 'https://example.com', |
| 53 | + { open: 'Google Chrome', openPage: '/index.html' }, |
| 54 | + console |
| 55 | + ).then(() => { |
| 56 | + expect(opn.mock.calls[0]).toEqual([ |
| 57 | + 'https://example.com/index.html', |
| 58 | + { app: 'Google Chrome' }, |
| 59 | + ]); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('should not open browser', () => { |
| 65 | + const logMock = { warn: jest.fn() }; |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + opn.mockImplementation(() => Promise.reject()); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + logMock.warn.mockClear(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('on specify URL and log error', () => { |
| 76 | + return runOpen('https://example.com', {}, logMock).then(() => { |
| 77 | + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( |
| 78 | + `"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"` |
| 79 | + ); |
| 80 | + expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('on specify URL with page and log error', () => { |
| 85 | + return runOpen( |
| 86 | + 'https://example.com', |
| 87 | + { openPage: '/index.html' }, |
| 88 | + logMock |
| 89 | + ).then(() => { |
| 90 | + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( |
| 91 | + `"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"` |
| 92 | + ); |
| 93 | + expect(opn.mock.calls[0]).toEqual([ |
| 94 | + 'https://example.com/index.html', |
| 95 | + {}, |
| 96 | + ]); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('on specify URL in Google Chrome and log error', () => { |
| 101 | + return runOpen( |
| 102 | + 'https://example.com', |
| 103 | + { open: 'Google Chrome' }, |
| 104 | + logMock |
| 105 | + ).then(() => { |
| 106 | + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( |
| 107 | + `"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"` |
| 108 | + ); |
| 109 | + expect(opn.mock.calls[0]).toEqual([ |
| 110 | + 'https://example.com', |
| 111 | + { |
| 112 | + app: 'Google Chrome', |
| 113 | + }, |
| 114 | + ]); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('on specify URL with page in Google Chrome and log error ', () => { |
| 119 | + return runOpen( |
| 120 | + 'https://example.com', |
| 121 | + { open: 'Google Chrome', openPage: '/index.html' }, |
| 122 | + logMock |
| 123 | + ).then(() => { |
| 124 | + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( |
| 125 | + `"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"` |
| 126 | + ); |
| 127 | + expect(opn.mock.calls[0]).toEqual([ |
| 128 | + 'https://example.com/index.html', |
| 129 | + { app: 'Google Chrome' }, |
| 130 | + ]); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments