Skip to content

Commit

Permalink
test(module:auto-complete): added missing tests (NG-ZORRO#8780)
Browse files Browse the repository at this point in the history
  • Loading branch information
ParsaArvanehPA authored Sep 23, 2024
1 parent dd42ad9 commit f9f7b9e
Showing 1 changed file with 138 additions and 1 deletion.
139 changes: 138 additions & 1 deletion components/auto-complete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NgZone,
OnInit,
QueryList,
SimpleChanges,
ViewChild,
ViewChildren
} from '@angular/core';
Expand All @@ -36,14 +37,16 @@ import {
MockNgZone,
typeInElement
} from 'ng-zorro-antd/core/testing';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzInputModule } from 'ng-zorro-antd/input';

import { getNzAutocompleteMissingPanelError } from './autocomplete-trigger.directive';
import {
NzAutocompleteComponent,
NzAutocompleteModule,
NzAutocompleteOptionComponent,
NzAutocompleteTriggerDirective
NzAutocompleteTriggerDirective,
NzOptionSelectionChange
} from './index';

describe('auto-complete', () => {
Expand Down Expand Up @@ -874,6 +877,25 @@ describe('auto-complete', () => {
tick(500);
expect(overlayContainerElement.querySelector('.ant-select-dropdown')).toBeFalsy();
}));

it('should call closePanel on correct circumstances', () => {
const trigger = fixture.componentInstance.trigger;

trigger.panelOpen = true;
trigger.nzAutocomplete.showPanel = true;
const event = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
which: 13,
keyCode: 13
});
spyOnProperty(trigger, 'activeOption', 'get').and.returnValue(null);
spyOn(trigger, 'closePanel');

trigger.handleKeydown(event);

expect(trigger.closePanel).toHaveBeenCalled();
});
});

// TODO: Implement this test case
Expand Down Expand Up @@ -1196,3 +1218,118 @@ class NzTestAutocompleteWithGroupInputComponent {
@ViewChild(NzAutocompleteTriggerDirective, { static: false }) trigger!: NzAutocompleteTriggerDirective;
@ViewChild('inputGroupComponent', { static: false, read: ElementRef }) inputGroupComponent!: ElementRef;
}

class MockDirectionality {
value = 'ltr';
change = new Subject();
}

describe('auto-complete', () => {
let component: NzAutocompleteComponent;
let fixture: ComponentFixture<NzAutocompleteComponent>;
let mockDirectionality: MockDirectionality;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NzAutocompleteModule],
providers: [{ provide: Directionality, useClass: MockDirectionality }]
}).compileComponents();

fixture = TestBed.createComponent(NzAutocompleteComponent);
component = fixture.componentInstance;
mockDirectionality = TestBed.inject(Directionality) as unknown as MockDirectionality;
});

it('should change dir', fakeAsync(() => {
spyOn(component['changeDetectorRef'], 'detectChanges');
mockDirectionality.value = 'ltr';
component.ngOnInit();
expect(component.dir).toEqual('ltr');
mockDirectionality.change.next('rtl');
tick();
expect(component.dir).toEqual('rtl');
expect(component['changeDetectorRef'].detectChanges).toHaveBeenCalled();
}));

it('should normalizeDataSource return correct value', () => {
let changes: SimpleChanges = {
nzDataSource: {
currentValue: [1, 2],
firstChange: false,
previousValue: undefined,
isFirstChange: function (): boolean {
throw new Error('Function not implemented.');
}
}
};
component.ngOnChanges(changes);
expect(component.normalizedDataSource).toEqual([
{
label: '1',
value: '1'
},
{
label: '2',
value: '2'
}
]);

changes = {
nzDataSource: {
currentValue: ['1', '2'],
firstChange: false,
previousValue: undefined,
isFirstChange: function (): boolean {
throw new Error('Function not implemented.');
}
}
};
component.ngOnChanges(changes);
expect(component.normalizedDataSource).toEqual([
{
label: '1',
value: '1'
},
{
label: '2',
value: '2'
}
]);

changes = {
nzDataSource: {
currentValue: [
{
label: '1',
value: '1'
},
{
label: '2',
value: '2'
}
],
firstChange: false,
previousValue: undefined,
isFirstChange: function (): boolean {
throw new Error('Function not implemented.');
}
}
};
component.ngOnChanges(changes);
expect(component.normalizedDataSource).toEqual([
{
label: '1',
value: '1'
},
{
label: '2',
value: '2'
}
]);
});

it('NzOptionSelectionChange should have correct initial value for isUserInput', () => {
const nzOptionSelectionChange = new NzOptionSelectionChange({} as NzSafeAny);
expect(nzOptionSelectionChange.isUserInput).toBeFalsy();
});
});

0 comments on commit f9f7b9e

Please sign in to comment.