@ordergroove/offers 2.27.14 → 2.27.15-alpha-PR-654-7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/offers",
3
- "version": "2.27.14",
3
+ "version": "2.27.15-alpha-PR-654-7.6+a8f4a9ff",
4
4
  "description": "offer state component",
5
5
  "author": "Eugenio Lattanzio <eugenio63@gmail.com>",
6
6
  "homepage": "https://github.com/ordergroove/plush-toys#readme",
@@ -45,5 +45,5 @@
45
45
  "devDependencies": {
46
46
  "@ordergroove/offers-templates": "^0.4.15"
47
47
  },
48
- "gitHead": "087e5b417f9d3468b05a89ae4f0a5dd4d10115ca"
48
+ "gitHead": "a8f4a9ffa12e4f7162f40d9f01dc9081de3ad6fe"
49
49
  }
@@ -248,7 +248,7 @@ export class Offer extends TemplateElement {
248
248
  }
249
249
  this.frequency = this.defaultFrequency;
250
250
 
251
- if (changed.has('product') && this.product.id && !this.isPreview) {
251
+ if (changed.has('product') && !this.isPreview) {
252
252
  onReady(() => this.fetchOffer(this.product.id, DEFAULT_OFFER_MODULE, this));
253
253
  }
254
254
 
@@ -31,9 +31,8 @@ export class When extends withProduct(LitElement) {
31
31
  shouldUpdate(changedProperties) {
32
32
  return (
33
33
  changedProperties.size &&
34
- this.product &&
35
- this.product.id in this.state.autoshipEligible &&
36
- this.product.id in this.state.inStock
34
+ ((this.product && this.product.id in this.state.autoshipEligible && this.product.id in this.state.inStock) ||
35
+ !this.product.id)
37
36
  );
38
37
  }
39
38
  }
@@ -1,4 +1,44 @@
1
1
  const og = window.og;
2
+
3
+ async function simulateChange(element, value) {
4
+ const evt = new Event('change', { bubbles: true });
5
+ element.value = value;
6
+ element.dispatchEvent(evt, { target: { value } });
7
+ await new Promise(r => setTimeout(r, 1));
8
+ }
9
+
10
+ const expectToBeVisible = offer => {
11
+ const { x, y, width, height } = offer.getClientRects()[0];
12
+ expect(height).toBeGreaterThan(30);
13
+ };
14
+ const expectNotToBeVisible = offer => {
15
+ const { x, y, width, height } = offer.getClientRects()[0];
16
+ expect(height).toBeLessThan(30);
17
+ };
18
+ const waitUpdate = async offer => {
19
+ await offer.updateComplete;
20
+ await new Promise(r => setTimeout(r, 10));
21
+ };
22
+ const mockOfferResponse = (productId, inStock = true, autoship = true) => {
23
+ return Promise.resolve({
24
+ json() {
25
+ return Promise.resolve({
26
+ in_stock: { [productId]: inStock },
27
+ eligibility_groups: { [productId]: ['subscription', 'upsell'] },
28
+ result: 'success',
29
+ autoship: { [productId]: autoship },
30
+ autoship_by_default: { [productId]: false },
31
+ modifiers: {},
32
+ module_view: { regular: '096135e6650111e9a444bc764e106cf4' },
33
+ incentives_display: {},
34
+ incentives: {
35
+ [productId]: { initial: [], ongoing: [] }
36
+ }
37
+ });
38
+ }
39
+ });
40
+ };
41
+
2
42
  describe('og.offers', function() {
3
43
  it('should define og namespace', () => {
4
44
  expect(og).toEqual(jasmine.any(Object));
@@ -33,3 +73,141 @@ describe('og.offers', function() {
33
73
  expect(og.offers).toEqual(api);
34
74
  });
35
75
  });
76
+
77
+ describe('Offer', function() {
78
+ let productIdNotInStock;
79
+ let productIdInStock;
80
+ let fetch;
81
+
82
+ beforeEach(async () => {
83
+ fetch = spyOn(window, 'fetch');
84
+ productIdNotInStock = `oos${Math.random()
85
+ .toString(36)
86
+ .substring(2, 10)}`;
87
+ productIdInStock = `s-${Math.random()
88
+ .toString(36)
89
+ .substring(2, 10)}`;
90
+ });
91
+
92
+ it('should show offer when instock and eligible', async () => {
93
+ fetch.and.returnValue(mockOfferResponse(productIdInStock, true, true));
94
+ document.body.innerHTML = `
95
+ <og-offer product="${productIdInStock}">
96
+ <og-select-frequency default-text=" (recomended)">
97
+ <option value="optedOut">Buy one time</option>
98
+ <option value="2w" selected="selected">2 weeks</option>
99
+ <option value="1m">1 month </option>
100
+ </og-select-frequency>
101
+ </og-offer>
102
+ `;
103
+ const offer = document.querySelector('og-offer');
104
+ await waitUpdate(offer);
105
+ expect(fetch).toHaveBeenCalledTimes(1);
106
+ expectToBeVisible(document.querySelector('og-offer'));
107
+ });
108
+
109
+ it('should not show offer when not in stock', async () => {
110
+ fetch.and.returnValue(mockOfferResponse(productIdNotInStock, false, false));
111
+ document.body.innerHTML = `
112
+ <og-offer product="${productIdNotInStock}">
113
+ <og-when test="regularEligible">
114
+ <og-select-frequency default-text=" (recomended)">
115
+ <option value="optedOut">Buy one time</option>
116
+ <option value="2w" selected="selected">2 weeks</option>
117
+ <option value="1m">1 month </option>
118
+ </og-select-frequency>
119
+ </og-when>
120
+ </og-offer>
121
+ `;
122
+ const offer = document.querySelector('og-offer');
123
+ await waitUpdate(offer);
124
+ expect(fetch).toHaveBeenCalledTimes(1);
125
+ expectNotToBeVisible(document.querySelector('og-offer'));
126
+ });
127
+
128
+ it('should show hide offer based on variants in stock or oos', async () => {
129
+ fetch.and.returnValue(mockOfferResponse(productIdNotInStock, false, true));
130
+ document.body.innerHTML = `
131
+ <og-offer product="${productIdNotInStock}">
132
+ <og-when test="regularEligible">
133
+ <og-select-frequency default-text=" (recomended)">
134
+ <option value="optedOut">Buy one time</option>
135
+ <option value="2w" selected="selected">2 weeks</option>
136
+ <option value="1m">1 month </option>
137
+ </og-select-frequency>
138
+ </og-when>
139
+ </og-offer>
140
+ `;
141
+ const offer = document.querySelector('og-offer');
142
+
143
+ await waitUpdate(offer);
144
+ expect(fetch).toHaveBeenCalledTimes(1);
145
+ expectNotToBeVisible(offer);
146
+
147
+ fetch.and.returnValue(mockOfferResponse(productIdInStock, true, true));
148
+ offer.setAttribute('product', productIdInStock);
149
+ await waitUpdate(offer);
150
+ expect(fetch).toHaveBeenCalledTimes(2);
151
+ expectToBeVisible(offer);
152
+ });
153
+
154
+ it('should hide when no product id', async () => {
155
+ fetch.and.returnValue(mockOfferResponse(productIdInStock, true, true));
156
+ document.body.innerHTML = `
157
+ <og-offer product="${productIdInStock}">
158
+ <og-when test="regularEligible">
159
+ <og-select-frequency default-text=" (recomended)">
160
+ <option value="optedOut">Buy one time</option>
161
+ <option value="2w" selected="selected">2 weeks</option>
162
+ <option value="1m">1 month </option>
163
+ </og-select-frequency>
164
+ </og-when>
165
+ </og-offer>
166
+ `;
167
+ const offer = document.querySelector('og-offer');
168
+ await waitUpdate(offer);
169
+ expect(fetch).toHaveBeenCalledTimes(1);
170
+ expectToBeVisible(offer);
171
+
172
+ offer.removeAttribute('product');
173
+ await waitUpdate(offer);
174
+ expectNotToBeVisible(offer);
175
+ });
176
+
177
+ describe('Select Frequency', function() {
178
+ let element;
179
+ beforeEach(async () => {
180
+ fetch.and.returnValue(mockOfferResponse(productIdInStock));
181
+ document.body.innerHTML = `
182
+ <og-offer product="${productIdInStock}">
183
+ <og-select-frequency default-text=" (recomended)">
184
+ <option value="optedOut">Buy one time</option>
185
+ <option value="2_2" selected="selected">2 weeks</option>
186
+ <option value="1_3">1 month </option>
187
+ </og-select-frequency>
188
+ </og-offer>
189
+ `;
190
+ element = document.querySelector('og-select-frequency');
191
+ await element.updateComplete;
192
+ await new Promise(r => setTimeout(r, 10));
193
+ expect(fetch).toHaveBeenCalledTimes(1);
194
+ });
195
+
196
+ it('it should have default frequency as value', async () => {
197
+ const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
198
+ expect(htmlSelectElement.value).toEqual('2_2');
199
+ });
200
+
201
+ it('should append recomended text to default frequency', async () => {
202
+ const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
203
+ expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
204
+ });
205
+
206
+ it('should not append recomended text to clicked frequency', async () => {
207
+ const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
208
+ await simulateChange(htmlSelectElement, '1_3');
209
+ expect(htmlSelectElement.value).toEqual('1_3');
210
+ expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
211
+ });
212
+ });
213
+ });
@@ -1,7 +1,7 @@
1
1
  import { receiveOffer, receiveOrders, authorize, unauthorized, optinProduct, optoutProduct } from './actions';
2
2
  import * as constants from './constants';
3
3
 
4
- export const setPreviewStandardOffer = (isPreview, productId) =>
4
+ export const setPreviewStandardOffer = (isPreview, productId, offer) =>
5
5
  async function setPreviewStandardOfferThunk(dispatch) {
6
6
  await dispatch({
7
7
  type: constants.SET_PREVIEW_STANDARD_OFFER,
@@ -11,69 +11,77 @@ export const setPreviewStandardOffer = (isPreview, productId) =>
11
11
  type: constants.UNAUTHORIZED
12
12
  });
13
13
  await dispatch(
14
- receiveOffer({
15
- in_stock: { [productId]: true },
16
- eligibility_groups: { [productId]: ['subscription', 'upsell'] },
17
- result: 'success',
18
- autoship: { [productId]: true },
19
- modifiers: {},
20
- module_view: { regular: '096135e6650111e9a444bc764e106cf4' },
21
- incentives_display: {
22
- '47c01e9aacbe40389b5c7325d79091aa': {
23
- field: 'sub_total',
24
- object: 'order',
25
- type: 'Discount Percent',
26
- value: 5
27
- },
28
- e6534b9d877f41e586c37b7d8abc3a58: {
29
- field: 'total_price',
30
- object: 'item',
31
- type: 'Discount Percent',
32
- value: 10
33
- },
34
- f35e842710b24929922db4a529eecd40: {
35
- field: 'total_price',
36
- object: 'item',
37
- type: 'Discount Percent',
38
- value: 10
14
+ receiveOffer(
15
+ {
16
+ in_stock: { [productId]: true },
17
+ eligibility_groups: { [productId]: ['subscription', 'upsell'] },
18
+ result: 'success',
19
+ autoship: { [productId]: true },
20
+ autoship_by_default: { [productId]: false },
21
+ modifiers: {},
22
+ module_view: { regular: '096135e6650111e9a444bc764e106cf4' },
23
+ incentives_display: {
24
+ '47c01e9aacbe40389b5c7325d79091aa': {
25
+ field: 'sub_total',
26
+ object: 'order',
27
+ type: 'Discount Percent',
28
+ value: 5
29
+ },
30
+ e6534b9d877f41e586c37b7d8abc3a58: {
31
+ field: 'total_price',
32
+ object: 'item',
33
+ type: 'Discount Percent',
34
+ value: 10
35
+ },
36
+ f35e842710b24929922db4a529eecd40: {
37
+ field: 'total_price',
38
+ object: 'item',
39
+ type: 'Discount Percent',
40
+ value: 10
41
+ },
42
+ '5be321d7c17f4e18a757212b9a20bfcc': {
43
+ field: 'total_price',
44
+ object: 'item',
45
+ type: 'Discount Percent',
46
+ value: 1
47
+ }
39
48
  },
40
- '5be321d7c17f4e18a757212b9a20bfcc': {
41
- field: 'total_price',
42
- object: 'item',
43
- type: 'Discount Percent',
44
- value: 1
49
+ incentives: {
50
+ [productId]: {
51
+ initial: ['5be321d7c17f4e18a757212b9a20bfcc'],
52
+ ongoing: [
53
+ 'e6534b9d877f41e586c37b7d8abc3a58',
54
+ '47c01e9aacbe40389b5c7325d79091aa',
55
+ 'f35e842710b24929922db4a529eecd40'
56
+ ]
57
+ }
45
58
  }
46
59
  },
47
- incentives: {
48
- [productId]: {
49
- initial: ['5be321d7c17f4e18a757212b9a20bfcc'],
50
- ongoing: [
51
- 'e6534b9d877f41e586c37b7d8abc3a58',
52
- '47c01e9aacbe40389b5c7325d79091aa',
53
- 'f35e842710b24929922db4a529eecd40'
54
- ]
55
- }
56
- }
57
- })
60
+ offer
61
+ )
58
62
  );
59
63
  };
60
64
 
61
- export const setPreviewUpsellOffer = (isPreview, productId) =>
65
+ export const setPreviewUpsellOffer = (isPreview, productId, offer) =>
62
66
  async function setPreviewUpsellOfferThunk(dispatch, getState) {
63
67
  await dispatch({ type: constants.SET_PREVIEW_UPSELL_OFFER, payload: isPreview });
64
68
 
65
69
  const { merchantId } = getState();
66
70
  if (isPreview) {
67
71
  await dispatch(
68
- receiveOffer({
69
- in_stock: { [productId]: true },
70
- module_view: { regular: '096135e6650111e9a444bc764e106cf4' },
71
- default_frequencies: { [productId]: { every: 1, every_period: 3 } },
72
- eligibility_groups: { [productId]: ['subscription', 'upsell'] },
73
- result: 'success',
74
- autoship: { [productId]: true },
75
- modifiers: {}
76
- })
72
+ receiveOffer(
73
+ {
74
+ in_stock: { [productId]: true },
75
+ module_view: { regular: '096135e6650111e9a444bc764e106cf4' },
76
+ default_frequencies: { [productId]: { every: 1, every_period: 3 } },
77
+ eligibility_groups: { [productId]: ['subscription', 'upsell'] },
78
+ result: 'success',
79
+ autoship: { [productId]: true },
80
+ autoship_by_default: { [productId]: false },
81
+ modifiers: {}
82
+ },
83
+ offer
84
+ )
77
85
  );
78
86
  await dispatch(
79
87
  receiveOrders({
@@ -114,7 +122,7 @@ export const setPreviewUpsellOffer = (isPreview, productId) =>
114
122
  }
115
123
  };
116
124
 
117
- export const setPreview = (value, oldValue, ownProps) =>
125
+ export const setPreview = (value, oldValue, offer) =>
118
126
  async function(dispatch, getState) {
119
127
  await dispatch({ type: constants.LOCAL_STORAGE_CLEAR });
120
128
  await dispatch({
@@ -128,14 +136,14 @@ export const setPreview = (value, oldValue, ownProps) =>
128
136
 
129
137
  switch (value) {
130
138
  case 'regular':
131
- dispatch(setPreviewStandardOffer(true, ownProps.product.id));
139
+ dispatch(setPreviewStandardOffer(true, offer.product.id, offer));
132
140
  break;
133
141
  case 'upsell':
134
- dispatch(setPreviewUpsellOffer(true, ownProps.product.id));
142
+ dispatch(setPreviewUpsellOffer(true, offer.product.id, offer));
135
143
  break;
136
144
  case 'subscribed':
137
- dispatch(setPreviewStandardOffer(true, ownProps.product.id));
138
- dispatch(optinProduct(ownProps.product, '2_2'));
145
+ dispatch(setPreviewStandardOffer(true, offer.product.id, offer));
146
+ dispatch(optinProduct(offer.product, '2_2'));
139
147
  break;
140
148
  default:
141
149
  }
@@ -197,7 +197,7 @@ export const fetchOffer = (product, module = constants.DEFAULT_OFFER_MODULE, off
197
197
  dispatch(requestAction);
198
198
 
199
199
  const productId = safeProductId(product);
200
-
200
+ if (!productId) return null;
201
201
  return api
202
202
  .fetchOffer(apiUrl, merchantId, sessionId, productId, module)
203
203
  .then(
@@ -31,7 +31,7 @@ export const upcomingOrderContainsProduct = (state, ownProps) =>
31
31
  */
32
32
  export const upsellEligible = (state, ownProps) =>
33
33
  // don't show IU in cart offers
34
- !ownProps.offer.isCart &&
34
+ !ownProps.offer?.isCart &&
35
35
  state.offerId &&
36
36
  state.offerId !== '0' &&
37
37
  state.auth &&
@@ -111,6 +111,9 @@ export const autoshipEligible = (state = {}, action) => {
111
111
 
112
112
  export const inStock = (state = {}, action) => {
113
113
  switch (action.type) {
114
+ // force offer to refresh when requesting a new one
115
+ case constants.REQUEST_OFFER:
116
+ return { ...state };
114
117
  case constants.RECEIVE_OFFER:
115
118
  return {
116
119
  ...state,
package/src/core/utils.ts CHANGED
@@ -36,6 +36,7 @@ export function resolveEnvAndMerchant() {
36
36
  }
37
37
 
38
38
  export const safeProductId = product => {
39
+ if (!product ) return '';
39
40
  let productId = `${product.id || product}`;
40
41
  if (platform?.shopify_selling_plans) {
41
42
  // we can't avoid make offer request since we need to know the upsell group and autoship by default
@@ -2,3 +2,4 @@ window.OG_SAVE_TO_LOCAL_TIMEOUT = 1;
2
2
  window.merchantId = '12345678901234567890123456789012';
3
3
  window.env = 'staging';
4
4
  window.og.offers.initialize(window.merchantId, window.env, 0);
5
+ localStorage.clear();
@@ -168,6 +168,10 @@ export const inStock = (state = {}, action) => {
168
168
 
169
169
  return [product, ...product?.variants]?.reduce(productOrVariantInStockReducer, state) || state;
170
170
  }
171
+ // force offer to refresh when requesting a new one
172
+ if (constants.REQUEST_OFFER === action.type && action.payload.product === null) {
173
+ return { ...state };
174
+ }
171
175
 
172
176
  return state;
173
177
  };
@@ -1,43 +0,0 @@
1
- const og = window.og;
2
-
3
- async function simulateChange(element, value) {
4
- const evt = new Event('change', { bubbles: true });
5
- element.value = value;
6
- element.dispatchEvent(evt, { target: { value } });
7
- await new Promise(r => setTimeout(r, 1));
8
- }
9
-
10
- describe('Select Frequency', function() {
11
- let element;
12
- beforeEach(async () => {
13
- og.offers.clear();
14
- document.body.innerHTML = `
15
- <og-offer product="123" preview-standard-offer>
16
- <og-select-frequency default-text=" (recomended)">
17
- <option value="optedOut">Buy one time</option>
18
- <option value="2w" selected="selected">2 weeks</option>
19
- <option value="1m">1 month </option>
20
- </og-select-frequency>
21
- </og-offer>
22
- `;
23
- element = document.querySelector('og-select-frequency');
24
- await element.updateComplete;
25
- });
26
-
27
- it('it should have default frequency as value', async () => {
28
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
29
- expect(htmlSelectElement.value).toEqual('2_2');
30
- });
31
-
32
- it('should append recomended text to default frequency', async () => {
33
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
34
- expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
35
- });
36
-
37
- it('should not append recomended text to clicked frequency', async () => {
38
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
39
- await simulateChange(htmlSelectElement, '1_3');
40
- expect(htmlSelectElement.value).toEqual('1_3');
41
- expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
42
- });
43
- });
@@ -1,44 +0,0 @@
1
- const og = window.og;
2
-
3
- async function simulateChange(element, value) {
4
- const evt = new Event('change', { bubbles: true });
5
- element.value = value;
6
- element.dispatchEvent(evt, { target: { value } });
7
- await new Promise(r => setTimeout(r, 1));
8
- }
9
-
10
- describe('Select Frequency', function() {
11
- let element;
12
- beforeEach(async () => {
13
- og.offers.clear();
14
- document.body.innerHTML = `
15
- <og-offer product="123" preview-standard-offer>
16
- <og-select-frequency default-text=" (recomended)">
17
- <option value="optedOut">Buy one time</option>
18
- <option value="2w" selected="selected">2 weeks</option>
19
- <option value="1m">1 month </option>
20
- </og-select-frequency>
21
- </og-offer>
22
- `;
23
- element = document.querySelector('og-select-frequency');
24
- await element.updateComplete;
25
- await new Promise(r => setTimeout(r, 1000));
26
- });
27
-
28
- it('it should have default frequency as value', async () => {
29
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
30
- expect(htmlSelectElement.value).toEqual('2_2');
31
- });
32
-
33
- it('should append recomended text to default frequency', async () => {
34
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
35
- expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
36
- });
37
-
38
- it('should not append recomended text to clicked frequency', async () => {
39
- const htmlSelectElement = element.shadowRoot.querySelector('og-select').shadowRoot.querySelector('select');
40
- await simulateChange(htmlSelectElement, '1_3');
41
- expect(htmlSelectElement.value).toEqual('1_3');
42
- expect(htmlSelectElement.innerText).toEqual('Buy one time\n2 weeks (recomended)\n1 month');
43
- });
44
- });