@ordergroove/offers 2.34.1 → 2.34.2-alpha-PR-742-5.9

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.34.1",
3
+ "version": "2.34.2-alpha-PR-742-5.9+959f36c6",
4
4
  "description": "offer state component",
5
5
  "author": "Eugenio Lattanzio <eugenio63@gmail.com>",
6
6
  "homepage": "https://github.com/ordergroove/plush-toys#readme",
@@ -47,5 +47,5 @@
47
47
  "devDependencies": {
48
48
  "@ordergroove/offers-templates": "^0.9.1"
49
49
  },
50
- "gitHead": "3fa30214d839413c66d20b0c3910f29d8b4df0aa"
50
+ "gitHead": "959f36c6e15f1889da33f7f4fc360fb75606e81c"
51
51
  }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { makeStore } from './core/store';
2
2
  import makeApi from './make-api';
3
+ import makeShopifyOverrides from './shopify/makeShopifyOverrides';
3
4
  import defaultReducer from './core/reducer';
4
5
  import shopifyReducer from './shopify/shopifyReducer';
5
6
  import shopifyMiddleware from './shopify/shopifyMiddleware';
@@ -13,7 +14,13 @@ export const store = makeStore(
13
14
  platform.shopify && shopifyTrackingMiddleware
14
15
  );
15
16
 
16
- export const offers = makeApi(store);
17
+ export const offers = Object.assign(
18
+ makeApi(store),
19
+ platform?.shopify && platform?.shopify_selling_plans ? makeShopifyOverrides() : {}
20
+ );
21
+
22
+ window.OG = window.OG || {};
23
+ Object.assign(window.OG, offers);
17
24
 
18
25
  export const isReady = offers.isReady;
19
26
  export const addOptinChangedCallback = offers.addOptinChangedCallback;
package/src/make-api.js CHANGED
@@ -185,8 +185,6 @@ export default function makeApi(store) {
185
185
  }
186
186
  };
187
187
 
188
- window.OG = window.OG || {};
189
- Object.assign(window.OG, offers);
190
188
  Object.assign(offers.initialize, offers);
191
189
 
192
190
  offersLiveEditor(window.opener, offers);
@@ -0,0 +1,56 @@
1
+ import fetchMock from 'fetch-mock';
2
+
3
+ import makeShopifyOverrides from '../makeShopifyOverrides';
4
+ import { CART_JS_URL } from '../shopifyMiddleware';
5
+
6
+ describe('getOptins', () => {
7
+ const cart = {
8
+ items: [
9
+ { variant_id: 123, selling_plan_allocation: { selling_plan: { id: 111 } } },
10
+ { product_id: 456, selling_plan_allocation: { selling_plan: { id: 222 } } },
11
+ { variant_id: 789, selling_plan_allocation: { selling_plan: { id: 333 } } }
12
+ ]
13
+ };
14
+
15
+ beforeEach(() => {
16
+ fetchMock.getOnce(CART_JS_URL, cart);
17
+ });
18
+
19
+ afterEach(() => {
20
+ fetchMock.restore();
21
+ });
22
+
23
+ it('should return all optins given no productIds are provided', async () => {
24
+ const expected = [
25
+ { product: 123, selling_plan: 111 },
26
+ { product: 456, selling_plan: 222 },
27
+ { product: 789, selling_plan: 333 }
28
+ ];
29
+ const shopifyOverrides = makeShopifyOverrides();
30
+ const optins = await shopifyOverrides.getOptins();
31
+
32
+ expect(optins).toEqual(expected);
33
+ expect(fetchMock.called(CART_JS_URL)).toBe(true);
34
+ });
35
+
36
+ it('should return optins for productIds given existant productIds are provided', async () => {
37
+ const expected = [
38
+ { product: 123, selling_plan: 111 },
39
+ { product: 456, selling_plan: 222 }
40
+ ];
41
+ const shopifyOverrides = makeShopifyOverrides();
42
+ const optins = await shopifyOverrides.getOptins([123, 456]);
43
+
44
+ expect(optins).toEqual(expected);
45
+ expect(fetchMock.called(CART_JS_URL)).toBe(true);
46
+ });
47
+
48
+ it('should return an empty array given nonexistant productIDs are provided', async () => {
49
+ const expected = [];
50
+ const shopifyOverrides = makeShopifyOverrides();
51
+ const optins = await shopifyOverrides.getOptins([888, 999]);
52
+
53
+ expect(optins).toEqual(expected);
54
+ expect(fetchMock.called(CART_JS_URL)).toBe(true);
55
+ });
56
+ });
@@ -0,0 +1,25 @@
1
+ import { getCart } from './shopifyMiddleware';
2
+
3
+ export default function makeShopifyOverrides() {
4
+ const shopifyOverrides = {
5
+ async getOptins(productIds = []) {
6
+ const cart = await getCart();
7
+ const { items } = cart;
8
+
9
+ const optins = items.reduce((result, item) => {
10
+ const product = item.variant_id || item.product_id;
11
+ const sellingPlan = item.selling_plan_allocation?.selling_plan?.id;
12
+
13
+ if (!!sellingPlan && (productIds.length === 0 || productIds.includes(product))) {
14
+ result.push({ product, selling_plan: sellingPlan });
15
+ }
16
+
17
+ return result;
18
+ }, []);
19
+
20
+ return optins;
21
+ }
22
+ };
23
+
24
+ return shopifyOverrides;
25
+ }
@@ -20,7 +20,7 @@ import { getTrackingKey } from './shopifyTrackingMiddleware';
20
20
 
21
21
  const SHOPIFY_ROOT = window.Shopify?.routes?.root || '/';
22
22
  const CART_PAGE_URL = '/cart';
23
- const CART_JS_URL = `${SHOPIFY_ROOT}cart.js`;
23
+ export const CART_JS_URL = `${SHOPIFY_ROOT}cart.js`;
24
24
  const PRODUCTS_URL = `${SHOPIFY_ROOT}products/`;
25
25
 
26
26
  /**
@@ -79,7 +79,7 @@ async function setupPdp(store, offer) {
79
79
  }
80
80
  }
81
81
 
82
- const getCart = async () => await (await fetch(CART_JS_URL)).json();
82
+ export const getCart = async () => await (await fetch(CART_JS_URL)).json();
83
83
 
84
84
  /**
85
85
  * Attemps to guess the product handle o