|
| 1 | +/************************************************************************ |
| 2 | + * |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained |
| 13 | + * from Adobe. |
| 14 | + * ************************************************************************ |
| 15 | + */ |
| 16 | + |
| 17 | +/* eslint-disable max-nested-callbacks */ |
| 18 | +define([ |
| 19 | + 'squire', |
| 20 | + 'underscore' |
| 21 | +], function (Squire, _) { |
| 22 | + 'use strict'; |
| 23 | + |
| 24 | + var injector = new Squire(), |
| 25 | + customerData, |
| 26 | + productResolver, |
| 27 | + productResolverIds, |
| 28 | + storage, |
| 29 | + mocks, |
| 30 | + obj, |
| 31 | + localStorageIds, |
| 32 | + namespace = 'namespace', |
| 33 | + windowCheckoutData, |
| 34 | + timestamp; |
| 35 | + |
| 36 | + beforeEach(function (done) { |
| 37 | + customerData = {}; |
| 38 | + productResolverIds = []; |
| 39 | + productResolver = jasmine.createSpy().and.callFake(function () { |
| 40 | + return productResolverIds; |
| 41 | + }); |
| 42 | + storage = { |
| 43 | + onStorageInit: jasmine.createSpy(), |
| 44 | + createStorage: jasmine.createSpy() |
| 45 | + }; |
| 46 | + mocks = { |
| 47 | + 'Magento_Customer/js/customer-data': customerData, |
| 48 | + 'Magento_Catalog/js/product/view/product-ids-resolver': productResolver, |
| 49 | + 'Magento_Catalog/js/product/storage/storage-service': storage |
| 50 | + }; |
| 51 | + injector.mock(mocks); |
| 52 | + injector.require(['Magento_Catalog/js/product/provider'], function (UiClass) { |
| 53 | + timestamp = new Date().getTime() / 1000; |
| 54 | + obj = new UiClass({ |
| 55 | + identifiersConfig: { |
| 56 | + namespace: namespace |
| 57 | + }, |
| 58 | + ids: { |
| 59 | + 'website-1-1': { |
| 60 | + added_at: timestamp - 300, |
| 61 | + product_id: 1, |
| 62 | + scope_id: 1 |
| 63 | + } |
| 64 | + }, |
| 65 | + data: { |
| 66 | + store: '1', |
| 67 | + currency: 'USD', |
| 68 | + productCurrentScope: 'website' |
| 69 | + } |
| 70 | + }); |
| 71 | + localStorageIds = { |
| 72 | + 'website-1-2': { |
| 73 | + added_at: timestamp - 60, |
| 74 | + product_id: 2, |
| 75 | + scope_id: 1 |
| 76 | + }, |
| 77 | + 'website-1-3': { |
| 78 | + added_at: timestamp - 120, |
| 79 | + product_id: 3, |
| 80 | + scope_id: 1 |
| 81 | + } |
| 82 | + }; |
| 83 | + done(); |
| 84 | + }); |
| 85 | + windowCheckoutData = window.checkout; |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(function () { |
| 89 | + try { |
| 90 | + injector.clean(); |
| 91 | + injector.remove(); |
| 92 | + } catch (e) { |
| 93 | + } |
| 94 | + window.localStorage.clear(); |
| 95 | + window.checkout = windowCheckoutData; |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Magento_Catalog/js/product/provider', function () { |
| 99 | + describe('"_resolveDataByIds" method', function () { |
| 100 | + beforeEach(function () { |
| 101 | + obj.initIdsListener = jasmine.createSpy(); |
| 102 | + obj.idsMerger = jasmine.createSpy(); |
| 103 | + obj.idsHandler = jasmine.createSpy(); |
| 104 | + obj.filterIds = jasmine.createSpy().and.returnValue({}); |
| 105 | + }); |
| 106 | + it('check "window.checkout" is required', function () { |
| 107 | + window.checkout = undefined; |
| 108 | + obj.ids = jasmine.createSpy(); |
| 109 | + |
| 110 | + obj._resolveDataByIds(); |
| 111 | + |
| 112 | + expect(obj.ids).not.toHaveBeenCalled(); |
| 113 | + expect(obj.filterIds).not.toHaveBeenCalled(); |
| 114 | + expect(obj.idsHandler).not.toHaveBeenCalled(); |
| 115 | + expect(obj.initIdsListener).not.toHaveBeenCalled(); |
| 116 | + expect(obj.idsMerger).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + it('check that initial ids, localstorage ids and ids from customer data are processed', function () { |
| 119 | + var initialIds, |
| 120 | + customerDataIds, |
| 121 | + customerDataGet; |
| 122 | + |
| 123 | + initialIds = obj.ids(); |
| 124 | + customerDataIds = { |
| 125 | + 4: {}, |
| 126 | + 6: {} |
| 127 | + }; |
| 128 | + customerDataGet = function () { |
| 129 | + return { |
| 130 | + items: customerDataIds |
| 131 | + }; |
| 132 | + }; |
| 133 | + window.checkout = { |
| 134 | + baseUrl: 'http://localhost/', |
| 135 | + websiteId: 1 |
| 136 | + }; |
| 137 | + obj.idsStorage = { |
| 138 | + get: jasmine.createSpy().and.returnValue(localStorageIds), |
| 139 | + lifetime: 1000 |
| 140 | + }; |
| 141 | + obj.prepareDataFromCustomerData = jasmine.createSpy().and.returnValue(customerDataIds); |
| 142 | + customerData.get = jasmine.createSpy().and.returnValue(customerDataGet); |
| 143 | + |
| 144 | + obj._resolveDataByIds(); |
| 145 | + |
| 146 | + expect(obj.filterIds).toHaveBeenCalledOnceWith(initialIds); |
| 147 | + expect(obj.idsHandler).toHaveBeenCalledOnceWith({}); |
| 148 | + expect(obj.initIdsListener).toHaveBeenCalled(); |
| 149 | + expect(customerData.get).toHaveBeenCalledOnceWith(namespace); |
| 150 | + expect(obj.prepareDataFromCustomerData).toHaveBeenCalledOnceWith({items: customerDataIds}); |
| 151 | + expect(obj.idsMerger).toHaveBeenCalledOnceWith(localStorageIds, customerDataIds); |
| 152 | + }); |
| 153 | + }); |
| 154 | + describe('"idsMerger" method', function () { |
| 155 | + beforeEach(function () { |
| 156 | + obj.idsHandler = jasmine.createSpy(); |
| 157 | + obj.filterIds = jasmine.createSpy().and.returnValue({}); |
| 158 | + }); |
| 159 | + it('check merge empty', function () { |
| 160 | + obj.idsMerger({}, {}); |
| 161 | + |
| 162 | + expect(obj.filterIds).not.toHaveBeenCalled(); |
| 163 | + expect(obj.idsHandler).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('check merge not empty', function () { |
| 167 | + var initialIds = obj.ids(); |
| 168 | + |
| 169 | + obj.idsMerger(localStorageIds, {}); |
| 170 | + |
| 171 | + expect(obj.filterIds).toHaveBeenCalledOnceWith(_.extend({}, initialIds, localStorageIds)); |
| 172 | + expect(obj.idsHandler).toHaveBeenCalledOnceWith({}); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('"prepareDataFromCustomerData" method', function () { |
| 177 | + it('argument is empty', function () { |
| 178 | + expect(obj.prepareDataFromCustomerData({})).toEqual({}); |
| 179 | + }); |
| 180 | + |
| 181 | + it('argument is an object and has "items" property', function () { |
| 182 | + expect(obj.prepareDataFromCustomerData({items: {1: {}, 2: {}}})).toEqual({1: {}, 2: {}}); |
| 183 | + }); |
| 184 | + |
| 185 | + it('argument is an object and does not have "items" property', function () { |
| 186 | + expect(obj.prepareDataFromCustomerData({1: {}, 2: {}})).toEqual({1: {}, 2: {}}); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('"filterIds" method', function () { |
| 191 | + beforeEach(function () { |
| 192 | + window.checkout = { |
| 193 | + websiteId: 1 |
| 194 | + }; |
| 195 | + obj.idsStorage = { |
| 196 | + lifetime: 1000 |
| 197 | + }; |
| 198 | + }); |
| 199 | + |
| 200 | + it('filters out "out of scope" ids', function () { |
| 201 | + window.checkout.websiteId = 2; |
| 202 | + expect(obj.filterIds(localStorageIds)).toEqual({}); |
| 203 | + }); |
| 204 | + |
| 205 | + it('filters out expired ids', function () { |
| 206 | + obj.idsStorage.lifetime = 70; |
| 207 | + expect(obj.filterIds(localStorageIds)).toEqual({2: localStorageIds['website-1-2']}); |
| 208 | + }); |
| 209 | + |
| 210 | + it('filters out current product id', function () { |
| 211 | + productResolverIds.push(2); |
| 212 | + expect(obj.filterIds(localStorageIds)).toEqual({3: localStorageIds['website-1-3']}); |
| 213 | + }); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments