|
| 1 | +import startAPIServer, { APIProcess } from "../../src/server/api"; |
| 2 | +import axios, { AxiosError } from "axios"; |
| 3 | +import electron from "electron"; |
| 4 | +import exp = require("constants"); |
| 5 | + |
| 6 | +let apiServer: APIProcess; |
| 7 | + |
| 8 | +const mockReadImage = { |
| 9 | + isEmpty: jest.fn(() => true), |
| 10 | + toDataURL: jest.fn(() => 'clipboard image'), |
| 11 | +}; |
| 12 | + |
| 13 | +jest.mock('electron', () => ({ |
| 14 | + dialog: { |
| 15 | + showOpenDialogSync: jest.fn(() => ['open dialog result']), |
| 16 | + showSaveDialogSync: jest.fn(() => ['save dialog result']), |
| 17 | + showMessageBoxSync: jest.fn(() => 1), |
| 18 | + showErrorBox: jest.fn(), |
| 19 | + } |
| 20 | +})); |
| 21 | + |
| 22 | +describe('Dialog test', () => { |
| 23 | + beforeEach(async () => { |
| 24 | + apiServer = await startAPIServer('randomSecret') |
| 25 | + |
| 26 | + axios.defaults.baseURL = `http://localhost:${apiServer.port}/api`; |
| 27 | + axios.defaults.headers.common['x-nativephp-secret'] = 'randomSecret'; |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(done => { |
| 31 | + apiServer.server.close(done); |
| 32 | + }); |
| 33 | + |
| 34 | + it('can open a dialog', async () => { |
| 35 | + const electron = require('electron'); |
| 36 | + const options = { |
| 37 | + title: 'Open Dialog', |
| 38 | + defaultPath: '/home/user/Desktop', |
| 39 | + buttonLabel: 'Open', |
| 40 | + filters: [ |
| 41 | + { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, |
| 42 | + { name: 'All Files', extensions: ['*'] } |
| 43 | + ], |
| 44 | + message: 'Select an image', |
| 45 | + properties: ['openFile', 'multiSelections'] |
| 46 | + } |
| 47 | + |
| 48 | + const response = await axios.post('/dialog/open', options); |
| 49 | + |
| 50 | + expect(electron.dialog.showOpenDialogSync).toHaveBeenCalledWith(options); |
| 51 | + expect(response.data.result).toEqual(['open dialog result']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('can open a save dialog', async () => { |
| 55 | + const electron = require('electron'); |
| 56 | + const options = { |
| 57 | + title: 'Open Dialog', |
| 58 | + defaultPath: '/home/user/Desktop', |
| 59 | + buttonLabel: 'Open', |
| 60 | + filters: [ |
| 61 | + { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, |
| 62 | + { name: 'All Files', extensions: ['*'] } |
| 63 | + ], |
| 64 | + message: 'Select an image', |
| 65 | + properties: ['openFile', 'multiSelections'] |
| 66 | + } |
| 67 | + |
| 68 | + const response = await axios.post('/dialog/save', options); |
| 69 | + |
| 70 | + expect(electron.dialog.showSaveDialogSync).toHaveBeenCalledWith(options); |
| 71 | + expect(response.data.result).toEqual(['save dialog result']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('can open a message dialog', async () => { |
| 75 | + const electron = require('electron'); |
| 76 | + const options = { |
| 77 | + title: 'Open Dialog', |
| 78 | + message: 'Select an image', |
| 79 | + type: 'info', |
| 80 | + buttons: ['OK', 'Cancel'] |
| 81 | + } |
| 82 | + |
| 83 | + const response = await axios.post('/dialog/message', options); |
| 84 | + |
| 85 | + expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options); |
| 86 | + expect(response.data.result).toEqual(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('can open an error dialog', async () => { |
| 90 | + const electron = require('electron'); |
| 91 | + const options = { |
| 92 | + title: 'Error Dialog', |
| 93 | + message: 'Uh oh!', |
| 94 | + } |
| 95 | + |
| 96 | + const response = await axios.post('/dialog/error', options); |
| 97 | + |
| 98 | + expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message); |
| 99 | + expect(response.data.result).toEqual(true); |
| 100 | + }); |
| 101 | + |
| 102 | +}); |
0 commit comments