Skip to content

Commit

Permalink
Add and export resetStateToDefault() method
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDebit committed Nov 20, 2018
1 parent 5507f84 commit 91146ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
25 changes: 16 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ module.exports = function (options) {
Number.isInteger(state.height) && state.height > 0;
}

function resetStateToDefault() {
const displayBounds = screen.getPrimaryDisplay().bounds;

// Reset state to default values on the primary display
state = {
width: config.defaultWidth || 800,
height: config.defaultHeight || 600,
x: 0,
y: 0,
displayBounds
};
}

function validateState() {
const isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
if (!isValid) {
Expand All @@ -45,14 +58,7 @@ module.exports = function (options) {
const displayBounds = screen.getDisplayMatching(state).bounds;
const sameBounds = deepEqual(state.displayBounds, displayBounds, {strict: true});
if (!sameBounds) {
// Reset state to default values on the retrieved display
state = {
width: config.defaultWidth || 800,
height: config.defaultHeight || 600,
x: 0,
y: 0,
displayBounds
};
resetStateToDefault();
}
}
}
Expand Down Expand Up @@ -159,6 +165,7 @@ module.exports = function (options) {
get isFullScreen() { return state.isFullScreen; },
saveState,
unmanage,
manage
manage,
resetStateToDefault
};
};
13 changes: 12 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ test.before(() => {
};
const electronMock = {
app: {getPath() {return '/temp';}},
screen: {getDisplayMatching() {}}
screen: {
getDisplayMatching() {},
getPrimaryDisplay() {}
}
};
mockery.registerAllowables(['./', 'path', 'object-assign', 'deep-equal', 'sinon', './lib/keys.js', './lib/is_arguments.js']);
mockery.registerMock('electron', electronMock);
Expand Down Expand Up @@ -222,6 +225,7 @@ test('Validate state if saved display is available but window outside display bo
const {screen} = require('electron');
const screenBounds = {x: 0, y: 0, width: 1680, height: 1050};
sinon.stub(screen, 'getDisplayMatching').returns({bounds: screenBounds});
sinon.stub(screen, 'getPrimaryDisplay').returns({bounds: screenBounds});

const state = require('.')({
defaultWidth: 500,
Expand All @@ -236,6 +240,7 @@ test('Validate state if saved display is available but window outside display bo

jsonfile.readFileSync.restore();
screen.getDisplayMatching.restore();
screen.getPrimaryDisplay.restore();
});

test('Ensure window is visible at startup if saved display is unavailable and was on the right', t => {
Expand All @@ -251,6 +256,7 @@ test('Ensure window is visible at startup if saved display is unavailable and wa
const {screen} = require('electron');
const screenBounds = {x: 0, y: 0, width: 1920, height: 1080};
sinon.stub(screen, 'getDisplayMatching').returns({bounds: screenBounds});
sinon.stub(screen, 'getPrimaryDisplay').returns({bounds: screenBounds});

const state = require('.')({
defaultWidth: 500,
Expand All @@ -264,6 +270,7 @@ test('Ensure window is visible at startup if saved display is unavailable and wa

jsonfile.readFileSync.restore();
screen.getDisplayMatching.restore();
screen.getPrimaryDisplay.restore();
});

test('Ensure window is visible at startup if saved display is unavailable and was on the left', t => {
Expand All @@ -279,6 +286,7 @@ test('Ensure window is visible at startup if saved display is unavailable and wa
const {screen} = require('electron');
const screenBounds = {x: 0, y: 0, width: 1920, height: 1080};
sinon.stub(screen, 'getDisplayMatching').returns({bounds: screenBounds});
sinon.stub(screen, 'getPrimaryDisplay').returns({bounds: screenBounds});

const state = require('.')({
defaultWidth: 500,
Expand All @@ -292,6 +300,7 @@ test('Ensure window is visible at startup if saved display is unavailable and wa

jsonfile.readFileSync.restore();
screen.getDisplayMatching.restore();
screen.getPrimaryDisplay.restore();
});

test('Reset state to default values if saved display is unavailable', t => {
Expand All @@ -307,6 +316,7 @@ test('Reset state to default values if saved display is unavailable', t => {
const {screen} = require('electron');
const screenBounds = {x: 0, y: 0, width: 1920, height: 1080};
sinon.stub(screen, 'getDisplayMatching').returns({bounds: screenBounds});
sinon.stub(screen, 'getPrimaryDisplay').returns({bounds: screenBounds});

const state = require('.')({
defaultWidth: 500,
Expand All @@ -321,4 +331,5 @@ test('Reset state to default values if saved display is unavailable', t => {

jsonfile.readFileSync.restore();
screen.getDisplayMatching.restore();
screen.getPrimaryDisplay.restore();
});

0 comments on commit 91146ae

Please sign in to comment.