SymphonyElectron/tests/SearchUtils.test.js

175 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-12-29 06:06:40 -06:00
const fs = require('fs');
const path = require('path');
2018-02-16 02:14:52 -06:00
const { isMac, isWindowsOS } = require('../js/utils/misc.js');
2017-12-29 06:06:40 -06:00
let executionPath = null;
let userConfigDir = null;
let SearchUtilsAPI;
let searchConfig;
jest.mock('electron', function() {
return {
app: {
getPath: mockedGetPath
}
}
});
function mockedGetPath(type) {
2018-01-08 05:27:18 -06:00
switch (type) {
case 'exe':
return executionPath;
case 'userData':
return userConfigDir;
default:
return ''
2017-12-29 06:06:40 -06:00
}
}
describe('Tests for Search Utils', function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
2017-12-29 06:06:40 -06:00
beforeAll(function (done) {
executionPath = path.join(__dirname, 'library');
2018-02-16 02:14:52 -06:00
if (isWindowsOS) {
2018-02-15 00:50:52 -06:00
executionPath = path.join(__dirname, '..', 'library');
}
2017-12-29 06:06:40 -06:00
userConfigDir = path.join(__dirname, '..');
searchConfig = require('../js/search/searchConfig.js');
const { SearchUtils } = require('../js/search/searchUtils.js');
SearchUtilsAPI = new SearchUtils();
SearchUtilsAPI.path = userConfigDir;
2018-01-08 05:27:18 -06:00
if (fs.existsSync(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE)) {
fs.unlinkSync(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE);
}
2017-12-29 06:06:40 -06:00
done();
});
afterAll(function (done) {
fs.unlinkSync(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE);
done();
});
describe('Tests for checking disk space', function () {
2018-01-08 05:27:18 -06:00
it('should return free space', function (done) {
2017-12-29 06:06:40 -06:00
const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace');
SearchUtilsAPI.checkFreeSpace().then(function () {
expect(checkFreeSpace).toHaveBeenCalled();
done();
});
});
it('should return error', function (done) {
const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace');
2018-01-12 00:53:34 -06:00
if (isMac) {
SearchUtilsAPI.path = undefined;
SearchUtilsAPI.checkFreeSpace().catch(function (err) {
expect(err).toEqual(new Error("Please provide path"));
expect(checkFreeSpace).toHaveBeenCalled();
done();
});
} else {
SearchUtilsAPI.path = undefined;
SearchUtilsAPI.checkFreeSpace().catch(function (err) {
expect(err).toBeTruthy();
expect(checkFreeSpace).toHaveBeenCalled();
done();
});
}
2017-12-29 06:06:40 -06:00
});
it('should return error invalid path', function (done) {
const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace');
SearchUtilsAPI.path = './tp';
2018-02-16 02:14:52 -06:00
if (isWindowsOS) {
2018-01-12 00:53:34 -06:00
SearchUtilsAPI.path = 'A://test';
searchConfig.LIBRARY_CONSTANTS.FREE_DISK_SPACE = path.join(__dirname, '..',
"node_modules/electron-utils/FreeDiskSpace/bin/Release/FreeDiskSpace.exe");
2018-01-12 00:53:34 -06:00
}
2017-12-29 06:06:40 -06:00
SearchUtilsAPI.checkFreeSpace().catch(function (err) {
searchConfig.LIBRARY_CONSTANTS.FREE_DISK_SPACE = path.join(__dirname, '..',
"library/FreeDiskSpace.exe");
2017-12-29 06:06:40 -06:00
expect(checkFreeSpace).toHaveBeenCalled();
2018-01-12 00:53:34 -06:00
expect(err).toBeTruthy();
2017-12-29 06:06:40 -06:00
done();
});
});
});
describe('Test for search users config', function () {
it('should return null for new user config', function (done) {
SearchUtilsAPI.getSearchUserConfig(1234567891011).then(function (res) {
expect(res).toEqual(null);
done();
});
});
it('should exist users config file', function (done) {
setTimeout(function () {
expect(fs.existsSync(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE)).toEqual(true);
done();
}, 2000)
});
it('should exist users config file', function (done) {
setTimeout(function () {
SearchUtilsAPI.getSearchUserConfig(1234567891011).then(function (res) {
expect(res).toEqual({});
done();
});
}, 3000)
});
it('should update user config file', function (done) {
let data = {
rotationId: 0,
version: 1,
language: 'en'
};
SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) {
2018-01-12 06:01:59 -06:00
data.indexVersion = 'v1';
2017-12-29 06:06:40 -06:00
expect(res).toEqual(data);
done();
})
});
it('should modify user config file', function (done) {
let data = {
rotationId: 1,
version: 1,
language: 'en'
};
SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) {
expect(res.rotationId).toEqual(1);
2018-01-12 06:01:59 -06:00
expect(res.indexVersion).toEqual('v1');
2017-12-29 06:06:40 -06:00
done();
})
});
2018-01-01 22:42:14 -06:00
it('should create user if not exist', function (done) {
SearchUtilsAPI.getSearchUserConfig(2234567891011).catch(function (err) {
expect(err).toEqual(null);
done();
})
});
it('should create file on update', function (done) {
fs.unlinkSync(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE);
let data = {
rotationId: 0,
version: 2,
language: 'en'
};
SearchUtilsAPI.updateUserConfig(2234567891011, data).catch(function (err) {
expect(err).toEqual(null);
done();
})
});
2017-12-29 06:06:40 -06:00
});
});