@ordergroove/offers 2.24.0 → 2.24.1-alpha-PR-566-5.3

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/examples/index.js CHANGED
@@ -5,6 +5,16 @@ import { getMarkup, getStyles } from '@ordergroove/offers-templates';
5
5
 
6
6
  // runTests();
7
7
 
8
+ window.og_settings = {
9
+ // object were key is product id and value is an array of plan discount
10
+ product_x_plans: {
11
+ UD729: {
12
+ '3_1': ['30.00', '10%', '$27.00'],
13
+ '1_2': ['30.00', '20%', '$24.00']
14
+ }
15
+ }
16
+ };
17
+
8
18
  const frequencies = [
9
19
  {
10
20
  every: 1,
@@ -325,7 +335,11 @@ Add item to order on
325
335
  {
326
336
  name: 'Tooltip',
327
337
  selector: 'og-offer[location="tooltip-placement"]',
328
- markup: [['top-left', 'top', 'top-right'], ['left', '', 'right'], ['bottom-left', 'bottom', 'bottom-right']]
338
+ markup: [
339
+ ['top-left', 'top', 'top-right'],
340
+ ['left', '', 'right'],
341
+ ['bottom-left', 'bottom', 'bottom-right']
342
+ ]
329
343
  .map(options =>
330
344
  options
331
345
  .map(
@@ -359,7 +373,6 @@ function updateTemplate(target) {
359
373
 
360
374
  const style = document.getElementById('offersGlobalCss');
361
375
  style.innerHTML = '';
362
- console.log('it.config', JSON.parse(data).config, getStyles(JSON.parse(data).config));
363
376
  style.appendChild(document.createTextNode(getStyles(JSON.parse(data).config)));
364
377
 
365
378
  document.getElementById('the-html').innerText = (JSON.parse(data).markup || '').trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/offers",
3
- "version": "2.24.0",
3
+ "version": "2.24.1-alpha-PR-566-5.3+b7a041a1",
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.0"
47
47
  },
48
- "gitHead": "5a104d20766e12bc957a4f052eb293facf8f26e5"
48
+ "gitHead": "b7a041a12a28708064fdbd024d104525fc4be05b"
49
49
  }
@@ -1,16 +1,111 @@
1
- import offers from '../index';
1
+ import { offers } from '../index';
2
+ import { api } from '../core/api';
2
3
 
3
4
  describe('Offers', () => {
5
+ let register, fetchOfferSpy, mockStore;
4
6
  // TODO revisit chunks
5
7
  it('should have setPublicPath method as compatibility', () => {
6
8
  offers.setPublicPath('yum-path');
7
9
  });
8
10
 
9
- xit('should warn if attempting to initialize twice', () => {
10
- const warnSpy = spyOn(console, 'warn');
11
+ beforeEach(() => {
12
+ register = spyOn(offers, 'register');
13
+ fetchOfferSpy = spyOn(api, 'fetchOffer');
14
+ const dispatch = jasmine.createSpy();
15
+ mockStore = {
16
+ getState() {
17
+ return { sessionId: 'xyz' };
18
+ },
19
+ dispatch
20
+ };
21
+
22
+ offers.isReady = false;
23
+ });
24
+
25
+ it('should warn if attempting to initialize twice', () => {
26
+ const warn = spyOn(console, 'warn');
27
+
11
28
  offers.initialize('0e5de2bedc5e11e3a2e4bc764e106cf4', 'staging');
12
- expect(warnSpy).not.toHaveBeenCalled();
29
+ expect(warn).not.toHaveBeenCalled();
30
+
13
31
  offers.initialize('0e5de2bedc5e11e3a2e4bc764e106cf4', 'staging');
14
- expect(warnSpy).toHaveBeenCalledWith('og.offers has been initialized already. Skipping.');
32
+ expect(warn).toHaveBeenCalledWith('og.offers has been initialized already. Skipping.');
33
+ expect(register).toHaveBeenCalledTimes(1);
34
+ });
35
+
36
+ it('should warn if attempting to initialize twice', () => {
37
+ const resolveSettings = spyOn(offers, 'resolveSettings');
38
+ offers.initialize('0e5de2bedc5e11e3a2e4bc764e106cf4', 'staging');
39
+ expect(resolveSettings).toHaveBeenCalledWith(
40
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
41
+ 'staging',
42
+ undefined,
43
+ offers.store
44
+ );
45
+ });
46
+
47
+ describe('offers.resolveSettings', () => {
48
+ it('should fetch offer for single product in pdp', () => {
49
+ offers.resolveSettings('0e5de2bedc5e11e3a2e4bc764e106cf4', 'staging', { product: '123' }, mockStore);
50
+ expect(fetchOfferSpy).toHaveBeenCalledWith(
51
+ 'https://staging.om.ordergroove.com',
52
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
53
+ 'xyz',
54
+ '123',
55
+ 'pdp'
56
+ );
57
+ });
58
+
59
+ it('should fetch offer for each product in cart', () => {
60
+ offers.resolveSettings(
61
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
62
+ 'staging',
63
+ { cart: { products: ['123', '456'] } },
64
+ mockStore
65
+ );
66
+ expect(fetchOfferSpy).toHaveBeenCalledTimes(2);
67
+
68
+ expect(fetchOfferSpy.calls.argsFor(0)).toEqual([
69
+ 'https://staging.om.ordergroove.com',
70
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
71
+ 'xyz',
72
+ '123',
73
+ 'pdp'
74
+ ]);
75
+ expect(fetchOfferSpy.calls.argsFor(1)).toEqual([
76
+ 'https://staging.om.ordergroove.com',
77
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
78
+ 'xyz',
79
+ '456',
80
+ 'pdp'
81
+ ]);
82
+ });
83
+
84
+ it('should set product price and discounts', () => {
85
+ offers.resolveSettings(
86
+ '0e5de2bedc5e11e3a2e4bc764e106cf4',
87
+ 'staging',
88
+ {
89
+ // object were key is product id and value is an array of plan discount
90
+ product_x_plans: {
91
+ 123: {
92
+ '2_2': ['30.00', '20%', '$24.00'],
93
+ '1_3': ['30.00', '10%', '$27.00']
94
+ }
95
+ }
96
+ },
97
+ mockStore
98
+ );
99
+ expect(mockStore.dispatch).toHaveBeenCalledTimes(1);
100
+ expect(mockStore.dispatch).toHaveBeenCalledWith({
101
+ type: 'RECEIVE_PRODUCT_PLANS',
102
+ payload: {
103
+ 123: {
104
+ '2_2': ['30.00', '20%', '$24.00'],
105
+ '1_3': ['30.00', '10%', '$27.00']
106
+ }
107
+ }
108
+ });
109
+ });
15
110
  });
16
111
  });
@@ -134,13 +134,22 @@ export class Offer extends TemplateElement {
134
134
  return `
135
135
  <og-when test="regularEligible">
136
136
  <div>
137
+
137
138
  <og-optout-button>
138
139
  <og-text key="offerOptOutLabel"></og-text>
139
140
  </og-optout-button>
140
141
  </div>
141
142
  <div>
142
143
  <og-optin-button>
143
- <og-text key="offerOptInLabel"></og-text>
144
+
145
+
146
+ <og-price discount>
147
+ <span slot="prepend">Subscribe and get</span>
148
+ <span slot="append">off</span>
149
+ </og-price>
150
+ <og-price regular></og-price>
151
+ <og-price subscription></og-price>
152
+
144
153
  </og-optin-button>
145
154
  <og-tooltip placement="top-right">
146
155
  <div slot="trigger">
@@ -0,0 +1,56 @@
1
+ import { html } from 'lit-element';
2
+ import { connect } from '../core/connect';
3
+
4
+ import { withProduct } from '../core/resolveProperties';
5
+ import { TemplateElement } from '../core/base';
6
+ import { configSelector, makeProductFrequencySelector } from '../core/selectors';
7
+
8
+ export class Price extends withProduct(TemplateElement) {
9
+ static get properties() {
10
+ return {
11
+ ...super.properties,
12
+ regular: { type: Boolean, reflect: true },
13
+ subscription: { type: Boolean, reflect: true },
14
+ discount: { type: Boolean, reflect: true }
15
+ };
16
+ }
17
+
18
+ get value() {
19
+ const frequency = this.frequency || this.defaultFrequency;
20
+ const plans = this.productPlans[this.product.id] || {};
21
+ const currentPlan = plans[frequency] || [];
22
+ if (!currentPlan) return '';
23
+ const [regularPrice, discountRate, subscriptionPrice] = currentPlan;
24
+ if (subscriptionPrice === regularPrice) return '';
25
+
26
+ if (this.regular) {
27
+ this.style.textDecoration = 'line-through';
28
+ return regularPrice;
29
+ }
30
+
31
+ if (this.subscription) return subscriptionPrice;
32
+ if (this.discount) return discountRate;
33
+ return '';
34
+ }
35
+
36
+ render() {
37
+ const value = this.value;
38
+ if (value)
39
+ return html`
40
+ <slot name="prepend"></slot>
41
+ ${value}
42
+ <slot name="append"></slot>
43
+ `;
44
+
45
+ return html`
46
+ <slot></slot>
47
+ `;
48
+ }
49
+ }
50
+ const mapStateToProps = (state, ownProps) => ({
51
+ productPlans: state.productPlans,
52
+ frequency: makeProductFrequencySelector(ownProps.product)(state),
53
+ ...configSelector(state, ownProps, 'defaultFrequency')
54
+ });
55
+
56
+ export default connect(mapStateToProps)(Price);
@@ -2,10 +2,10 @@ const og = window.og;
2
2
  describe('og.offers', function() {
3
3
  it('should define og namespace', () => {
4
4
  expect(og).toEqual(jasmine.any(Object));
5
- expect(og.offers).toEqual(jasmine.any(Function));
5
+ expect(og.offers).toEqual(jasmine.any(Object));
6
6
  });
7
7
 
8
- it('shoul define register() method', () => {
8
+ it('should define register() method', () => {
9
9
  expect(og.offers.register).toEqual(jasmine.any(Function));
10
10
  });
11
11
  });
@@ -0,0 +1,43 @@
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
+ });
@@ -36,3 +36,4 @@ export const LOCAL_STORAGE_CHANGE = 'LOCAL_STORAGE_CHANGE';
36
36
  export const LOCAL_STORAGE_CLEAR = 'LOCAL_STORAGE_CLEAR';
37
37
  export const SET_FIRST_ORDER_PLACE_DATE = 'SET_FIRST_ORDER_PLACE_DATE';
38
38
  export const SET_PRODUCT_TO_SUBSCRIBE = 'SET_PRODUCT_TO_SUBSCRIBE';
39
+ export const RECEIVE_PRODUCT_PLANS = 'RECEIVE_PRODUCT_PLANS';
@@ -443,7 +443,17 @@ export const templates = (state = [], action) => {
443
443
  }
444
444
  };
445
445
 
446
+ export const productPlans = (state = {}, action) => {
447
+ switch (action.type) {
448
+ case constants.RECEIVE_PRODUCT_PLANS:
449
+ return { ...action.payload };
450
+ default:
451
+ return state;
452
+ }
453
+ };
454
+
446
455
  export default combineReducers({
456
+ productPlans,
447
457
  environment,
448
458
  optedin,
449
459
  optedout,