generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
settings.test.js
182 lines (163 loc) · 6.58 KB
/
settings.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Env } from '../src/external.js';
import { Landscape, WCS_PROD_URL, WCS_STAGE_URL } from '../src/constants.js';
import { Defaults } from '../src/defaults.js';
import { getSettings } from '../src/settings.js';
import { expect } from './utilities.js';
import { PARAM_ENV, PARAM_LANDSCAPE } from '../src/constants.js';
describe('getSettings', () => {
let href;
after(() => {
document.head.querySelectorAll('meta').forEach((meta) => meta.remove());
window.history.replaceState({}, '', href);
});
afterEach(() => {
window.sessionStorage.clear();
window.history.replaceState({}, '', href);
});
before(() => {
({ href } = window.location);
});
it('returns default settings, if called without arguments', () => {
expect(getSettings()).to.deep.equal({
...Defaults,
locale: `${Defaults.language}_${Defaults.country}`,
quantity: [Defaults.quantity],
});
});
it('overrides with search parameters', () => {
const checkoutClientId = 'adobecom';
const checkoutWorkflowStep = 'segmentation';
const promotionCode = 'nicopromo';
const url = new URL(window.location.href);
url.searchParams.set('checkoutClientId', checkoutClientId);
url.searchParams.set('checkoutWorkflowStep', checkoutWorkflowStep);
url.searchParams.set('promotionCode', promotionCode);
url.searchParams.set('displayOldPrice', 'false');
url.searchParams.set('displayPerUnit', 'true');
url.searchParams.set('displayRecurrence', 'false');
url.searchParams.set('displayTax', 'true');
url.searchParams.set('entitlement', 'true');
url.searchParams.set('modal', 'true');
url.searchParams.set('commerce.landscape', 'DRAFT');
url.searchParams.set('commerce.env', 'STAGE');
url.searchParams.set('wcsBufferDelay', '30');
url.searchParams.set('wcsBufferLimit', '5');
url.searchParams.set('quantity', '2');
url.searchParams.set('wcsApiKey', 'testapikey');
window.history.replaceState({}, '', url.toString());
const config = { commerce: {}, env: { name: 'stage' }, };
expect(
getSettings(config),
).to.deep.equal({
...Defaults,
checkoutClientId,
checkoutWorkflowStep,
promotionCode,
displayOldPrice: false,
displayPerUnit: true,
displayRecurrence: false,
displayTax: true,
entitlement: true,
modal: true,
landscape: 'DRAFT',
wcsBufferDelay: 30,
wcsBufferLimit: 5,
quantity: [2],
wcsApiKey: 'testapikey',
locale: "en_US",
env: "STAGE",
wcsURL: WCS_STAGE_URL
});
});
it('uses document metadata and storage', () => {
const wcsApiKey = 'wcs-api-key';
const meta = document.createElement('meta');
meta.content = wcsApiKey;
meta.name = 'wcs-api-key';
document.head.append(meta);
window.sessionStorage.setItem(PARAM_ENV, 'stage');
const commerce = {
forceTaxExclusive: true,
promotionCode: 'promo1',
'commerce.landscape': 'DRAFT',
};
expect(
getSettings({
commerce,
env: { name: 'stage' },
locale: { prefix: '/no' },
}),
).to.deep.equal({
...Defaults,
forceTaxExclusive: true,
promotionCode: 'promo1',
country: 'NO',
env: Env.STAGE,
language: 'nb',
locale: 'nb_NO',
quantity: [Defaults.quantity],
wcsApiKey,
wcsURL: WCS_STAGE_URL,
landscape: Landscape.DRAFT,
});
window.sessionStorage.removeItem(PARAM_ENV);
});
it('host env "local" -> WCS prod origin + prod akamai', () => {
const config = { commerce: {}, env: { name: 'local' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_PROD_URL);
expect(settings.env).to.equal(Env.PRODUCTION);
});
it('host env "stage" -> WCS prod origin + prod akamai', () => {
const config = { commerce: {}, env: { name: 'stage' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_PROD_URL);
expect(settings.env).to.equal(Env.PRODUCTION);
});
it('host env "prod" -> WCS prod origin + prod akamai', () => {
const config = { commerce: {}, env: { name: 'prod' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_PROD_URL);
expect(settings.env).to.equal(Env.PRODUCTION);
});
it('host env "local" - override landscape and WCS origin (_stage)', () => {
window.sessionStorage.setItem(PARAM_ENV, 'stage');
window.sessionStorage.setItem(PARAM_LANDSCAPE, 'DRAFT');
const config = { commerce: {}, env: { name: 'local' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_STAGE_URL);
expect(settings.landscape).to.equal(Landscape.DRAFT);
expect(settings.env).to.equal(Env.STAGE);
});
it('host env "stage" - override landscape and WCS origin (_stage)', () => {
window.sessionStorage.setItem(PARAM_ENV, 'stage');
window.sessionStorage.setItem(PARAM_LANDSCAPE, 'DRAFT');
const config = { commerce: {}, env: { name: 'stage' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_STAGE_URL);
expect(settings.landscape).to.equal(Landscape.DRAFT);
expect(settings.env).to.equal(Env.STAGE);
});
it('if host env is "prod" - cant override landscape or WCS origin', () => {
window.sessionStorage.setItem(PARAM_ENV, 'stage');
window.sessionStorage.setItem(PARAM_LANDSCAPE, 'DRAFT');
const config = { commerce: {}, env: { name: 'prod' }, };
const settings = getSettings(config);
expect(settings.wcsURL).to.equal(WCS_PROD_URL);
expect(settings.landscape).to.equal(Landscape.PUBLISHED);
expect(settings.env).to.equal(Env.PRODUCTION);
});
[
{ prefix: '/ar', expectedLocale: 'es_AR' },
{ prefix: '/africa', expectedLocale: 'en_MU' },
{ prefix: '', expectedLocale: 'en_US' },
{ prefix: '/ae_ar', expectedLocale: 'ar_AE' },
].forEach(({ prefix, expectedLocale }) => {
it(`returns correct locale for "${prefix}"`, () => {
const wcsLocale = getSettings({
locale: { prefix },
}).locale;
expect(wcsLocale).to.be.equal(expectedLocale);
});
});
});