mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-13 11:03:50 +00:00
Added unit tests for settings business logic elements
This commit is contained in:
46
test/settings/services/SettingsService.test.js
Normal file
46
test/settings/services/SettingsService.test.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import SettingsService from '../../../src/settings/services/SettingsService';
|
||||
|
||||
describe('SettingsService', () => {
|
||||
const settings = { foo: 'bar' };
|
||||
const createService = (withSettings = true) => {
|
||||
const storageMock = {
|
||||
set: jest.fn(),
|
||||
get: jest.fn(() => withSettings ? settings : undefined),
|
||||
};
|
||||
const service = new SettingsService(storageMock);
|
||||
|
||||
return [ service, storageMock ];
|
||||
};
|
||||
|
||||
afterEach(jest.resetAllMocks);
|
||||
|
||||
describe('loadSettings', () => {
|
||||
it.each([
|
||||
[ false, {}],
|
||||
[ true, settings ],
|
||||
])('returns result if found in storage', (withSettings, expectedResult) => {
|
||||
const [ service, storageMock ] = createService(withSettings);
|
||||
|
||||
const result = service.loadSettings();
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.set).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateSettings', () => {
|
||||
it.each([
|
||||
[ false, { hi: 'goodbye' }, { hi: 'goodbye' }],
|
||||
[ true, { hi: 'goodbye' }, { foo: 'bar', hi: 'goodbye' }],
|
||||
[ true, { foo: 'goodbye' }, { foo: 'goodbye' }],
|
||||
])('appends provided data to existing settings', (withSettings, settingsToUpdate, expectedResult) => {
|
||||
const [ service, storageMock ] = createService(withSettings);
|
||||
|
||||
service.updateSettings(settingsToUpdate);
|
||||
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user