@magento/experience-platform-connector 1.0.0-alpha.1

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.
Files changed (56) hide show
  1. package/intercept.js +24 -0
  2. package/package.json +30 -0
  3. package/src/__tests__/__snapshots__/utils.spec.js.snap +137 -0
  4. package/src/__tests__/utils.spec.js +52 -0
  5. package/src/config.js +31 -0
  6. package/src/handleEvent.js +9 -0
  7. package/src/handlers/__tests__/__mocks__/cartAddItem.js +36 -0
  8. package/src/handlers/__tests__/__mocks__/cartPageView.js +880 -0
  9. package/src/handlers/__tests__/__mocks__/categoryPageView.js +9 -0
  10. package/src/handlers/__tests__/__mocks__/checkoutPageView.js +138 -0
  11. package/src/handlers/__tests__/__mocks__/completeCheckout.js +156 -0
  12. package/src/handlers/__tests__/__mocks__/createAccount.js +9 -0
  13. package/src/handlers/__tests__/__mocks__/editAccount.js +9 -0
  14. package/src/handlers/__tests__/__mocks__/miniCartView.js +846 -0
  15. package/src/handlers/__tests__/__mocks__/orderConfirmationPageView.js +67 -0
  16. package/src/handlers/__tests__/__mocks__/pageView.js +7 -0
  17. package/src/handlers/__tests__/__mocks__/placeOrderButtonClicked.js +161 -0
  18. package/src/handlers/__tests__/__mocks__/productPageView.js +15 -0
  19. package/src/handlers/__tests__/__mocks__/searchPageRequest.js +22 -0
  20. package/src/handlers/__tests__/__mocks__/searchRequestSent.js +34 -0
  21. package/src/handlers/__tests__/__mocks__/searchResponseReceived.js +106 -0
  22. package/src/handlers/__tests__/__snapshots__/shoppingMiniCartView.spec.js.snap +137 -0
  23. package/src/handlers/__tests__/addToCart.spec.js +73 -0
  24. package/src/handlers/__tests__/categoryPageView.spec.js +55 -0
  25. package/src/handlers/__tests__/completeCheckout.spec.js +70 -0
  26. package/src/handlers/__tests__/createAccount.spec.js +43 -0
  27. package/src/handlers/__tests__/editAccount.spec.js +43 -0
  28. package/src/handlers/__tests__/pageView.spec.js +46 -0
  29. package/src/handlers/__tests__/placeOrder.spec.js +55 -0
  30. package/src/handlers/__tests__/productPageView.spec.js +64 -0
  31. package/src/handlers/__tests__/searchRequestSent.spec.js +119 -0
  32. package/src/handlers/__tests__/searchResponseReceived.spec.js +143 -0
  33. package/src/handlers/__tests__/shoppingCartPageView.spec.js +198 -0
  34. package/src/handlers/__tests__/shoppingMiniCartView.spec.js +36 -0
  35. package/src/handlers/__tests__/signIn.spec.js +73 -0
  36. package/src/handlers/__tests__/startCheckout.spec.js +193 -0
  37. package/src/handlers/addToCart.js +60 -0
  38. package/src/handlers/categoryPageView.js +34 -0
  39. package/src/handlers/completeCheckout.js +49 -0
  40. package/src/handlers/createAccount.js +25 -0
  41. package/src/handlers/editAccount.js +25 -0
  42. package/src/handlers/pageView.js +26 -0
  43. package/src/handlers/placeOrder.js +32 -0
  44. package/src/handlers/productPageView.js +41 -0
  45. package/src/handlers/searchRequestSent.js +38 -0
  46. package/src/handlers/searchResponseReceived.js +39 -0
  47. package/src/handlers/shoppingCartPageView.js +45 -0
  48. package/src/handlers/shoppingMiniCartView.js +31 -0
  49. package/src/handlers/signIn.js +29 -0
  50. package/src/handlers/startCheckout.js +45 -0
  51. package/src/hooks/useExtensionContext.js +21 -0
  52. package/src/main.js +116 -0
  53. package/src/queries/getExtensionContext.js +31 -0
  54. package/src/utils.js +101 -0
  55. package/src/wrappers/wrapUseAccountMenu.js +29 -0
  56. package/src/wrappers/wrapUseAutocomplete.js +48 -0
package/intercept.js ADDED
@@ -0,0 +1,24 @@
1
+ module.exports = targets => {
2
+ const { talons } = targets.of('@magento/peregrine');
3
+ const { specialFeatures } = targets.of('@magento/pwa-buildpack');
4
+
5
+ specialFeatures.tap(flags => {
6
+ /**
7
+ * Wee need to activate esModules, cssModules and GQL Queries to allow build pack to load our extension
8
+ * {@link https://magento.github.io/pwa-studio/pwa-buildpack/reference/configure-webpack/#special-flags}.
9
+ */
10
+ flags[targets.name] = {
11
+ esModules: true
12
+ };
13
+ });
14
+
15
+ talons.tap(({ App, Header, SearchBar }) => {
16
+ App.useApp.wrapWith('@magento/experience-platform-connector');
17
+ Header.useAccountMenu.wrapWith(
18
+ '@magento/experience-platform-connector/src/wrappers/wrapUseAccountMenu'
19
+ );
20
+ SearchBar.useAutocomplete.wrapWith(
21
+ '@magento/experience-platform-connector/src/wrappers/wrapUseAutocomplete'
22
+ );
23
+ });
24
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@magento/experience-platform-connector",
3
+ "version": "1.0.0-alpha.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Sends storefront events to the Adobe Experience Platform",
8
+ "main": "./src/main.js",
9
+ "scripts": {
10
+ "clean": " "
11
+ },
12
+ "repository": "github:magento/pwa-studio",
13
+ "license": "(OSL-3.0 OR AFL-3.0)",
14
+ "dependencies": {
15
+ "@adobe/magento-storefront-event-collector": "~1.1.13",
16
+ "@adobe/magento-storefront-events-sdk": "~1.1.13"
17
+ },
18
+ "devDependencies": {},
19
+ "peerDependencies": {
20
+ "@apollo/client": "~3.6.6",
21
+ "@magento/peregrine": "~12.3.0",
22
+ "@magento/pwa-buildpack": "~11.2.0",
23
+ "react": "~17.0.1"
24
+ },
25
+ "pwa-studio": {
26
+ "targets": {
27
+ "intercept": "./intercept"
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,137 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`getFormattedProducts() returns correctly formatted data 1`] = `
4
+ Array [
5
+ Object {
6
+ "configurableOptions": Array [
7
+ Object {
8
+ "id": 157,
9
+ "optionLabel": "Fashion Color",
10
+ "valueId": "Y29uZmlndXJhYmxlLzE1Ny8zMQ==",
11
+ "valueLabel": "Peach",
12
+ },
13
+ Object {
14
+ "id": 190,
15
+ "optionLabel": "Fashion Size",
16
+ "valueId": "Y29uZmlndXJhYmxlLzE5MC80Mw==",
17
+ "valueLabel": "L",
18
+ },
19
+ ],
20
+ "formattedPrice": "",
21
+ "id": "MjQ2Ng==",
22
+ "prices": Object {
23
+ "__typename": "CartItemPrices",
24
+ "price": Object {
25
+ "__typename": "Money",
26
+ "currency": "USD",
27
+ "value": 78,
28
+ },
29
+ "row_total": Object {
30
+ "__typename": "Money",
31
+ "value": 78,
32
+ },
33
+ "total_item_discount": Object {
34
+ "__typename": "Money",
35
+ "value": 0,
36
+ },
37
+ },
38
+ "product": Object {
39
+ "canonicalUrl": "rowena-skirt",
40
+ "mainImageUrl": "https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/609faca36a4bc16a754bc2f43c184970/v/s/vsk02-ll_main_2.jpg",
41
+ "name": "Rowena Skirt",
42
+ "pricing": Object {
43
+ "currencyCode": "USD",
44
+ "maximalPrice": 78,
45
+ "minimalPrice": 78,
46
+ "regularPrice": 78,
47
+ },
48
+ "productType": "ConfigurableProduct",
49
+ "sku": "VSK02",
50
+ },
51
+ "quantity": 1,
52
+ },
53
+ Object {
54
+ "configurableOptions": null,
55
+ "formattedPrice": "",
56
+ "id": "MjQ4Mg==",
57
+ "prices": Object {
58
+ "__typename": "CartItemPrices",
59
+ "price": Object {
60
+ "__typename": "Money",
61
+ "currency": "USD",
62
+ "value": 68,
63
+ },
64
+ "row_total": Object {
65
+ "__typename": "Money",
66
+ "value": 136,
67
+ },
68
+ "total_item_discount": Object {
69
+ "__typename": "Money",
70
+ "value": 0,
71
+ },
72
+ },
73
+ "product": Object {
74
+ "canonicalUrl": "silver-cirque-earrings",
75
+ "mainImageUrl": "https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/609faca36a4bc16a754bc2f43c184970/v/a/va17-si_main.jpg",
76
+ "name": "Silver Cirque Earrings",
77
+ "pricing": Object {
78
+ "currencyCode": "USD",
79
+ "maximalPrice": 68,
80
+ "minimalPrice": 68,
81
+ "regularPrice": 68,
82
+ },
83
+ "productType": "SimpleProduct",
84
+ "sku": "VA17-SI-NA",
85
+ },
86
+ "quantity": 2,
87
+ },
88
+ Object {
89
+ "configurableOptions": Array [
90
+ Object {
91
+ "id": 157,
92
+ "optionLabel": "Fashion Color",
93
+ "valueId": "Y29uZmlndXJhYmxlLzE1Ny8zMQ==",
94
+ "valueLabel": "Peach",
95
+ },
96
+ Object {
97
+ "id": 190,
98
+ "optionLabel": "Fashion Size",
99
+ "valueId": "Y29uZmlndXJhYmxlLzE5MC80NA==",
100
+ "valueLabel": "M",
101
+ },
102
+ ],
103
+ "formattedPrice": "",
104
+ "id": "MjQ4Mw==",
105
+ "prices": Object {
106
+ "__typename": "CartItemPrices",
107
+ "price": Object {
108
+ "__typename": "Money",
109
+ "currency": "USD",
110
+ "value": 48,
111
+ },
112
+ "row_total": Object {
113
+ "__typename": "Money",
114
+ "value": 144,
115
+ },
116
+ "total_item_discount": Object {
117
+ "__typename": "Money",
118
+ "value": 0,
119
+ },
120
+ },
121
+ "product": Object {
122
+ "canonicalUrl": "antonia-infinity-scarf",
123
+ "mainImageUrl": "https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/609faca36a4bc16a754bc2f43c184970/v/a/va04-ll_main_2.jpg",
124
+ "name": "Antonia Infinity Scarf",
125
+ "pricing": Object {
126
+ "currencyCode": "USD",
127
+ "maximalPrice": 48,
128
+ "minimalPrice": 48,
129
+ "regularPrice": 48,
130
+ },
131
+ "productType": "ConfigurableProduct",
132
+ "sku": "VA04",
133
+ },
134
+ "quantity": 3,
135
+ },
136
+ ]
137
+ `;
@@ -0,0 +1,52 @@
1
+ import { getCartTotal, getCurrency, getFormattedProducts } from '../utils';
2
+
3
+ import mockEvent from '../handlers/__tests__/__mocks__/cartPageView';
4
+
5
+ describe('getCartTotal', () => {
6
+ it('returns the correct sum for a populated array', () => {
7
+ const total = getCartTotal(mockEvent.payload.products);
8
+
9
+ expect(total).toEqual(358);
10
+ });
11
+
12
+ it('returns zero for an empty array', () => {
13
+ const total = getCartTotal([]);
14
+
15
+ expect(total).toEqual(0);
16
+ });
17
+
18
+ it('returns zero for a null parameter', () => {
19
+ const total = getCartTotal();
20
+
21
+ expect(total).toEqual(0);
22
+ });
23
+ });
24
+
25
+ describe('getCurrency()', () => {
26
+ it('returns the correct currency code from the first array entry', () => {
27
+ const currency = getCurrency(mockEvent.payload.products);
28
+
29
+ expect(currency).toMatchInlineSnapshot(`"USD"`);
30
+ });
31
+
32
+ it('returns null if the array is empty or null', () => {
33
+ expect(getCurrency([])).toBeNull();
34
+ expect(getCurrency()).toBeNull();
35
+ });
36
+ });
37
+
38
+ describe('getFormattedProducts()', () => {
39
+ it('returns correctly formatted data', () => {
40
+ const formattedData = getFormattedProducts(mockEvent.payload.products);
41
+
42
+ expect(formattedData).toMatchSnapshot();
43
+ });
44
+
45
+ it('returns an empty array when given an empty array', () => {
46
+ expect(getFormattedProducts([])).toEqual([]);
47
+ });
48
+
49
+ it('returns null when given a null value', () => {
50
+ expect(getFormattedProducts()).toBeNull();
51
+ });
52
+ });
package/src/config.js ADDED
@@ -0,0 +1,31 @@
1
+ import { default as addToCartHandler } from './handlers/addToCart';
2
+ import { default as categoryPageViewHandler } from './handlers/categoryPageView';
3
+ import { default as completeCheckoutHandler } from './handlers/completeCheckout';
4
+ import { default as createAccountHandler } from './handlers/createAccount';
5
+ import { default as editAccountHandler } from './handlers/editAccount';
6
+ import { default as pageViewHandler } from './handlers/pageView';
7
+ import { default as placeOrderHandler } from './handlers/placeOrder';
8
+ import { default as productPageViewHandler } from './handlers/productPageView';
9
+ import { default as searchRequestSentHandler } from './handlers/searchRequestSent';
10
+ import { default as searchResponseReceivedHandler } from './handlers/searchResponseReceived';
11
+ import { default as shoppingCartPageViewHandler } from './handlers/shoppingCartPageView';
12
+ import { default as shoppingMiniCartViewHandler } from './handlers/shoppingMiniCartView';
13
+ import { default as startCheckoutHandler } from './handlers/startCheckout';
14
+ import { default as signInHandler } from './handlers/signIn';
15
+
16
+ export default [
17
+ addToCartHandler,
18
+ categoryPageViewHandler,
19
+ completeCheckoutHandler,
20
+ createAccountHandler,
21
+ editAccountHandler,
22
+ pageViewHandler,
23
+ placeOrderHandler,
24
+ productPageViewHandler,
25
+ searchRequestSentHandler,
26
+ searchResponseReceivedHandler,
27
+ shoppingCartPageViewHandler,
28
+ shoppingMiniCartViewHandler,
29
+ startCheckoutHandler,
30
+ signInHandler
31
+ ];
@@ -0,0 +1,9 @@
1
+ import handlers from './config';
2
+
3
+ export default (sdk, event) => {
4
+ handlers.forEach(({ canHandle, handle }) => {
5
+ if (canHandle(event)) {
6
+ handle(sdk, event);
7
+ }
8
+ });
9
+ };
@@ -0,0 +1,36 @@
1
+ export const addConfigurableProductEvent = {
2
+ type: 'CART_ADD_ITEM',
3
+ payload: {
4
+ cartId: 'kAR5Gg6uPC6J5wGY0ebecyKfX905epmU',
5
+ sku: 'VSK03',
6
+ name: 'Johanna Skirt',
7
+ priceTotal: 78,
8
+ currencyCode: 'USD',
9
+ discountAmount: 0,
10
+ selectedOptions: [
11
+ {
12
+ attribute: 'Fashion Color',
13
+ value: 'Peach'
14
+ },
15
+ {
16
+ attribute: 'Fashion Size',
17
+ value: 'M'
18
+ }
19
+ ],
20
+ quantity: 1
21
+ }
22
+ };
23
+
24
+ export const addSimpleProductEvent = {
25
+ type: 'CART_ADD_ITEM',
26
+ payload: {
27
+ cartId: 'kAR5Gg6uPC6J5wGY0ebecyKfX905epmU',
28
+ sku: 'VA15-SI-NA',
29
+ name: 'Silver Sol Earrings',
30
+ priceTotal: 48,
31
+ currencyCode: 'USD',
32
+ discountAmount: 0,
33
+ selectedOptions: [],
34
+ quantity: 1
35
+ }
36
+ };