forked from TTLApp/time-to-leave
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve coverage of several components (TTLApp#911)
- Loading branch information
Showing
11 changed files
with
344 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const ElectronStore = require('electron-store'); | ||
const { getDateStr } = require('../../js/date-aux'); | ||
const {shouldCheckForUpdates, checkForUpdates} = require('../../js/update-manager'); | ||
|
||
jest.mock('electron', () => | ||
{ | ||
const original = jest.requireActual('electron'); | ||
return { | ||
__esModule: true, | ||
...original, | ||
net: { | ||
...original.net, | ||
request: jest.fn() | ||
} | ||
}; | ||
}); | ||
|
||
jest.mock('is-online', () => () => jest.fn().mockResolvedValueOnce(false).mockResolvedValue(true)); | ||
|
||
const { net } = require('electron'); | ||
|
||
describe('js/update-manager.js', () => | ||
{ | ||
const mocks = {}; | ||
describe('shouldCheckForUpdates', () => | ||
{ | ||
test('Should return true when was never checked', () => | ||
{ | ||
const store = new ElectronStore(); | ||
store.set('update-remind-me-after', false); | ||
expect(shouldCheckForUpdates()).toBe(true); | ||
}); | ||
|
||
test('Should return true when was checked before today', () => | ||
{ | ||
const now = new Date(); | ||
now.setDate(now.getDate() - 1); | ||
const store = new ElectronStore(); | ||
store.set('update-remind-me-after', getDateStr(now)); | ||
expect(shouldCheckForUpdates()).toBe(true); | ||
}); | ||
|
||
test('Should return false when was checked today', () => | ||
{ | ||
const now = new Date(); | ||
const store = new ElectronStore(); | ||
store.set('update-remind-me-after', getDateStr(now)); | ||
expect(shouldCheckForUpdates()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('checkForUpdates', () => | ||
{ | ||
test('should not execute when is offline', () => | ||
{ | ||
mocks.net = jest.spyOn(net, 'request').mockImplementation(() => {}); | ||
checkForUpdates(); | ||
expect(mocks.net).toBeCalledTimes(0); | ||
}); | ||
|
||
test('should not execute when is online', (done) => | ||
{ | ||
mocks.net = jest.spyOn(net, 'request').mockImplementation(() => | ||
{ | ||
return { | ||
on: () => | ||
{ | ||
expect(mocks.net).toBeCalledTimes(1); | ||
done(); | ||
} | ||
}; | ||
}); | ||
checkForUpdates(); | ||
}); | ||
|
||
}); | ||
|
||
afterEach(() => | ||
{ | ||
for (const mock of Object.values(mocks)) | ||
{ | ||
mock.mockClear(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const { BrowserWindow } = require('electron'); | ||
const { getDateStr } = require('../../js/date-aux.js'); | ||
const windows = require('../../js/windows.js'); | ||
const {getWaiverWindow, tray, contextMenu, prefWindow, resetWindowsElements, openWaiverManagerWindow, getDialogCoordinates} = require('../../js/windows.js'); | ||
|
||
describe('windows.js', () => | ||
{ | ||
let showSpy; | ||
let loadSpy; | ||
beforeEach(() => | ||
{ | ||
// Avoid window being shown | ||
loadSpy = jest.spyOn(BrowserWindow.prototype, 'loadURL').mockImplementation(() => {}); | ||
showSpy = jest.spyOn(BrowserWindow.prototype, 'show').mockImplementation(() => {}); | ||
jest.spyOn(windows, 'getDialogCoordinates').mockImplementation(() => ({x:0, y:0})); | ||
}); | ||
|
||
test('Elements should be null on starting', () => | ||
{ | ||
expect(getWaiverWindow()).toBe(null); | ||
expect(tray).toBe(null); | ||
expect(contextMenu).toBe(null); | ||
expect(prefWindow).toBe(null); | ||
}); | ||
|
||
test('Should create waiver window', (done) => | ||
{ | ||
const mainWindow = new BrowserWindow({ | ||
show: false | ||
}); | ||
openWaiverManagerWindow(mainWindow); | ||
expect(getWaiverWindow()).not.toBe(null); | ||
expect(getWaiverWindow()).toBeInstanceOf(BrowserWindow); | ||
expect(getWaiverWindow().getSize()).toEqual([600, 500]); | ||
done(); | ||
}); | ||
|
||
test('Should show waiver window it has been created', (done) => | ||
{ | ||
const mainWindow = new BrowserWindow({ | ||
show: false | ||
}); | ||
openWaiverManagerWindow(mainWindow); | ||
openWaiverManagerWindow(mainWindow); | ||
expect(getWaiverWindow()).not.toBe(null); | ||
// It should only load once the URL because it already exists | ||
expect(showSpy).toHaveBeenCalledTimes(2); | ||
expect(loadSpy).toHaveBeenCalledTimes(1); | ||
done(); | ||
}); | ||
|
||
test('Should set global waiverDay when event is sent', (done) => | ||
{ | ||
const mainWindow = new BrowserWindow({ | ||
show: false | ||
}); | ||
openWaiverManagerWindow(mainWindow, true); | ||
expect(getWaiverWindow()).not.toBe(null); | ||
expect(global.waiverDay).toBe(getDateStr(new Date())); | ||
done(); | ||
}); | ||
|
||
test('Should reset waiverWindow on close', () => | ||
{ | ||
const mainWindow = new BrowserWindow({ | ||
show: false | ||
}); | ||
openWaiverManagerWindow(mainWindow, true); | ||
getWaiverWindow().emit('close'); | ||
expect(getWaiverWindow()).toBe(null); | ||
}); | ||
|
||
test('Should get dialog coordinates', () => | ||
{ | ||
const coordinates = getDialogCoordinates(500, 250, { | ||
getBounds: () => ({ | ||
x: 200, | ||
y: 300, | ||
width: 400, | ||
height: 600 | ||
}) | ||
}); | ||
expect(coordinates).toEqual({ | ||
x: 150, | ||
y: 475 | ||
}); | ||
}); | ||
|
||
afterEach(() => | ||
{ | ||
jest.restoreAllMocks(); | ||
resetWindowsElements(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.