|
| 1 | +/************************************************************************ |
| 2 | + * Copyright 2024 Adobe |
| 3 | + * All Rights Reserved. |
| 4 | + * |
| 5 | + * NOTICE: All information contained herein is, and remains |
| 6 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 7 | + * and technical concepts contained herein are proprietary to Adobe |
| 8 | + * and its suppliers and are protected by all applicable intellectual |
| 9 | + * property laws, including trade secret and copyright laws. |
| 10 | + * Dissemination of this information or reproduction of this material |
| 11 | + * is strictly forbidden unless prior written permission is obtained |
| 12 | + * from Adobe. |
| 13 | + * *********************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +/* eslint-disable max-nested-callbacks */ |
| 17 | +define([ |
| 18 | + 'jquery', |
| 19 | + 'underscore', |
| 20 | + 'uiElement', |
| 21 | + 'Magento_Ui/js/grid/export' |
| 22 | +], function ($, _, Element, ExportComponent) { |
| 23 | + 'use strict'; |
| 24 | + |
| 25 | + describe('Magento_Ui/js/grid/export', function () { |
| 26 | + var obj; |
| 27 | + |
| 28 | + beforeEach(function () { |
| 29 | + // Mock for selections |
| 30 | + var selectionsMock = { |
| 31 | + getSelections: function () { |
| 32 | + return { |
| 33 | + excludeMode: false, |
| 34 | + params: { |
| 35 | + filters: 'filters', |
| 36 | + search: 'search', |
| 37 | + namespace: 'namespace' |
| 38 | + }, |
| 39 | + selected: [1, 2, 3] |
| 40 | + }; |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + // Initialize the instance with the mock |
| 45 | + obj = new ExportComponent({ |
| 46 | + ns: 'testNamespace', // Define ns here |
| 47 | + options: [ |
| 48 | + {value: 'csv', url: '/export/csv'}, |
| 49 | + {value: 'xml', url: '/export/xml'} |
| 50 | + ], |
| 51 | + additionalParams: {foo: 'bar'}, |
| 52 | + imports: {}, // Ensure imports is initialized |
| 53 | + selections: selectionsMock // Pass the mock |
| 54 | + }); |
| 55 | + |
| 56 | + // Manually set the selections observable to the mock |
| 57 | + obj.selections(selectionsMock); |
| 58 | + |
| 59 | + // Mocking $.ajax to avoid actual server calls |
| 60 | + spyOn($, 'ajax').and.callFake(function (options) { |
| 61 | + if (options.url === '/export/csv') { |
| 62 | + options.success('csvData'); |
| 63 | + } else if (options.url === '/export/xml') { |
| 64 | + options.success('xmlData'); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should initialize correctly', function () { |
| 70 | + expect(obj.template).toEqual('ui/grid/exportButton'); |
| 71 | + expect(obj.selectProvider).toEqual('ns = testNamespace, index = ids'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should initialize checked value', function () { |
| 75 | + obj.initChecked(); |
| 76 | + expect(obj.checked()).toEqual('csv'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should observe checked property', function () { |
| 80 | + spyOn(obj, 'observe').and.callThrough(); |
| 81 | + obj.initObservable(); |
| 82 | + expect(obj.observe).toHaveBeenCalledWith('checked'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return active option', function () { |
| 86 | + obj.checked('xml'); |
| 87 | + let option = obj.getActiveOption(); |
| 88 | + |
| 89 | + expect(option.value).toEqual('xml'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should apply the option and call postRequest', function () { |
| 93 | + spyOn(obj, 'getActiveOption').and.returnValue({ url: 'test-url' }); |
| 94 | + spyOn(obj, 'postRequest'); |
| 95 | + obj.applyOption(); |
| 96 | + expect(obj.postRequest).toHaveBeenCalledWith({ url: 'test-url' }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should post request correctly', function () { |
| 100 | + spyOn(obj, 'getParams').and.returnValue({ param1: 'value1' }); |
| 101 | + |
| 102 | + obj.postRequest({ url: 'test-url' }); |
| 103 | + |
| 104 | + expect($.ajax).toHaveBeenCalledWith(jasmine.objectContaining({ |
| 105 | + url: 'test-url', |
| 106 | + type: 'POST', |
| 107 | + data: $.param({ param1: 'value1' }), |
| 108 | + showLoader: true |
| 109 | + })); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments