|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable max-nested-callbacks */ |
| 7 | +define([ |
| 8 | + 'mage/decorate', |
| 9 | + 'jquery' |
| 10 | +], function (decorate, $) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + describe('mage/decorate', function () { |
| 14 | + describe('"list" method', function () { |
| 15 | + var listId = 'testList'; |
| 16 | + |
| 17 | + beforeEach(function () { |
| 18 | + var list = $('<ul id="' + listId + '"><li/><li/><li/></ul>'); |
| 19 | + |
| 20 | + $('body').append(list); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(function () { |
| 24 | + $('#' + listId).remove(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('Check correct class decoration', function () { |
| 28 | + var $list = $('#' + listId); |
| 29 | + |
| 30 | + $list.decorate('list'); |
| 31 | + expect($list.find('li:first').hasClass('first')).toBe(false); |
| 32 | + expect($list.find('li:first').hasClass('odd')).toBe(true); |
| 33 | + expect($list.find('li:last').hasClass('last')).toBe(true); |
| 34 | + expect($list.find('li:odd').hasClass('even')).toBe(true); |
| 35 | + expect($list.find('li:even').hasClass('odd')).toBe(true); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('"generic" method', function () { |
| 40 | + var listId = 'testList'; |
| 41 | + |
| 42 | + beforeEach(function () { |
| 43 | + var list = $('<ul id="' + listId + '"><li/><li/><li/></ul>'); |
| 44 | + |
| 45 | + $('body').append(list); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(function () { |
| 49 | + $('#' + listId).remove(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('Check correct class decoration with default params', function () { |
| 53 | + var $list = $('#' + listId); |
| 54 | + |
| 55 | + $list.find('li').decorate('generic'); |
| 56 | + expect($list.find('li:first').hasClass('first')).toBe(true); |
| 57 | + expect($list.find('li:first').hasClass('odd')).toBe(true); |
| 58 | + expect($list.find('li:last').hasClass('last')).toBe(true); |
| 59 | + expect($list.find('li:odd').hasClass('even')).toBe(true); |
| 60 | + expect($list.find('li:even').hasClass('odd')).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('Check correct class decoration with custom params', function () { |
| 64 | + var $list = $('#' + listId); |
| 65 | + |
| 66 | + $list.find('li').decorate('generic', ['last', 'first']); |
| 67 | + expect($list.find('li:first').hasClass('first')).toBe(true); |
| 68 | + expect($list.find('li:first').hasClass('odd')).toBe(false); |
| 69 | + expect($list.find('li:last').hasClass('last')).toBe(true); |
| 70 | + expect($list.find('li:odd').hasClass('even')).toBe(false); |
| 71 | + expect($list.find('li:even').hasClass('odd')).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Check correct class decoration with empty items', function () { |
| 75 | + var $list = $('#' + listId); |
| 76 | + |
| 77 | + $list.find('span').decorate('generic', ['last', 'first']); |
| 78 | + expect($list.find('li:first').hasClass('first')).toBe(false); |
| 79 | + expect($list.find('li:first').hasClass('odd')).toBe(false); |
| 80 | + expect($list.find('li:last').hasClass('last')).toBe(false); |
| 81 | + expect($list.find('li:odd').hasClass('even')).toBe(false); |
| 82 | + expect($list.find('li:even').hasClass('odd')).toBe(false); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('"table" method', function () { |
| 87 | + var tableId = 'testTable'; |
| 88 | + |
| 89 | + beforeEach(function () { |
| 90 | + var table = $('<table id="' + tableId + '">' + |
| 91 | + '<thead><tr><th/><th/></tr></thead>' + |
| 92 | + '<tbody>' + |
| 93 | + '<tr><td/><td/></tr>' + |
| 94 | + '<tr><td/><td/></tr>' + |
| 95 | + '<tr><td/><td/></tr>' + |
| 96 | + '</tbody>>' + |
| 97 | + '<tfoot><tr><th/><th/></tr></tfoot>' + |
| 98 | + '</table>'); |
| 99 | + |
| 100 | + $('body').append(table); |
| 101 | + }); |
| 102 | + |
| 103 | + afterEach(function () { |
| 104 | + $('#' + tableId).remove(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('Check correct class decoration with default params', function () { |
| 108 | + var $table = $('#' + tableId); |
| 109 | + |
| 110 | + $table.decorate('table'); |
| 111 | + expect($table.find('tbody tr:first').hasClass('first')).toBe(true); |
| 112 | + expect($table.find('tbody tr:first').hasClass('odd')).toBe(true); |
| 113 | + expect($table.find('tbody tr:odd').hasClass('even')).toBe(true); |
| 114 | + expect($table.find('tbody tr:even').hasClass('odd')).toBe(true); |
| 115 | + expect($table.find('tbody tr:last').hasClass('last')).toBe(true); |
| 116 | + expect($table.find('thead tr:first').hasClass('first')).toBe(true); |
| 117 | + expect($table.find('thead tr:last').hasClass('last')).toBe(true); |
| 118 | + expect($table.find('tfoot tr:first').hasClass('first')).toBe(true); |
| 119 | + expect($table.find('tfoot tr:last').hasClass('last')).toBe(true); |
| 120 | + expect($table.find('tr td:last').hasClass('last')).toBe(true); |
| 121 | + expect($table.find('tr td:first').hasClass('first')).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + it('Check correct class decoration with custom params', function () { |
| 125 | + var $table = $('#' + tableId); |
| 126 | + |
| 127 | + $table.decorate('table', { |
| 128 | + 'tbody': ['first'], |
| 129 | + 'tbody tr': ['first'], |
| 130 | + 'thead tr': ['first'], |
| 131 | + 'tfoot tr': ['last'], |
| 132 | + 'tr td': ['first'] |
| 133 | + }); |
| 134 | + expect($table.find('tbody:first').hasClass('first')).toBe(true); |
| 135 | + expect($table.find('tbody tr:first').hasClass('first')).toBe(true); |
| 136 | + expect($table.find('tbody tr:first').hasClass('odd')).toBe(false); |
| 137 | + expect($table.find('tbody tr:odd').hasClass('even')).toBe(false); |
| 138 | + expect($table.find('tbody tr:even').hasClass('odd')).toBe(false); |
| 139 | + expect($table.find('tbody tr:last').hasClass('last')).toBe(false); |
| 140 | + expect($table.find('thead tr:first').hasClass('first')).toBe(true); |
| 141 | + expect($table.find('thead tr:last').hasClass('last')).toBe(false); |
| 142 | + expect($table.find('tfoot tr:first').hasClass('first')).toBe(false); |
| 143 | + expect($table.find('tfoot tr:last').hasClass('last')).toBe(true); |
| 144 | + expect($table.find('tr td:last').hasClass('last')).toBe(false); |
| 145 | + expect($table.find('tr td:first').hasClass('first')).toBe(true); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('"dataList" method', function () { |
| 150 | + var listId = 'testDataList'; |
| 151 | + |
| 152 | + beforeEach(function () { |
| 153 | + var list = $('<dl id="' + listId + '"><dt/><dd/><dt/><dd/><dt/><dd/></dl>'); |
| 154 | + |
| 155 | + $('body').append(list); |
| 156 | + }); |
| 157 | + |
| 158 | + afterEach(function () { |
| 159 | + $('#' + listId).remove(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('Check correct class decoration', function () { |
| 163 | + var $list = $('#' + listId); |
| 164 | + |
| 165 | + $list.decorate('dataList'); |
| 166 | + expect($list.find('dt:first').hasClass('first')).toBe(false); |
| 167 | + expect($list.find('dt:first').hasClass('odd')).toBe(true); |
| 168 | + expect($list.find('dt:odd').hasClass('even')).toBe(true); |
| 169 | + expect($list.find('dt:even').hasClass('odd')).toBe(true); |
| 170 | + expect($list.find('dt:last').hasClass('last')).toBe(true); |
| 171 | + expect($list.find('dd:first').hasClass('first')).toBe(false); |
| 172 | + expect($list.find('dd:first').hasClass('odd')).toBe(true); |
| 173 | + expect($list.find('dd:odd').hasClass('even')).toBe(true); |
| 174 | + expect($list.find('dd:even').hasClass('odd')).toBe(true); |
| 175 | + expect($list.find('dd:last').hasClass('last')).toBe(true); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('Call decorate with fake method', function () { |
| 180 | + var listId = 'testDataList'; |
| 181 | + |
| 182 | + beforeEach(function () { |
| 183 | + var list = $('<dl id="' + listId + '"><dt/><dd/><dt/><dd/><dt/><dd/></dl>'); |
| 184 | + |
| 185 | + $('body').append(list); |
| 186 | + }); |
| 187 | + |
| 188 | + afterEach(function () { |
| 189 | + $('#' + listId).remove(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('Check error message', function () { |
| 193 | + var $list = $('#' + listId); |
| 194 | + |
| 195 | + spyOn(jQuery, 'error'); |
| 196 | + $list.decorate('customMethod'); |
| 197 | + |
| 198 | + expect(jQuery.error).toHaveBeenCalledWith('Method customMethod does not exist on jQuery.decorate'); |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments