|
| 1 | +import startAPIServer, {APIProcess} from "../../src/server/api"; |
| 2 | +import axios from "axios"; |
| 3 | +import electron from "electron"; |
| 4 | + |
| 5 | +let apiServer: APIProcess; |
| 6 | + |
| 7 | +jest.mock('electron', () => ({ |
| 8 | + ...jest.requireActual('electron'), |
| 9 | + |
| 10 | + dialog: { |
| 11 | + showMessageBoxSync: jest.fn(() => 1), |
| 12 | + showErrorBox: jest.fn(), |
| 13 | + } |
| 14 | +})); |
| 15 | + |
| 16 | +describe('Alert test', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + apiServer = await startAPIServer('randomSecret') |
| 19 | + |
| 20 | + axios.defaults.baseURL = `http://localhost:${apiServer.port}/api`; |
| 21 | + axios.defaults.headers.common['x-nativephp-secret'] = 'randomSecret'; |
| 22 | + }) |
| 23 | + |
| 24 | + afterEach(done => { |
| 25 | + apiServer.server.close(done); |
| 26 | + }); |
| 27 | + |
| 28 | + it('can open a alert', async () => { |
| 29 | + const options = { |
| 30 | + message: 'Do you really want to delete this?', |
| 31 | + type: 'info', |
| 32 | + title: 'Are you sure', |
| 33 | + detail: 'This action cannot be undone', |
| 34 | + buttons: ['Delete', 'Cancel'], |
| 35 | + defaultId: 0, |
| 36 | + cancelId: 1 |
| 37 | + } |
| 38 | + |
| 39 | + const response = await axios.post('/alert/message', options); |
| 40 | + |
| 41 | + expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options); |
| 42 | + expect(response.data.result).toEqual(1); |
| 43 | + }); |
| 44 | + |
| 45 | + it('can open an error alert', async () => { |
| 46 | + const options = { |
| 47 | + title: 'Error Dialog', |
| 48 | + message: 'Uh oh!', |
| 49 | + } |
| 50 | + |
| 51 | + const response = await axios.post('/alert/error', options); |
| 52 | + |
| 53 | + expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message); |
| 54 | + expect(response.data.result).toEqual(true); |
| 55 | + }); |
| 56 | +}); |
0 commit comments