Skip to content

Commit

Permalink
refactor: remove test logic from lib (#171)
Browse files Browse the repository at this point in the history
* refactor: remove test logic from lib

* clean up test fixtures
  • Loading branch information
erickzhao authored Nov 30, 2024
1 parent 29be651 commit ace95db
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 106 deletions.
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: 'node',
transform: {
'^.+.tsx?$': ['ts-jest', {}]
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
},
"devDependencies": {
"@types/github-url-to-object": "^4.0.1",
"@types/jest": "^29.5.14",
"@types/ms": "^0.7.31",
"electron": "^22.0.0",
"jest": "^29.0.0",
"prettier": "^3.0.3",
"standard": "^14.3.4",
"standard-markdown": "^6.0.0",
"ts-jest": "^29.2.5",
"typescript": "^4.9.4"
},
"scripts": {
Expand Down
34 changes: 17 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import assert from 'assert';
import ms from 'ms';
import gh from 'github-url-to-object';
import path from 'path';
import fs from 'fs';
import os from 'os';
import { format } from 'util';

import { app } from 'electron';
import assert from 'node:assert';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { format } from 'node:util';

import { app, autoUpdater, dialog } from 'electron';

export interface ILogger {
log(message: string): void;
Expand Down Expand Up @@ -100,12 +101,15 @@ export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) {
return;
}

if (safeOpts.electron.app.isReady()) initUpdater(safeOpts);
else app.on('ready', () => initUpdater(safeOpts));
if (app.isReady()) {
initUpdater(safeOpts);
} else {
app.on('ready', () => initUpdater(safeOpts));
}
}

function initUpdater(opts: ReturnType<typeof validateInput>) {
const { updateSource, updateInterval, logger, electron } = opts;
const { updateSource, updateInterval, logger } = opts;

// exit early on unsupported platforms, e.g. `linux`
if (!supportedPlatforms.includes(process?.platform)) {
Expand All @@ -115,7 +119,6 @@ function initUpdater(opts: ReturnType<typeof validateInput>) {
return;
}

const { app, autoUpdater, dialog } = electron;
let feedURL: string;
let serverType: 'default' | 'json' = 'default';
switch (updateSource.type) {
Expand Down Expand Up @@ -195,8 +198,8 @@ function initUpdater(opts: ReturnType<typeof validateInput>) {
}, ms(updateInterval));
}

function guessRepo(electron: typeof Electron.Main) {
const pkgBuf = fs.readFileSync(path.join(electron.app.getAppPath(), 'package.json'));
function guessRepo() {
const pkgBuf = fs.readFileSync(path.join(app.getAppPath(), 'package.json'));
const pkg = JSON.parse(pkgBuf.toString());
const repoString = pkg.repository?.url || pkg.repository;
const repoObject = gh(repoString);
Expand All @@ -213,15 +216,12 @@ function validateInput(opts: IUpdateElectronAppOptions) {
};
const { host, updateInterval, logger, notifyUser } = Object.assign({}, defaults, opts);

// allows electron to be mocked in tests
const electron: typeof Electron.Main = (opts as any).electron || require('electron');

let updateSource = opts.updateSource;
// Handle migration from old properties + default to update service
if (!updateSource) {
updateSource = {
type: UpdateSourceType.ElectronPublicUpdateService,
repo: opts.repo || guessRepo(electron),
repo: opts.repo || guessRepo(),
host,
};
}
Expand Down Expand Up @@ -254,5 +254,5 @@ function validateInput(opts: IUpdateElectronAppOptions) {

assert(logger && typeof logger.log, 'function');

return { updateSource, updateInterval, logger, electron, notifyUser };
return { updateSource, updateInterval, logger, notifyUser };
}
1 change: 0 additions & 1 deletion test/__mocks__/electron-is-dev.ts

This file was deleted.

28 changes: 28 additions & 0 deletions test/__mocks__/electron.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import os from 'node:os';

module.exports = {
app: {
getVersion: () => {
return '1.2.3';
},
isReady: () => true,
on: () => {
/* no-op */
},
getAppPath: () => {
return os.tmpdir();
},
isPackaged: true,
},
autoUpdater: {
checkForUpdates: () => {
/* no-op */
},
on: () => {
/* no-op */
},
setFeedURL: () => {
/* no-op */
},
},
dialog: {
showMessageBox: () => {
/* no-op */
},
},
};
85 changes: 0 additions & 85 deletions test/index.test.js

This file was deleted.

61 changes: 61 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { updateElectronApp } from '..';
const repo = 'some-owner/some-repo';

jest.mock('electron');

beforeEach(() => {
jest.useFakeTimers();
});

describe('updateElectronApp', () => {
it('is a function', () => {
expect(typeof updateElectronApp).toBe('function');
});

describe('repository', () => {
const tmpdir = os.tmpdir();
const packageJson = path.join(tmpdir, 'package.json');
beforeAll(() => {
fs.writeFileSync(packageJson, JSON.stringify({}));
});

it('is required', () => {
expect(() => {
updateElectronApp();
}).toThrow("repo not found. Add repository string to your app's package.json file");
});

it('from opts', () => {
updateElectronApp({ repo: 'foo/bar' });
});

it('from package.json', () => {
fs.writeFileSync(packageJson, JSON.stringify({ repository: 'foo/bar' }));
updateElectronApp();
});

afterAll(() => {
fs.rmSync(packageJson);
});
});

describe('host', () => {
it('must a valid HTTPS URL', () => {
expect(() => {
updateElectronApp({ repo, host: 'http://example.com' });
}).toThrow('host must be a valid HTTPS URL');
});
});

describe('updateInterval', () => {
it('must be 5 minutes or more', () => {
expect(() => {
updateElectronApp({ repo, updateInterval: '20 seconds' });
}).toThrow('updateInterval must be `5 minutes` or more');
});
});
});
Loading

0 comments on commit ace95db

Please sign in to comment.