|
8 | 8 | IgxComboModule, |
9 | 9 | IComboSelectionChangeEventArgs, |
10 | 10 | IgxComboState, |
11 | | - IComboSearchInputEventArgs |
| 11 | + IComboSearchInputEventArgs, |
| 12 | + IComboItemAdditionEvent |
12 | 13 | } from './combo.component'; |
13 | 14 | import { IgxComboItemComponent } from './combo-item.component'; |
14 | 15 | import { IgxComboDropDownComponent } from './combo-dropdown.component'; |
@@ -685,6 +686,106 @@ describe('igxCombo', () => { |
685 | 686 | combo.handleClearItems(spyObj); |
686 | 687 | expect(combo.value).toEqual(item[0]); |
687 | 688 | }); |
| 689 | + |
| 690 | + it('should allow canceling and overwriting of item addition', fakeAsync(() => { |
| 691 | + const selectionService = new IgxSelectionAPIService(); |
| 692 | + combo = new IgxComboComponent(elementRef, mockCdr, selectionService, mockComboService, |
| 693 | + mockIconService, null, null, mockInjector); |
| 694 | + const dropdown = jasmine.createSpyObj('IgxComboDropDownComponent', ['selectItem']); |
| 695 | + const mockVirtDir = jasmine.createSpyObj('virtDir', ['scrollTo']); |
| 696 | + const mockInput = jasmine.createSpyObj('mockInput', [], { |
| 697 | + nativeElement: jasmine.createSpyObj('mockElement', ['focus']) |
| 698 | + }); |
| 699 | + spyOn(combo.onAddition, 'emit').and.callThrough(); |
| 700 | + spyOn(mockIconService, 'addSvgIconFromText').and.returnValue(null); |
| 701 | + const subParams: { cancel: boolean; newValue: string; modify: boolean } = { |
| 702 | + cancel: false, |
| 703 | + modify: false, |
| 704 | + newValue: 'mockValue' |
| 705 | + }; |
| 706 | + const sub = combo.onAddition.subscribe((e) => { |
| 707 | + if (subParams.cancel) { |
| 708 | + e.cancel = true; |
| 709 | + } |
| 710 | + if (subParams.modify) { |
| 711 | + e.addedItem = subParams.newValue; |
| 712 | + } |
| 713 | + }); |
| 714 | + |
| 715 | + combo.ngOnInit(); |
| 716 | + combo.data = ['Item 1', 'Item 2', 'Item 3']; |
| 717 | + combo.dropdown = dropdown; |
| 718 | + combo.searchInput = mockInput; |
| 719 | + (combo as any).virtDir = mockVirtDir; |
| 720 | + let mockAddParams: IComboItemAdditionEvent = { |
| 721 | + cancel: false, |
| 722 | + owner: combo, |
| 723 | + addedItem: 'Item 99', |
| 724 | + newCollection: ['Item 1', 'Item 2', 'Item 3', 'Item 99'], |
| 725 | + oldCollection: ['Item 1', 'Item 2', 'Item 3'] |
| 726 | + }; |
| 727 | + |
| 728 | + |
| 729 | + // handle addition |
| 730 | + |
| 731 | + combo.searchValue = 'Item 99'; |
| 732 | + combo.addItemToCollection(); |
| 733 | + tick(); |
| 734 | + expect(combo.data.length).toEqual(4); |
| 735 | + expect(combo.onAddition.emit).toHaveBeenCalledWith(mockAddParams); |
| 736 | + expect(combo.onAddition.emit).toHaveBeenCalledTimes(1); |
| 737 | + expect(mockVirtDir.scrollTo).toHaveBeenCalledTimes(1); |
| 738 | + expect(combo.searchInput.nativeElement.focus).toHaveBeenCalledTimes(1); |
| 739 | + expect(combo.data[combo.data.length - 1]).toBe('Item 99'); |
| 740 | + expect(selectionService.get(combo.id).size).toBe(1); |
| 741 | + expect([...selectionService.get(combo.id)][0]).toBe('Item 99'); |
| 742 | + |
| 743 | + // cancel |
| 744 | + subParams.cancel = true; |
| 745 | + mockAddParams = { |
| 746 | + cancel: true, |
| 747 | + owner: combo, |
| 748 | + addedItem: 'Item 99', |
| 749 | + newCollection: ['Item 1', 'Item 2', 'Item 3', 'Item 99', 'Item 99'], |
| 750 | + oldCollection: ['Item 1', 'Item 2', 'Item 3', 'Item 99'] |
| 751 | + }; |
| 752 | + |
| 753 | + combo.searchValue = 'Item 99'; |
| 754 | + combo.addItemToCollection(); |
| 755 | + tick(); |
| 756 | + expect(combo.onAddition.emit).toHaveBeenCalledWith(mockAddParams); |
| 757 | + expect(combo.onAddition.emit).toHaveBeenCalledTimes(2); |
| 758 | + expect(mockVirtDir.scrollTo).toHaveBeenCalledTimes(1); |
| 759 | + expect(combo.searchInput.nativeElement.focus).toHaveBeenCalledTimes(1); |
| 760 | + expect(combo.data.length).toEqual(4); |
| 761 | + expect(combo.data[combo.data.length - 1]).toBe('Item 99'); |
| 762 | + expect(selectionService.get(combo.id).size).toBe(1); |
| 763 | + expect([...selectionService.get(combo.id)][0]).toBe('Item 99'); |
| 764 | + |
| 765 | + // overwrite |
| 766 | + subParams.modify = true; |
| 767 | + subParams.cancel = false; |
| 768 | + mockAddParams = { |
| 769 | + cancel: false, |
| 770 | + owner: combo, |
| 771 | + addedItem: 'mockValue', |
| 772 | + newCollection: ['Item 1', 'Item 2', 'Item 3', 'Item 99', 'Item 99'], |
| 773 | + oldCollection: ['Item 1', 'Item 2', 'Item 3', 'Item 99'] |
| 774 | + }; |
| 775 | + |
| 776 | + combo.searchValue = 'Item 99'; |
| 777 | + combo.addItemToCollection(); |
| 778 | + tick(); |
| 779 | + expect(combo.onAddition.emit).toHaveBeenCalledWith(mockAddParams); |
| 780 | + expect(combo.onAddition.emit).toHaveBeenCalledTimes(3); |
| 781 | + expect(mockVirtDir.scrollTo).toHaveBeenCalledTimes(2); |
| 782 | + expect(combo.searchInput.nativeElement.focus).toHaveBeenCalledTimes(2); |
| 783 | + expect(combo.data.length).toEqual(5); |
| 784 | + expect(combo.data[combo.data.length - 1]).toBe(subParams.newValue); |
| 785 | + expect(selectionService.get(combo.id).size).toBe(2); |
| 786 | + expect([...selectionService.get(combo.id)][1]).toBe(subParams.newValue); |
| 787 | + sub.unsubscribe(); |
| 788 | + })); |
688 | 789 | }); |
689 | 790 | describe('Initialization and rendering tests: ', () => { |
690 | 791 | configureTestSuite(); |
|
0 commit comments