Added a utility service for updating settings

This commit is contained in:
Christopher Allford 2020-10-01 15:23:46 -07:00
parent f0ad6e4fc2
commit f8574bed56
4 changed files with 99 additions and 0 deletions

View File

@ -1,2 +1,3 @@
export { HTTPClientFactory } from './http';
export * from './models';
export * from './services';

View File

@ -0,0 +1,51 @@
import { mock, MockProxy } from 'jest-mock-extended';
import { UpdatesSettings } from '../../models/settings/setting';
import { SettingService } from '../setting-service';
describe( 'SettingService', () => {
let repository: MockProxy< UpdatesSettings >;
let service: SettingService;
beforeEach( () => {
repository = mock< UpdatesSettings >();
service = new SettingService( repository );
} );
it( 'should update address', async () => {
const result = await service.updateStoreAddress(
'line1',
'line2',
'New York',
'US:NY',
'12345',
);
expect( result ).toBeTruthy();
expect( repository.update ).toHaveBeenCalledTimes( 5 );
expect( repository.update ).toHaveBeenCalledWith(
{ settingGroupID: 'general' },
'woocommerce_store_address',
{ value: 'line1' },
);
expect( repository.update ).toHaveBeenCalledWith(
{ settingGroupID: 'general' },
'woocommerce_store_address_2',
{ value: 'line2' },
);
expect( repository.update ).toHaveBeenCalledWith(
{ settingGroupID: 'general' },
'woocommerce_store_city',
{ value: 'New York' },
);
expect( repository.update ).toHaveBeenCalledWith(
{ settingGroupID: 'general' },
'woocommerce_default_country',
{ value: 'US:NY' },
);
expect( repository.update ).toHaveBeenCalledWith(
{ settingGroupID: 'general' },
'woocommerce_store_postcode',
{ value: '12345' },
);
} );
} );

View File

@ -0,0 +1 @@
export { SettingService } from './setting-service';

View File

@ -0,0 +1,46 @@
import { Setting, UpdatesSettings } from '../models/settings/setting';
/**
* A service that wraps setting changes in convenient methods.
*/
export class SettingService {
/**
* The repository that will be used to change the settings.
*
* @type {UpdatesSettings}
* @private
*/
private readonly repository: UpdatesSettings;
/**
* Creates a new service class for easily changing store settings.
*
* @param {UpdatesSettings} repository The repository that will be used to change the settings.
*/
public constructor( repository: UpdatesSettings ) {
this.repository = repository;
}
/**
* Updates the address for the store.
*
* @param {string} address1 The first address line.
* @param {string} address2 The second address line.
* @param {string} city The city.
* @param {string} country The country or country/state.
* @param {string} postCode The postal code.
* @return {Promise.<boolean>} Resolves to true if all of the settings are updated.
*/
public updateStoreAddress( address1: string, address2: string, city: string, country: string, postCode: string ): Promise< boolean > {
const promises: Promise< Setting >[] = [];
const parent = { settingGroupID: 'general' };
promises.push( this.repository.update( parent, 'woocommerce_store_address', { value: address1 } ) );
promises.push( this.repository.update( parent, 'woocommerce_store_address_2', { value: address2 } ) );
promises.push( this.repository.update( parent, 'woocommerce_store_city', { value: city } ) );
promises.push( this.repository.update( parent, 'woocommerce_default_country', { value: country } ) );
promises.push( this.repository.update( parent, 'woocommerce_store_postcode', { value: postCode } ) );
return Promise.all( promises ).then( () => true );
}
}