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