|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable max-nested-callbacks */ |
| 7 | +/*jscs:disable jsDoc*/ |
| 8 | +define([ |
| 9 | + 'squire', 'jquery', 'ko' |
| 10 | +], function (Squire, $, ko) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + describe('Magento_Customer/js/customer-global-session-loader', function () { |
| 14 | + var injector = new Squire(), |
| 15 | + customer = ko.observable({}), |
| 16 | + mocks = { |
| 17 | + 'Magento_Customer/js/customer-data': { |
| 18 | + get: jasmine.createSpy('get', function () { |
| 19 | + return customer; |
| 20 | + }).and.callThrough(), |
| 21 | + reload: jasmine.createSpy(), |
| 22 | + getInitCustomerData: function () {} |
| 23 | + } |
| 24 | + }, |
| 25 | + deferred, |
| 26 | + customerSessionLoader; |
| 27 | + |
| 28 | + beforeEach(function (done) { |
| 29 | + $('body').append('<div id="customerMenu" class="customer-menu">Customer Menu</div>'); |
| 30 | + injector.mock(mocks); |
| 31 | + injector.require(['Magento_Customer/js/customer-global-session-loader'], function (instance) { |
| 32 | + customerSessionLoader = instance; |
| 33 | + done(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(function () { |
| 38 | + try { |
| 39 | + injector.clean(); |
| 40 | + injector.remove(); |
| 41 | + } catch (e) {} |
| 42 | + |
| 43 | + customer({}); |
| 44 | + $('#customerMenu').remove(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('Check customer data preparation process', function () { |
| 48 | + it('Tests that customer data is NOT checked before initialization', function () { |
| 49 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () { |
| 50 | + deferred = $.Deferred(); |
| 51 | + |
| 52 | + return deferred.promise(); |
| 53 | + }); |
| 54 | + expect(customerSessionLoader()).toBe(undefined); |
| 55 | + |
| 56 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer'); |
| 57 | + expect(mocks['Magento_Customer/js/customer-data'].getInitCustomerData).toHaveBeenCalled(); |
| 58 | + expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Tests that customer data reloads if customer first name is not there', function () { |
| 62 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () { |
| 63 | + deferred = $.Deferred(); |
| 64 | + |
| 65 | + deferred.resolve(); |
| 66 | + |
| 67 | + return deferred.promise(); |
| 68 | + }); |
| 69 | + customer({ |
| 70 | + _data: null |
| 71 | + }); |
| 72 | + customerSessionLoader(); |
| 73 | + |
| 74 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer'); |
| 75 | + expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith([], false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Tests that customer data is checked only after initialization', function () { |
| 79 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () { |
| 80 | + deferred = $.Deferred(); |
| 81 | + |
| 82 | + return deferred.promise(); |
| 83 | + }); |
| 84 | + customer({ |
| 85 | + firstname: "First Name" |
| 86 | + }); |
| 87 | + customerSessionLoader(); |
| 88 | + |
| 89 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer'); |
| 90 | + expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled(); |
| 91 | + |
| 92 | + deferred.resolve(); |
| 93 | + |
| 94 | + expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith([], false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('Tests that customer data does not reloads if it has first name defined', function () { |
| 98 | + spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () { |
| 99 | + deferred = $.Deferred(); |
| 100 | + |
| 101 | + deferred.resolve(); |
| 102 | + |
| 103 | + return deferred.promise(); |
| 104 | + }); |
| 105 | + customer({ |
| 106 | + firstname: "First Name" |
| 107 | + }); |
| 108 | + customerSessionLoader(); |
| 109 | + |
| 110 | + expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer'); |
| 111 | + expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments