-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to add config watching and test coverage (#27)
* Add StandardJS linter * Add windows tests * Add more tests * [WIP] Convert windows to class; plugin broken * Add dispose * Refactor modules; add tests * Add tests for setup * Fix broken paths and listeners * Refactor with more tests * Change hideOnBlur default to false Update default behavior for blurring a window, to reduce confusion for users. * Add debounce to properly manage blur event Prevent hiding Hyper during new window creation. * Fix setup tests * Fix missing parameter in setup * Add coverage; event handler removal with DI Use dependency injection to allow for additional test coverage and proper removal of event handlers. * Repair handler provided for blur event * Repair last focused window tracking * Fix activate handler * Update README * Update Jest coverage config * Align callback generation
- Loading branch information
Showing
18 changed files
with
1,374 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
coverage/ | ||
node_modules/ |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
registerShortcut = () => jest.fn() | ||
|
||
module.exports = registerShortcut | ||
module.exports = jest.fn( | ||
() => jest.fn() | ||
) |
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,17 @@ | ||
exports.generateApp = () => ({ | ||
config: { | ||
getConfig: jest.fn(() => ({})), | ||
subscribe: jest.fn() | ||
}, | ||
createWindow: jest.fn(), | ||
dock: { | ||
hide: jest.fn(), | ||
show: jest.fn() | ||
}, | ||
getLastFocusedWindow: jest.fn(), | ||
getWindows: jest.fn(() => new Set()), | ||
hide: jest.fn(), | ||
on: jest.fn(), | ||
removeListener: jest.fn(), | ||
show: jest.fn() | ||
}) |
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,25 @@ | ||
const generateWindowSet = (count, opts = {}) => { | ||
const windows = [] | ||
|
||
for (var i = 0; i < count; i++) { | ||
windows.push(generateWindow(opts)) | ||
} | ||
|
||
return new Set(windows) | ||
} | ||
|
||
const generateWindow = ({ focused = false, fullScreen = false, visible = true } = {}) => ({ | ||
focus: jest.fn(), | ||
hide: jest.fn(), | ||
isFocused: jest.fn(() => focused), | ||
isFullScreen: jest.fn(() => fullScreen), | ||
isVisible: jest.fn(() => visible), | ||
minimize: jest.fn(), | ||
on: jest.fn(), | ||
show: jest.fn() | ||
}) | ||
|
||
module.exports = { | ||
generateWindow, | ||
generateWindowSet | ||
} |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
const addWindow = require('./modules/windows').addWindow; | ||
const setup = require('./modules/setup'); | ||
const dispose = require('./modules/dispose') | ||
const { generateActivateCallback, onApp } = require('./modules/app') | ||
const { generateBlurCallback, hideWindows, showWindows } = require('./modules/windows') | ||
|
||
const windowSet = new Set([]); | ||
let handleActivate, handleBlur | ||
|
||
module.exports = { | ||
onApp: app => setup(app, windowSet), | ||
onWindow: window => addWindow(window, windowSet) | ||
exports.onApp = app => { | ||
handleBlur = generateBlurCallback(hideWindows)(app) | ||
handleActivate = generateActivateCallback(showWindows)(app) | ||
|
||
onApp(app, handleActivate, handleBlur) | ||
} | ||
exports.onUnload = app => dispose(app, handleActivate, handleBlur) |
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,95 @@ | ||
const registerShortcut = require('hyperterm-register-shortcut') | ||
const toggle = require('../toggle') | ||
const { applyConfig, generateActivateCallback, onApp } = require('../app') | ||
const { generateApp } = require('../../fixtures/app') | ||
|
||
jest.mock('../toggle') | ||
jest.mock('../windows') | ||
jest.mock('hyperterm-register-shortcut') | ||
|
||
let app = generateApp() | ||
let callback | ||
const handleBlurMock = jest.fn() | ||
const generateActivateCallbackMock = jest.fn() | ||
|
||
describe('applyConfig', () => { | ||
describe('with default config', () => { | ||
beforeEach(() => { | ||
applyConfig(app, handleBlurMock) | ||
}) | ||
|
||
it('registers the default hot key', () => { | ||
expect(registerShortcut).toHaveBeenCalledWith('summon', toggle, 'Ctrl+;') | ||
}) | ||
|
||
it('shows the dock', () => { | ||
expect(app.dock.show).toHaveBeenCalled() | ||
}) | ||
|
||
it('does not handle blur events', () => { | ||
expect(app.removeListener).toHaveBeenCalledWith('browser-window-blur', handleBlurMock) | ||
}) | ||
}) | ||
|
||
describe('with hideDock config enabled', () => { | ||
beforeEach(() => { | ||
app.config.getConfig.mockReturnValue({ | ||
summon: { | ||
hideDock: true | ||
} | ||
}) | ||
applyConfig(app, handleBlurMock) | ||
}) | ||
|
||
it('hides the dock', () => { | ||
expect(app.dock.hide).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('with hideOnBlur config enabled', () => { | ||
beforeEach(() => { | ||
app.config.getConfig.mockReturnValue({ | ||
summon: { | ||
hideOnBlur: true | ||
} | ||
}) | ||
applyConfig(app, handleBlurMock) | ||
}) | ||
|
||
it('handles blur events', () => { | ||
expect(app.on).toHaveBeenCalledWith('browser-window-blur', handleBlurMock) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('generateActivateCallback', () => { | ||
beforeAll(() => { | ||
callback = jest.fn() | ||
}) | ||
|
||
it('returns a function', () => { | ||
expect(generateActivateCallback(callback)(app)).toBeInstanceOf(Function) | ||
}) | ||
|
||
it('resulting callback shows the windows', () => { | ||
generateActivateCallback(callback)(app)() | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('onApp', () => { | ||
describe('with default config', () => { | ||
beforeEach(() => { | ||
onApp(app, handleBlurMock, generateActivateCallbackMock) | ||
}) | ||
|
||
it('handles the activate event', () => { | ||
expect(app.on).toHaveBeenCalledWith('activate', expect.any(Function)) | ||
}) | ||
|
||
it('subscribes to config change', () => { | ||
expect(app.config.subscribe).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
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,31 @@ | ||
const dispose = require('../dispose') | ||
const { generateApp } = require('../../fixtures/app') | ||
// const { unregisterShortcut } = require('hyperterm-register-shortcut') | ||
|
||
// jest.mock('hyperterm-register-shortcut') | ||
|
||
const app = generateApp() | ||
const handleActivateMock = jest.fn() | ||
const handleBlurMock = jest.fn() | ||
|
||
describe('dispose', () => { | ||
beforeEach(() => { | ||
dispose(app, handleActivateMock, handleBlurMock) | ||
}) | ||
|
||
it('shows the dock icon', () => { | ||
expect(app.dock.show).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
xit('unregisters the shortcut', () => { | ||
// expect(unregisterShortcut).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('removes the activate listener', () => { | ||
expect(app.removeListener).toHaveBeenCalledWith('activate', handleActivateMock) | ||
}) | ||
|
||
it('removes the blur listener', () => { | ||
expect(app.removeListener).toHaveBeenCalledWith('browser-window-blur', handleBlurMock) | ||
}) | ||
}) |
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,46 @@ | ||
const toggle = require('../toggle') | ||
const { generateApp } = require('../../fixtures/app') | ||
const { generateWindow, generateWindowSet } = require('../../fixtures/windowSet') | ||
const { hideWindows, showWindows } = require('../windows') | ||
|
||
jest.mock('../windows') | ||
|
||
const app = generateApp() | ||
let win | ||
let set = generateWindowSet(2) | ||
|
||
describe('toggle', () => { | ||
beforeAll(() => { | ||
app.getWindows.mockReturnValue(set) | ||
}) | ||
|
||
describe('when windows blurred', () => { | ||
beforeEach(() => { | ||
toggle(app) | ||
}) | ||
|
||
it('shows the windows', () => { | ||
expect(showWindows).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('when windows focused', () => { | ||
beforeAll(() => { | ||
win = generateWindow() | ||
win.isFocused.mockReturnValue(true) | ||
set.add(win) | ||
}) | ||
|
||
beforeEach(() => { | ||
toggle(app) | ||
}) | ||
|
||
afterAll(() => { | ||
set.delete(win) | ||
}) | ||
|
||
it('hides windows', () => { | ||
expect(hideWindows).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.