Skip to content

Commit

Permalink
refactor: simplify initial state + moneyMoney resources
Browse files Browse the repository at this point in the history
thereby hopefully stabilizing integration tests

also update cypress because it went nuts
  • Loading branch information
Xiphe committed Sep 22, 2020
1 parent 020b9cd commit 8108f1d
Show file tree
Hide file tree
Showing 43 changed files with 1,003 additions and 908 deletions.
12 changes: 9 additions & 3 deletions cypress/integration/budgetView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

describe('Budget View', () => {
afterEach(() => {
cy.cleanup();
cy.checkTrailingHandlers();
});

it('displays correct overview values', () => {
Expand Down Expand Up @@ -42,7 +42,10 @@ describe('Budget View', () => {
],
setup({ fs, electron: { ipcMain } }) {
fs.writeFileSync(budgetFile, JSON.stringify(myBudget));
ipcMain.handleOnce('INIT', () => budgetFile);
ipcMain.handleOnce('INIT', () => ({
type: 'budget',
file: budgetFile,
}));
ipcMain.handleOnce('MM_EXPORT_CATEGORIES', () => [
incomeCategory,
spendingCategory,
Expand Down Expand Up @@ -99,7 +102,10 @@ describe('Budget View', () => {
],
setup({ fs, electron: { ipcMain } }) {
fs.writeFileSync(budgetFile, JSON.stringify(myBudget));
ipcMain.handleOnce('INIT', () => budgetFile);
ipcMain.handleOnce('INIT', () => ({
type: 'budget',
file: budgetFile,
}));
ipcMain.handleOnce('MM_EXPORT_CATEGORIES', () => [someCategory]);
ipcMain.handleOnce('MM_EXPORT_TRANSACTIONS', () =>
transactions([transaction({ categoryUuid: someCategory.uuid })]),
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/createNewBudget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { account, category } from '../factories';

describe('Create New Budget', () => {
afterEach(() => {
cy.cleanup();
cy.checkTrailingHandlers();
});

it('creates a new budget files with options', () => {
Expand All @@ -20,7 +20,7 @@ describe('Create New Budget', () => {
'BLUR',
],
setup({ electron: { ipcMain } }) {
ipcMain.handleOnce('INIT', () => undefined);
ipcMain.handleOnce('INIT', () => ({ type: 'welcome' }));
ipcMain.handleOnce('MM_EXPORT_ACCOUNTS', () => accounts);
ipcMain.handleOnce('MM_EXPORT_CATEGORIES', () => categories);
ipcMain.handleOnce('MM_EXPORT_TRANSACTIONS', () => []);
Expand Down
8 changes: 4 additions & 4 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Exposed as ExposedElectron,
ExposedInternal,
} from '../../src/__mocks__/electron';
import { BudgetState, validateBudgetState } from '../../src/budget/Types';
import { validateBudgetState } from '../../src/budget/Types';

type BB = {
electron: ExposedElectron;
Expand All @@ -26,7 +26,7 @@ declare global {
namespace Cypress {
interface Chainable<Subject = any> {
open: (config: OpenConfig) => Cypress.Chainable<void>;
cleanup: () => Cypress.Chainable<void>;
checkTrailingHandlers: () => Cypress.Chainable<void>;
bb: () => Cypress.Chainable<BB>;
_bb: () => Cypress.Chainable<BBexposed>;
readBudget: (file: string) => Cypress.Chainable<BudgetState>;
Expand Down Expand Up @@ -70,8 +70,8 @@ Cypress.Commands.add('readBudget', (file: string) => {
)
.then((data) => validateBudgetState(data));
});
Cypress.Commands.add('cleanup', () => {
cy._bb().then(({ _electron }) => _electron.cleanup());
Cypress.Commands.add('checkTrailingHandlers', () => {
cy._bb().then(({ _electron }) => _electron.checkTrailingHandlers());
});
Cypress.Commands.add(
'open',
Expand Down
5 changes: 3 additions & 2 deletions main/createOpenFileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dialog } from 'electron';
import { View } from '../src/shared/types';

export default function createOpenFileHandler(
createWindow: (file?: string) => void,
createWindow: (initialView: View) => void,
) {
const openFile = async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
Expand All @@ -11,7 +12,7 @@ export default function createOpenFileHandler(
if (canceled) {
return;
}
filePaths.forEach(createWindow);
filePaths.forEach((file) => createWindow({ type: 'budget', file }));
};

return openFile;
Expand Down
9 changes: 5 additions & 4 deletions main/defaultMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
createOpenRecent,
} from '../src/shared/createMenu';
import { Settings } from '../src/shared/settings';
import { View } from '../src/shared/types';

export function createDefaultMenu(
createWindow: (file?: string, hash?: string) => void,
createWindow: (initialView: View) => void,
openFile: () => void,
settings: Settings,
) {
Expand All @@ -21,14 +22,14 @@ export function createDefaultMenu(
createFileMenu({
openRecent: createOpenRecent(
settings.getRecentFiles(),
createWindow,
(file) => createWindow({ type: 'budget', file }),
),
fileNew: () => createWindow(undefined, 'new'),
fileNew: () => createWindow({ type: 'new' }),
fileOpen: openFile,
}),
],
{
welcome: () => createWindow(undefined),
welcome: () => createWindow({ type: 'welcome' }),
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function main() {
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
windowManager.createWindow();
windowManager.createWindow({ type: 'welcome' });
}
});
}
3 changes: 2 additions & 1 deletion main/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": false
"noEmit": false,
"types": ["./typings"]
},
"include": ["./*.ts"]
}
7 changes: 7 additions & 0 deletions main/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { View } from '../src/shared/types';

declare module 'electron' {
interface WebContents {
initialView: View;
}
}
45 changes: 31 additions & 14 deletions main/windowManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { BrowserWindow, IpcMain, WebContents, App } from 'electron';
import { join } from 'path';
import { Settings } from '../src/shared/settings';
import { View } from '../src/shared/types';

// type WebContents = WCT & {
// initialView?: View;
// };
// type WebContents = Omit<BrowserWindow, 'webContents'> & {
// webContents: WebContents;
// };

export type WindowManager = ReturnType<typeof createWindowManager>;
export default function createWindowManager(
Expand Down Expand Up @@ -71,9 +79,12 @@ export default function createWindowManager(
});
}

function createWindow(file?: string, hash?: string) {
if (file && windows[file]) {
windows[file].show();
function createWindow(initialView: View) {
if (
(initialView.type === 'budget' || initialView.type === 'settings') &&
windows[initialView.file]
) {
windows[initialView.file].show();
return;
}

Expand Down Expand Up @@ -103,14 +114,15 @@ export default function createWindowManager(
if (!SERVER_URL) {
throw new Error('Can not open dev window without SERVER_URL');
}
win.loadURL(`${SERVER_URL}#${hash}`);
win.loadURL(`${SERVER_URL}`);
win.webContents.openDevTools();
} else {
win.loadFile(join(__dirname, '../build/index.html'), { hash });
win.loadFile(join(__dirname, '../build/index.html'));
}

win.webContents.initialView = initialView;
broadcast('WINDOW_CREATED');
registerWindow(win, file);
registerWindow(win, initialView.file);

win.once('closed', (ev: any) => {
if (!appIsQuitting) {
Expand All @@ -119,9 +131,12 @@ export default function createWindowManager(
});
}

ipcMain.handle('INIT', (ev) => {
return findFile(ev.sender);
});
ipcMain.handle(
'INIT',
(ev): View => {
return ev.sender.initialView;
},
);
ipcMain.on('QUIT', (ev) => {
const win = getWindow(ev.sender);
if (!win) {
Expand All @@ -137,22 +152,24 @@ export default function createWindowManager(
win.setDocumentEdited(edited);
});
ipcMain.handle('MENU_FILE_NEW', () => {
createWindow(undefined, 'new');
createWindow({ type: 'new' });
});
ipcMain.handle('MENU_FILE_WELCOME', () => {
createWindow(undefined);
createWindow({ type: 'welcome' });
});
ipcMain.handle('MENU_FILE_OPEN_EXISTING', (_, file: string) => {
createWindow(file);
createWindow({ type: 'budget', file });
});

return {
init() {
const previouslyOpen = settings.getOpenBudgets();
if (previouslyOpen.length) {
previouslyOpen.forEach((r) => createWindow(r));
previouslyOpen.forEach((r) =>
createWindow({ type: 'budget', file: r }),
);
} else {
createWindow();
createWindow({ type: 'welcome' });
}
},
createWindow,
Expand Down
Loading

0 comments on commit 8108f1d

Please sign in to comment.