-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add base settings and move skip config to here
- Loading branch information
1 parent
06362a5
commit 37d82fb
Showing
7 changed files
with
152 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import FileService from '../file' | ||
|
||
export default class BaseSettings { | ||
private static moduleName = '' | ||
private static fileName = 'settings.json' | ||
|
||
private static instance: BaseSettings | ||
|
||
public static getInstance(): BaseSettings { | ||
if (!BaseSettings.instance) { | ||
BaseSettings.instance = new BaseSettings() | ||
} | ||
|
||
return BaseSettings.instance | ||
} | ||
|
||
public updateSetting = (key: string, value: any) => { | ||
let settings = this.read() | ||
if (settings === undefined) { | ||
settings = {} | ||
} | ||
Object.assign(settings, { [key]: value }) | ||
FileService.getInstance().writeFileSync(BaseSettings.moduleName, BaseSettings.fileName, JSON.stringify(settings)) | ||
} | ||
|
||
public getSetting = (key: string) => { | ||
const info = this.read() | ||
|
||
if (info) { | ||
return info[key] | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
public read = () => { | ||
const fileService = FileService.getInstance() | ||
const { moduleName, fileName } = BaseSettings | ||
|
||
if (fileService.hasFile(moduleName, fileName)) { | ||
const info = FileService.getInstance().readFileSync(moduleName, fileName) | ||
const value = JSON.parse(info) | ||
return value | ||
} | ||
|
||
return undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/neuron-wallet/tests/services/settings/base.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import BaseSettings from '../../../src/services/settings/base' | ||
import FileService from '../../../src/services/file' | ||
|
||
describe('BaseSettings', () => { | ||
let base: BaseSettings | undefined | ||
|
||
beforeEach(() => { | ||
const fileService = FileService.getInstance() | ||
// @ts-ignore: Private method | ||
const { moduleName, fileName } = BaseSettings | ||
if (fileService.hasFile(moduleName, fileName)) { | ||
fileService.deleteFileSync(moduleName, fileName) | ||
} | ||
|
||
base = new BaseSettings() | ||
}) | ||
|
||
const key = 'testKey' | ||
const value = 'testValue' | ||
|
||
const key2 = 'testKey2' | ||
const value2 = 'testValue2' | ||
|
||
it('getInstance', () => { | ||
const baseSettings = BaseSettings.getInstance() | ||
expect(baseSettings).toBeInstanceOf(BaseSettings) | ||
}) | ||
|
||
it('update', () => { | ||
expect(() => { | ||
base!.updateSetting(key, value) | ||
}).not.toThrowError() | ||
}) | ||
|
||
it('read empty', () => { | ||
const settings = base!.read() | ||
expect(settings).toBeUndefined() | ||
}) | ||
|
||
it('update and read', () => { | ||
base!.updateSetting(key, value) | ||
const settings = base!.read() | ||
expect(settings).toEqual({ [key]: value }) | ||
}) | ||
|
||
it('update and get', () => { | ||
base!.updateSetting(key, value) | ||
const result = base!.getSetting(key) | ||
expect(result).toEqual(value) | ||
}) | ||
|
||
it('update multi', () => { | ||
base!.updateSetting(key, value) | ||
base!.updateSetting(key2, value2) | ||
const result = base!.read() | ||
expect(result).toEqual({ | ||
[key]: value, | ||
[key2]: value2, | ||
}) | ||
}) | ||
|
||
it('update multi and get', () => { | ||
base!.updateSetting(key, value) | ||
base!.updateSetting(key2, value2) | ||
const result = base!.getSetting(key) | ||
expect(result).toEqual(value) | ||
}) | ||
|
||
it('update key multi times', () => { | ||
base!.updateSetting(key, value) | ||
base!.updateSetting(key, value2) | ||
|
||
const result = base!.getSetting(key) | ||
expect(result).toEqual(value2) | ||
}) | ||
|
||
it('new instance', () => { | ||
base!.updateSetting(key, value) | ||
const newInstance = new BaseSettings() | ||
const result = newInstance.getSetting(key) | ||
expect(result).toEqual(value) | ||
}) | ||
|
||
it('getSetting empty', () => { | ||
expect(base!.read()).toBeUndefined() | ||
const result = base!.getSetting(key) | ||
expect(result).toBeUndefined() | ||
}) | ||
}) |
7 changes: 4 additions & 3 deletions
7
...tests/services/skip-data-and-type.test.ts → ...vices/settings/skip-data-and-type.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters