@ordergroove/offers 2.33.1 → 2.33.2

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.33.1",
3
+ "version": "2.33.2",
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.8.0"
49
49
  },
50
- "gitHead": "9efba8c79207132f927c2bff1f3ee4c5eaa11eda"
50
+ "gitHead": "771e0978a0b4025ce0a250e35a976e22cd1f09b2"
51
51
  }
@@ -16,9 +16,16 @@ export class When extends withProduct(LitElement) {
16
16
  }
17
17
 
18
18
  render() {
19
- const result =
20
- this.test &&
21
- expression.parse(removeWhitespace(this.test), key => descriptors[key] && descriptors[key](this.state, this));
19
+ if (!this.test) {
20
+ return html``;
21
+ }
22
+
23
+ let test = removeWhitespace(this.test);
24
+ // the parser returns incorrect result if you use an expression with &!, e.g. test1&!test2
25
+ // it succeeds if you wrap !test2 in parentheses, e.g. test1&(!test2)
26
+ // so we'll wrap all "not" expressions in parentheses
27
+ test = test.replace(/(![a-zA-Z]+)/g, '($1)');
28
+ const result = expression.parse(test, key => descriptors[key] && descriptors[key](this.state, this));
22
29
 
23
30
  if (result) {
24
31
  return html`
@@ -12,38 +12,62 @@ class When extends WhenBase {
12
12
  }
13
13
  customElements.define('og-when-test', When);
14
14
 
15
+ const PRODUCT_ID = 'prodA';
16
+
17
+ function stateToString(state) {
18
+ return Object.keys(state)
19
+ .map(key => `${key}=${state[key][PRODUCT_ID]}`)
20
+ .join(' and ');
21
+ }
22
+
23
+ async function testWhen(expression, productState, expected) {
24
+ const element = new When();
25
+ element.product = { id: PRODUCT_ID };
26
+ element.test = expression;
27
+ element.state = Object.keys(productState).reduce((acc, key) => {
28
+ acc[key] = { [PRODUCT_ID]: productState[key] };
29
+ return acc;
30
+ }, {});
31
+ await appendToBody(element).then(() => {
32
+ const slot = querySelector(element, 'slot');
33
+ const expectedMessage = `expected ${expression} to be ${expected} when ${stateToString(element.state)}`;
34
+ if (expected) {
35
+ expect(slot).toBeTruthy(expectedMessage);
36
+ } else {
37
+ expect(slot).toBeFalsy(expectedMessage);
38
+ }
39
+ });
40
+ }
41
+
15
42
  describe('Conditional', () => {
16
43
  it('should not render child given test result is false', async () => {
17
- const element = new When();
18
- element.product = { id: 'prodA' };
19
- element.test = 'inStock';
20
- element.state = {
21
- inStock: {
22
- prodA: false
23
- },
24
- autoshipEligible: {
25
- prodA: false
26
- }
27
- };
28
- await appendToBody(element);
29
-
30
- expect(querySelector(element, 'slot')).toBeFalsy();
44
+ await testWhen('inStock', { inStock: false, autoshipEligible: false }, false);
31
45
  });
32
46
 
33
47
  it('should render child given test result is true', async () => {
34
- const element = new When();
35
- element.product = { id: 'prodA' };
36
- element.test = 'inStock';
37
- element.state = {
38
- inStock: {
39
- prodA: true
40
- },
41
- autoshipEligible: {
42
- prodA: true
43
- }
44
- };
45
- await appendToBody(element);
46
-
47
- expect(querySelector(element, 'slot')).toBeTruthy();
48
+ await testWhen('inStock', { inStock: true, autoshipEligible: false }, true);
49
+ });
50
+
51
+ it('should handle compound expressions', async () => {
52
+ await testWhen('inStock&!eligible', { inStock: false, autoshipEligible: false }, false);
53
+ await testWhen('inStock&(!eligible)', { inStock: false, autoshipEligible: false }, false);
54
+ await testWhen('inStock|!eligible', { inStock: false, autoshipEligible: false }, true);
55
+ await testWhen('inStock|eligible', { inStock: false, autoshipEligible: true }, true);
56
+ await testWhen('!inStock&!eligible', { inStock: false, autoshipEligible: false }, true);
57
+ await testWhen(
58
+ 'inStock&eligible&autoshipByDefault',
59
+ { inStock: false, autoshipEligible: true, autoshipbyDefault: true },
60
+ false
61
+ );
62
+ await testWhen(
63
+ 'inStock&eligible&autoshipByDefault',
64
+ { inStock: true, autoshipEligible: true, autoshipbyDefault: true },
65
+ false
66
+ );
67
+ await testWhen(
68
+ 'inStock&(!eligible|autoshipByDefault)',
69
+ { inStock: true, autoshipEligible: true, autoshipByDefault: true },
70
+ true
71
+ );
48
72
  });
49
73
  });