2019-03-20 23:21:59 -05:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2020-10-14 06:58:28 -05:00
|
|
|
import * as rimraf from 'rimraf';
|
2021-01-29 00:25:40 -06:00
|
|
|
import {
|
|
|
|
cleanAppCacheOnInstall,
|
|
|
|
cleanUpAppCache,
|
|
|
|
createAppCacheFile,
|
|
|
|
} from '../src/app/app-cache-handler';
|
2019-03-20 23:21:59 -05:00
|
|
|
import { app, session } from './__mocks__/electron';
|
|
|
|
|
|
|
|
jest.mock('fs', () => ({
|
2021-01-29 00:25:40 -06:00
|
|
|
writeFileSync: jest.fn(),
|
|
|
|
existsSync: jest.fn(() => true),
|
|
|
|
unlinkSync: jest.fn(),
|
|
|
|
readdirSync: jest.fn(() => [
|
|
|
|
'Cache',
|
|
|
|
'GPUCache',
|
|
|
|
'Symphony.config',
|
|
|
|
'cloudConfig.config',
|
|
|
|
]),
|
|
|
|
lstatSync: jest.fn(() => {
|
|
|
|
return {
|
|
|
|
isDirectory: jest.fn(() => true),
|
|
|
|
};
|
|
|
|
}),
|
2020-10-14 06:58:28 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('path', () => ({
|
2021-01-29 00:25:40 -06:00
|
|
|
join: jest.fn(),
|
2020-10-14 06:58:28 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('rimraf', () => ({
|
2021-01-29 00:25:40 -06:00
|
|
|
sync: jest.fn(),
|
2019-03-20 23:21:59 -05:00
|
|
|
}));
|
|
|
|
|
2019-05-15 07:51:52 -05:00
|
|
|
jest.mock('../src/common/logger', () => {
|
2021-01-29 00:25:40 -06:00
|
|
|
return {
|
|
|
|
logger: {
|
|
|
|
error: jest.fn(),
|
|
|
|
info: jest.fn(),
|
|
|
|
},
|
|
|
|
};
|
2019-05-15 07:51:52 -05:00
|
|
|
});
|
|
|
|
|
2019-03-20 23:21:59 -05:00
|
|
|
describe('app cache handler', () => {
|
2021-01-29 00:25:40 -06:00
|
|
|
describe('check app cache file', () => {
|
|
|
|
const cachePathExpected = path.join(app.getPath('userData'), 'CacheCheck');
|
|
|
|
|
|
|
|
it('should call `cleanUpAppCache` correctly', () => {
|
|
|
|
const spyFn = 'unlinkSync';
|
|
|
|
const spy = jest.spyOn(fs, spyFn);
|
|
|
|
cleanUpAppCache();
|
|
|
|
expect(spy).toBeCalledWith(cachePathExpected);
|
|
|
|
});
|
2019-03-20 23:21:59 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
it('should call `clearCache` when `session.defaultSession` is not null', () => {
|
|
|
|
jest.spyOn(fs, 'existsSync').mockImplementation(() => false);
|
|
|
|
const spyFn = 'clearCache';
|
|
|
|
const spy = jest.spyOn(session.defaultSession, spyFn);
|
|
|
|
cleanUpAppCache();
|
|
|
|
expect(spy).toBeCalled();
|
2019-03-20 23:21:59 -05:00
|
|
|
});
|
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
it('should call `createAppCacheFile` correctly', () => {
|
|
|
|
const spyFn = 'writeFileSync';
|
|
|
|
const spy = jest.spyOn(fs, spyFn);
|
|
|
|
createAppCacheFile();
|
|
|
|
expect(spy).lastCalledWith(cachePathExpected, '');
|
|
|
|
});
|
|
|
|
});
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
describe('clean app cache on install', () => {
|
|
|
|
it('should clean app cache and cookies on install', () => {
|
|
|
|
const pathSpy = jest.spyOn(path, 'join');
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
const fsReadDirSpy = jest.spyOn(fs, 'readdirSync');
|
|
|
|
const fsStatSpy = jest.spyOn(fs, 'lstatSync');
|
|
|
|
const fsUnlinkSpy = jest.spyOn(fs, 'unlinkSync');
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
const rimrafSpy = jest.spyOn(rimraf, 'sync');
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
cleanAppCacheOnInstall();
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
expect(pathSpy).toBeCalled();
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
expect(fsReadDirSpy).toBeCalled();
|
|
|
|
expect(fsStatSpy).toBeCalled();
|
|
|
|
expect(fsUnlinkSpy).toBeCalled();
|
2020-10-14 06:58:28 -05:00
|
|
|
|
2021-01-29 00:25:40 -06:00
|
|
|
expect(rimrafSpy).toBeCalled();
|
2019-03-20 23:21:59 -05:00
|
|
|
});
|
2021-01-29 00:25:40 -06:00
|
|
|
});
|
2019-03-20 23:21:59 -05:00
|
|
|
});
|