Mailspring/app/spec/spec-runner/master-before-each.es6

140 lines
3.8 KiB
Plaintext
Raw Normal View History

import {
Account,
TaskQueue,
AccountStore,
DatabaseStore,
ComponentRegistry,
MailboxPerspective,
FocusedPerspectiveStore,
} from 'mailspring-exports';
2017-09-27 02:33:08 +08:00
import { clipboard } from 'electron';
2016-10-18 08:59:33 +08:00
import Config from '../../src/config';
2017-09-27 02:33:08 +08:00
import configUtils from '../../src/config-utils';
import TimeOverride from './time-override';
import TestConstants from './test-constants';
2017-09-27 02:33:08 +08:00
import * as jasmineExtensions from './jasmine-extensions';
class MasterBeforeEach {
setup(loadSettings, beforeEach) {
this.loadSettings = loadSettings;
const self = this;
beforeEach(function jasmineBeforeEach() {
const currentSpec = this;
currentSpec.addMatchers({
toHaveLength: jasmineExtensions.toHaveLength,
2017-09-27 02:33:08 +08:00
});
2017-09-27 02:36:58 +08:00
self._resetAppEnv();
2017-09-27 02:33:08 +08:00
self._resetDatabase();
self._resetTaskQueue();
self._resetTimeOverride();
self._resetAccountStore();
self._resetConfig();
self._resetClipboard();
ComponentRegistry._clear();
advanceClock(1000);
TimeOverride.resetSpyData();
});
}
2017-09-27 02:36:58 +08:00
_resetAppEnv() {
// Don't actually write to disk
spyOn(AppEnv, 'saveWindowState');
// prevent specs from modifying N1's menus
2017-09-27 02:36:58 +08:00
spyOn(AppEnv.menu, 'sendToBrowserProcess');
FocusedPerspectiveStore._current = MailboxPerspective.forNothing();
}
_resetDatabase() {
global.localStorage.clear();
DatabaseStore._transactionQueue = undefined;
// If we don't spy on DatabaseStore._query, then
// `DatabaseStore.inTransaction` will never complete and cause all
// tests that depend on transactions to hang.
//
// @_query("BEGIN IMMEDIATE TRANSACTION") never resolves because
// DatabaseStore._query never runs because the @_open flag is always
2017-09-27 02:36:58 +08:00
// false because we never setup the DB when `AppEnv.inSpecMode` is
// true.
2017-09-27 02:33:08 +08:00
spyOn(DatabaseStore, '_query').andCallFake(() => Promise.resolve([]));
}
_resetTaskQueue() {
TaskQueue._queue = [];
TaskQueue._completed = [];
TaskQueue._onlineStatus = true;
}
_resetTimeOverride() {
TimeOverride.resetTime();
TimeOverride.enableSpies();
}
_resetAccountStore() {
// Log in a fake user, and ensure that accountForId, etc. work
AccountStore._accounts = [
new Account({
2017-09-27 02:33:08 +08:00
provider: 'gmail',
name: TestConstants.TEST_ACCOUNT_NAME,
emailAddress: TestConstants.TEST_ACCOUNT_EMAIL,
id: TestConstants.TEST_ACCOUNT_ID,
aliases: [
`${TestConstants.TEST_ACCOUNT_NAME} Alternate <${
TestConstants.TEST_ACCOUNT_ALIAS_EMAIL
}>`,
],
}),
new Account({
2017-09-27 02:33:08 +08:00
provider: 'gmail',
name: 'Second',
emailAddress: 'second@gmail.com',
2017-06-22 04:12:49 +08:00
id: 'second-test-account-id',
aliases: [
'Second Support <second@gmail.com>',
'Second Alternate <second+alternate@gmail.com>',
'Second <second+third@gmail.com>',
],
}),
];
}
_resetConfig() {
// reset config before each spec; don't load or save from/to `config.json`
2017-08-27 06:13:34 +08:00
let fakePersistedConfig = {
env: 'production',
};
spyOn(Config.prototype, 'getRawValues').andCallFake(() => {
return fakePersistedConfig;
2017-08-27 06:13:34 +08:00
});
spyOn(Config.prototype, 'setRawValue').andCallFake(function setRawValue(keyPath, value) {
if (keyPath) {
configUtils.setValueForKeyPath(fakePersistedConfig, keyPath, value);
} else {
fakePersistedConfig = value;
}
return this.load();
});
2017-08-27 06:13:34 +08:00
2017-09-27 02:36:58 +08:00
AppEnv.config = new Config();
AppEnv.loadConfig();
}
_resetClipboard() {
let clipboardContent = 'initial clipboard content';
spyOn(clipboard, 'writeText').andCallFake(text => {
clipboardContent = text;
});
spyOn(clipboard, 'readText').andCallFake(() => clipboardContent);
}
}
2017-09-27 02:33:08 +08:00
export default new MasterBeforeEach();