|
| 1 | +import { I18N } from 'aurelia-i18n'; |
| 2 | +import { GridOption } from '../../models/gridOption.interface'; |
| 3 | +import { CellExternalCopyManagerExtension } from '../cellExternalCopyManagerExtension'; |
| 4 | +import { ExtensionUtility } from '../extensionUtility'; |
| 5 | +import { SharedService } from '../../services/shared.service'; |
| 6 | +import { SelectedRange } from '../../models'; |
| 7 | + |
| 8 | +declare var Slick: any; |
| 9 | + |
| 10 | +const gridStub = { |
| 11 | + getOptions: jest.fn(), |
| 12 | + registerPlugin: jest.fn(), |
| 13 | + setSelectionModel: jest.fn(), |
| 14 | +}; |
| 15 | + |
| 16 | +const addonStub = { |
| 17 | + init: jest.fn(), |
| 18 | + destroy: jest.fn(), |
| 19 | + onCopyCells: new Slick.Event(), |
| 20 | + onCopyCancelled: new Slick.Event(), |
| 21 | + onPasteCells: new Slick.Event(), |
| 22 | +}; |
| 23 | +const mockAddon = jest.fn().mockImplementation(() => addonStub); |
| 24 | +const mockSelectionModel = jest.fn().mockImplementation(() => ({ |
| 25 | + init: jest.fn(), |
| 26 | + destroy: jest.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('slickgrid/plugins/slick.cellexternalcopymanager', () => mockAddon); |
| 30 | +Slick.CellExternalCopyManager = mockAddon; |
| 31 | + |
| 32 | +jest.mock('slickgrid/plugins/slick.cellselectionmodel', () => mockSelectionModel); |
| 33 | +Slick.CellSelectionModel = mockSelectionModel; |
| 34 | + |
| 35 | +describe('cellExternalCopyManagerExtension', () => { |
| 36 | + const mockEventCallback = (e, args: { ranges: SelectedRange[] }) => { }; |
| 37 | + const mockSelectRange = [{ fromCell: 1, fromRow: 1, toCell: 1, toRow: 1 }] as SelectedRange[]; |
| 38 | + const mockSelectRangeEvent = { ranges: mockSelectRange }; |
| 39 | + |
| 40 | + let extension: CellExternalCopyManagerExtension; |
| 41 | + let extensionUtility: ExtensionUtility; |
| 42 | + let sharedService: SharedService; |
| 43 | + const gridOptionsMock = { |
| 44 | + enableCheckboxSelector: true, |
| 45 | + excelCopyBufferOptions: { |
| 46 | + onExtensionRegistered: jest.fn(), |
| 47 | + onCopyCells: mockEventCallback, |
| 48 | + onCopyCancelled: mockEventCallback, |
| 49 | + onPasteCells: mockEventCallback, |
| 50 | + } |
| 51 | + } as GridOption; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + extensionUtility = new ExtensionUtility({} as I18N, sharedService); |
| 55 | + sharedService = new SharedService(); |
| 56 | + extension = new CellExternalCopyManagerExtension(extensionUtility, sharedService); |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + jest.clearAllMocks(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return null when either the grid object or the grid options is missing', () => { |
| 64 | + const output = extension.register(); |
| 65 | + expect(output).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('registered addon', () => { |
| 69 | + beforeEach(() => { |
| 70 | + jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub); |
| 71 | + jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should register the addon', () => { |
| 75 | + const pluginSpy = jest.spyOn(SharedService.prototype.grid, 'registerPlugin'); |
| 76 | + const optionSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onExtensionRegistered'); |
| 77 | + |
| 78 | + const instance = extension.register(); |
| 79 | + |
| 80 | + expect(optionSpy).toHaveBeenCalledWith(instance); |
| 81 | + expect(pluginSpy).toHaveBeenCalledWith(instance); |
| 82 | + expect(mockSelectionModel).toHaveBeenCalled(); |
| 83 | + expect(mockAddon).toHaveBeenCalledWith({ |
| 84 | + clipboardCommandHandler: expect.anything(), |
| 85 | + dataItemColumnValueExtractor: expect.anything(), |
| 86 | + newRowCreator: expect.anything(), |
| 87 | + includeHeaderWhenCopying: false, |
| 88 | + readOnlyMode: false, |
| 89 | + onCopyCancelled: expect.anything(), |
| 90 | + onCopyCells: expect.anything(), |
| 91 | + onExtensionRegistered: expect.anything(), |
| 92 | + onPasteCells: expect.anything(), |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call internal event handler subscribe and expect the "onCopyCells" option to be called when addon notify is called', () => { |
| 97 | + const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe'); |
| 98 | + const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells'); |
| 99 | + const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled'); |
| 100 | + const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells'); |
| 101 | + |
| 102 | + const instance = extension.register(); |
| 103 | + instance.onCopyCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub); |
| 104 | + |
| 105 | + expect(handlerSpy).toHaveBeenCalledTimes(3); |
| 106 | + expect(handlerSpy).toHaveBeenCalledWith( |
| 107 | + { notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), }, |
| 108 | + expect.anything() |
| 109 | + ); |
| 110 | + expect(onCopySpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent); |
| 111 | + expect(onCancelSpy).not.toHaveBeenCalled(); |
| 112 | + expect(onPasteSpy).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should call internal event handler subscribe and expect the "onCopyCancelled" option to be called when addon notify is called', () => { |
| 116 | + const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe'); |
| 117 | + const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells'); |
| 118 | + const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled'); |
| 119 | + const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells'); |
| 120 | + |
| 121 | + const instance = extension.register(); |
| 122 | + instance.onCopyCancelled.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub); |
| 123 | + |
| 124 | + expect(handlerSpy).toHaveBeenCalledTimes(3); |
| 125 | + expect(handlerSpy).toHaveBeenCalledWith( |
| 126 | + { notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), }, |
| 127 | + expect.anything() |
| 128 | + ); |
| 129 | + expect(onCopySpy).not.toHaveBeenCalled(); |
| 130 | + expect(onCancelSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent); |
| 131 | + expect(onPasteSpy).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should call internal event handler subscribe and expect the "onPasteCells" option to be called when addon notify is called', () => { |
| 135 | + const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe'); |
| 136 | + const onCopySpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCells'); |
| 137 | + const onCancelSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onCopyCancelled'); |
| 138 | + const onPasteSpy = jest.spyOn(SharedService.prototype.gridOptions.excelCopyBufferOptions, 'onPasteCells'); |
| 139 | + |
| 140 | + const instance = extension.register(); |
| 141 | + instance.onPasteCells.notify(mockSelectRangeEvent, new Slick.EventData(), gridStub); |
| 142 | + |
| 143 | + expect(handlerSpy).toHaveBeenCalledTimes(3); |
| 144 | + expect(handlerSpy).toHaveBeenCalledWith( |
| 145 | + { notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), }, |
| 146 | + expect.anything() |
| 147 | + ); |
| 148 | + expect(onCopySpy).not.toHaveBeenCalled(); |
| 149 | + expect(onCancelSpy).not.toHaveBeenCalled(); |
| 150 | + expect(onPasteSpy).toHaveBeenCalledWith(expect.anything(), mockSelectRangeEvent); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should dispose of the addon', () => { |
| 154 | + const instance = extension.register(); |
| 155 | + const destroySpy = jest.spyOn(instance, 'destroy'); |
| 156 | + |
| 157 | + extension.dispose(); |
| 158 | + |
| 159 | + expect(destroySpy).toHaveBeenCalled(); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments