|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +define([ |
| 7 | + 'squire', |
| 8 | + 'jquery' |
| 9 | +], function (Squire, $) { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + var blockLoaderTmpl = '<div data-role="loader" class="loading-mask" style="position: absolute;">\n' + |
| 13 | + ' <div class="loader">\n' + |
| 14 | + ' <img src="<%= loaderImageHref %>" alt="Loading..." title="Loading..." style="position: absolute;">\n' + |
| 15 | + ' </div>\n' + |
| 16 | + '</div>', |
| 17 | + injector = new Squire(), |
| 18 | + mocks = { |
| 19 | + 'Magento_Ui/js/lib/knockout/template/loader': { |
| 20 | + /** Method stub. */ |
| 21 | + loadTemplate: function () { |
| 22 | + var defer = $.Deferred(); |
| 23 | + |
| 24 | + defer.resolve(blockLoaderTmpl); |
| 25 | + |
| 26 | + return defer; |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + obj, |
| 31 | + ko; |
| 32 | + |
| 33 | + beforeEach(function (done) { |
| 34 | + injector.mock(mocks); |
| 35 | + injector.require(['Magento_Ui/js/block-loader', 'ko'], function (blockLoader, knockout) { |
| 36 | + obj = blockLoader; |
| 37 | + ko = knockout; |
| 38 | + done(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(function () { |
| 43 | + try { |
| 44 | + injector.clean(); |
| 45 | + injector.remove(); |
| 46 | + } catch (e) { |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + describe('Magento_Ui/js/block-loader', function () { |
| 51 | + var blockContentLoadingClass = '_block-content-loading', |
| 52 | + loaderImageUrl = 'https://static.magento.com/loader.gif', |
| 53 | + element = $('<span data-bind="blockLoader: isLoading"/>'), |
| 54 | + isLoading, |
| 55 | + context; |
| 56 | + |
| 57 | + beforeEach(function () { |
| 58 | + isLoading = ko.observable(); |
| 59 | + context = ko.bindingContext.prototype.createChildContext({ |
| 60 | + isLoading: isLoading |
| 61 | + }); |
| 62 | + obj(loaderImageUrl); |
| 63 | + $('body').append(element); |
| 64 | + ko.applyBindings(context, element[0]); |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(function () { |
| 68 | + ko.cleanNode(element[0]); |
| 69 | + element.remove(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('Check adding loader block to element', function () { |
| 73 | + isLoading(true); |
| 74 | + expect(element.hasClass(blockContentLoadingClass)).toBeTruthy(); |
| 75 | + expect(element.children().attr('class')).toEqual('loading-mask'); |
| 76 | + expect(element.find('img').attr('src')).toEqual(loaderImageUrl); |
| 77 | + }); |
| 78 | + |
| 79 | + it('Check removing loader block from element', function () { |
| 80 | + isLoading(false); |
| 81 | + expect(element.hasClass(blockContentLoadingClass)).toBeFalsy(); |
| 82 | + expect(element.children().length).toEqual(0); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments