From f8574bed5677c72024366882746d30782d9ef25e Mon Sep 17 00:00:00 2001 From: Christopher Allford Date: Thu, 1 Oct 2020 15:23:46 -0700 Subject: [PATCH] Added a utility service for updating settings --- tests/e2e/api/src/index.ts | 1 + .../__tests__/setting-service.spec.ts | 51 +++++++++++++++++++ tests/e2e/api/src/services/index.ts | 1 + tests/e2e/api/src/services/setting-service.ts | 46 +++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 tests/e2e/api/src/services/__tests__/setting-service.spec.ts create mode 100644 tests/e2e/api/src/services/index.ts create mode 100644 tests/e2e/api/src/services/setting-service.ts diff --git a/tests/e2e/api/src/index.ts b/tests/e2e/api/src/index.ts index 781edafc99c..0130a8e0b57 100644 --- a/tests/e2e/api/src/index.ts +++ b/tests/e2e/api/src/index.ts @@ -1,2 +1,3 @@ export { HTTPClientFactory } from './http'; export * from './models'; +export * from './services'; diff --git a/tests/e2e/api/src/services/__tests__/setting-service.spec.ts b/tests/e2e/api/src/services/__tests__/setting-service.spec.ts new file mode 100644 index 00000000000..518cf9e438f --- /dev/null +++ b/tests/e2e/api/src/services/__tests__/setting-service.spec.ts @@ -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' }, + ); + } ); +} ); diff --git a/tests/e2e/api/src/services/index.ts b/tests/e2e/api/src/services/index.ts new file mode 100644 index 00000000000..532393127a5 --- /dev/null +++ b/tests/e2e/api/src/services/index.ts @@ -0,0 +1 @@ +export { SettingService } from './setting-service'; diff --git a/tests/e2e/api/src/services/setting-service.ts b/tests/e2e/api/src/services/setting-service.ts new file mode 100644 index 00000000000..97789c60002 --- /dev/null +++ b/tests/e2e/api/src/services/setting-service.ts @@ -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.} 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 ); + } +}