|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +define([ |
| 6 | + 'Magento_PageBuilder/js/widget/bind-click-to-data-link-element', |
| 7 | + 'jquery' |
| 8 | +], function (bindClickToLinkInitializerWidget, $) { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + var $el; |
| 12 | + |
| 13 | + afterEach(function () { |
| 14 | + if ($el !== undefined) { |
| 15 | + $el.remove(); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + describe('Magento_PageBuilder/js/widget/bind-click-to-data-link-element', function () { |
| 20 | + it('Should not navigate away from page if it is missing href attribute', function () { |
| 21 | + spyOn(bindClickToLinkInitializerWidget, 'redirectTo'); |
| 22 | + $el = $( |
| 23 | + '<div data-role="content-type">' + |
| 24 | + '<div data-link-type="link" href="">' + |
| 25 | + '<span class="span-outside-inner-anchor">Hello world</span>' + |
| 26 | + '<a class="inner-anchor" href="https://something.com"><span>Something</span></a>' + |
| 27 | + '</div>' + |
| 28 | + '</div>' |
| 29 | + ); |
| 30 | + |
| 31 | + $el.appendTo('body'); |
| 32 | + |
| 33 | + bindClickToLinkInitializerWidget(null, $el); |
| 34 | + |
| 35 | + $el.find('.span-outside-inner-anchor').click(); |
| 36 | + |
| 37 | + expect(bindClickToLinkInitializerWidget.redirectTo).not.toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Should not navigate away from page if href is javascript:void(0)', function () { |
| 41 | + spyOn(bindClickToLinkInitializerWidget, 'redirectTo'); |
| 42 | + $el = $( |
| 43 | + '<div data-role="content-type">' + |
| 44 | + '<div data-link-type="link" href="javascript:void(0)">' + |
| 45 | + '<span class="span-outside-inner-anchor">Hello world</span>' + |
| 46 | + '<a class="inner-anchor" href="https://something.com"><span>Something</span></a>' + |
| 47 | + '</div>' + |
| 48 | + '</div>' |
| 49 | + ); |
| 50 | + |
| 51 | + $el.appendTo('body'); |
| 52 | + |
| 53 | + bindClickToLinkInitializerWidget(null, $el); |
| 54 | + |
| 55 | + $el.find('.span-outside-inner-anchor').click(); |
| 56 | + |
| 57 | + expect(bindClickToLinkInitializerWidget.redirectTo).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Should not navigate away from page if it is missing data-link-type attribute', function () { |
| 61 | + spyOn(bindClickToLinkInitializerWidget, 'redirectTo'); |
| 62 | + $el = $( |
| 63 | + '<div data-role="content-type">' + |
| 64 | + '<div href="https://adobe.com">' + |
| 65 | + '<span class="span-outside-inner-anchor">Hello world</span>' + |
| 66 | + '<a class="inner-anchor" href="https://something.com"><span>Something</span></a>' + |
| 67 | + '</div>' + |
| 68 | + '</div>' |
| 69 | + ); |
| 70 | + |
| 71 | + $el.appendTo('body'); |
| 72 | + |
| 73 | + bindClickToLinkInitializerWidget(null, $el); |
| 74 | + |
| 75 | + $el.find('.span-outside-inner-anchor').click(); |
| 76 | + |
| 77 | + expect(bindClickToLinkInitializerWidget.redirectTo).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Should not navigate to an simulated anchor\'s href if clicked inside of nested anchor', function () { |
| 81 | + spyOn(bindClickToLinkInitializerWidget, 'redirectTo'); |
| 82 | + |
| 83 | + $el = $( |
| 84 | + '<div data-role="content-type">' + |
| 85 | + '<div data-link-type="link" href="https://adobe.com">' + |
| 86 | + '<span class="span-outside-inner-anchor">Hello world</span>' + |
| 87 | + '<a class="inner-anchor" href="https://something.com"><span>Something</span></a>' + |
| 88 | + '</div>' + |
| 89 | + '</div>' |
| 90 | + ); |
| 91 | + |
| 92 | + $el.appendTo('body'); |
| 93 | + |
| 94 | + bindClickToLinkInitializerWidget(null, $el); |
| 95 | + |
| 96 | + $el.find('.inner-anchor').click(); |
| 97 | + |
| 98 | + expect(bindClickToLinkInitializerWidget.redirectTo).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Should navigate to an simulated anchor\'s href if clicked outside of nested anchor', function () { |
| 102 | + spyOn(bindClickToLinkInitializerWidget, 'redirectTo'); |
| 103 | + |
| 104 | + $el = $( |
| 105 | + '<div data-role="content-type">' + |
| 106 | + '<div data-link-type="link" href="https://adobe.com">' + |
| 107 | + '<span class="span-outside-inner-anchor">Hello world</span>' + |
| 108 | + '<a class="inner-anchor" href="https://something.com"><span>Something</span></a>' + |
| 109 | + '</div>' + |
| 110 | + '</div>' |
| 111 | + ); |
| 112 | + |
| 113 | + $el.appendTo('body'); |
| 114 | + |
| 115 | + bindClickToLinkInitializerWidget(null, $el); |
| 116 | + |
| 117 | + $el.find('.span-outside-inner-anchor').click(); |
| 118 | + |
| 119 | + expect(bindClickToLinkInitializerWidget.redirectTo).toHaveBeenCalledWith('https://adobe.com'); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments