Skip to content

Commit

Permalink
Updated customerData jasmine test to work when whole test suite is ru…
Browse files Browse the repository at this point in the history
…n and fixed an issue with jquery/jquery-storageapi where object may not exist yet.
  • Loading branch information
Greg Harvell committed Jul 23, 2020
1 parent 1505a36 commit ff99146
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@
/* eslint max-nested-callbacks: 0 */

define([
'squire'
], function (Squire) {
'squire',
'jquery',
'jquery/jquery-storageapi'
], function (Squire, $) {
'use strict';

var injector = new Squire(),
originalGetJSON,
storage,
storageInvalidation = {},
sectionConfig,
obj;

beforeEach(function (done) {
injector.require(['Magento_Customer/js/customer-data'], function (Constr) {
originalGetJSON = $.getJSON;
obj = Constr;
done();
});
});
describe('Magento_Customer/js/customer-data', function () {

afterEach(function () {
try {
injector.clean();
injector.remove();
$.getJSON = originalGetJSON;
} catch (e) {
}
});
beforeEach(function (done) {
injector.require([
'Magento_Customer/js/customer-data',
'Magento_Customer/js/section-config'
], function (Constr, sectionConfiguration) {
obj = Constr;
sectionConfig = sectionConfiguration;
done();
});
});

describe('Magento_Customer/js/customer-data', function () {
afterEach(function () {
try {
injector.clean();
injector.remove();
} catch (e) {
}
});

describe('"init" method', function () {
beforeEach(function () {
spyOn(obj, "reload").and.returnValue(true);

storageInvalidation = {
var storageInvalidation = {
keys: function () {
return ['section'];
}
}

var dataProvider = {
getFromStorage: function (sections) {
},
dataProvider = {
getFromStorage: function () {
return ['section'];
}
};

},
storage = {
keys: function () {
return ['section'];
}
};

beforeEach(function () {
spyOn(obj, "reload").and.returnValue(true);
spyOn($, 'initNamespaceStorage').and.returnValue(true);
spyOn(dataProvider, "getFromStorage");
spyOn(storage, "keys").and.returnValue(['section']);
spyOn(storageInvalidation, "keys").and.returnValue(['section']);
Expand Down Expand Up @@ -124,15 +124,17 @@ define([
});

it('Does not throw before component is initialized', function () {
_.each = jasmine.createSpy().and.returnValue(true);

expect(function () {
obj.set('cart', {});
obj.set();
}).not.toThrow();
});
});

describe('"reload" method', function () {
beforeEach(function () {
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

deferred.promise().done = function () {
Expand Down Expand Up @@ -160,7 +162,9 @@ define([
it('Returns proper sections object when passed array with a single section name', function () {
var result;

$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['section']);

jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

deferred.promise().done = function () {
Expand Down Expand Up @@ -190,7 +194,9 @@ define([
it('Returns proper sections object when passed array with a multiple section names', function () {
var result;

$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['cart,customer,messages']);

jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

expect(parameters).toEqual(jasmine.objectContaining({
Expand Down Expand Up @@ -224,7 +230,7 @@ define([
it('Returns all sections when passed wildcard string', function () {
var result;

$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

expect(parameters).toEqual(jasmine.objectContaining({
Expand All @@ -246,7 +252,7 @@ define([

result = obj.reload('*', true);

expect($.getJSON).toHaveBeenCalled();
expect(jQuery.getJSON).toHaveBeenCalled();
expect(result).toEqual(jasmine.objectContaining({
responseJSON: {
cart: {},
Expand All @@ -261,6 +267,12 @@ define([
it('Should be defined', function () {
expect(obj.hasOwnProperty('invalidate')).toBeDefined();
});

it('Does not throw before component is initialized', function () {
expect(function () {
obj.invalidate();
}).not.toThrow();
});
});

describe('"Magento_Customer/js/customer-data" method', function () {
Expand Down
2 changes: 1 addition & 1 deletion lib/web/jquery/jquery.storageapi.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff99146

Please sign in to comment.