Skip to content
This repository was archived by the owner on Oct 16, 2019. It is now read-only.

Commit d0fe385

Browse files
author
Gleb Galkin
committed
fix(SalesChannel): in case of bad sales_channel getLocale() throws error
#122
1 parent 2a4702a commit d0fe385

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/api/__tests__/config_service_tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ test('Should return current locale, language and country code according to Sales
4444

4545
});
4646

47+
test('Should throw error if sales_channel is bad', async t => {
48+
fetchMock.get('https://atlas-config-api.dc.zalan.do/api/config/CLIENT_ID-staging.json', configJson);
49+
50+
const sdk = await AtlasSDK.configure({
51+
client_id: 'CLIENT_ID',
52+
sales_channel: 'SALES_CHANNEL_BAD',
53+
is_sandbox: true
54+
}).catch(error => {
55+
t.fail(error);
56+
});
57+
58+
const error = t.throws(() => {
59+
sdk.getLocale();
60+
}, Error);
61+
62+
t.is(error.message, 'Cannot get locale for SALES_CHANNEL_BAD sales channel. This sales channel does not exists.');
63+
});
64+
4765
test.afterEach.always(() => {
4866
fetchMock.restore();
4967
});

src/api/atlas_sdk_client.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,16 @@ class AtlasSDKClient {
9797
* Returns locale based on sales channel, e.g. 'de_DE'
9898
* @method
9999
* @return {String} locale
100+
* @throws Error - if sales channel doesn't exists for the client id, an Error is thrown.
100101
*/
101102
getLocale() {
102-
return this.config.salesChannels.find(sc => sc.channel === this.config.salesChannel).locale;
103+
const salesChannel = this.config.salesChannels.find(sc => sc.channel === this.config.salesChannel);
104+
105+
if (salesChannel) {
106+
return salesChannel.locale;
107+
}
108+
109+
throw new Error(`Cannot get locale for ${this.config.salesChannel} sales channel. This sales channel does not exists.`);
103110
}
104111

105112
/**

0 commit comments

Comments
 (0)