Skip to content

add to test @for (ng syntax) #29623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 25_1
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 131 additions & 107 deletions packages/devextreme-angular/tests/src/ui/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,134 +228,158 @@ describe('DxList', () => {
expect(instance.element().querySelectorAll('.dx-item.dx-state-disabled').length).toBe(1);
});

it('should be able to accept items as an *ngFor components list', () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `
<dx-list>
<dxi-item *ngFor="let item of items">{{item}}</dxi-item>
</dx-list>
`,
},
[
{ name: '*ngFor', tpl: '<dxi-item *ngFor="let item of items">{{item}}</dxi-item>' },
{
name: '@for', tpl: `@for (item of items; track item) {
<dxi-item>{{item}}</dxi-item>
}`,
},
].forEach(({ name, tpl }) => {
it(`should be able to accept items as an ${name} components list`, () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `<dx-list>${tpl}</dx-list>`,
},
});

const fixture = TestBed.createComponent(TestContainerComponent);

fixture.detectChanges();

const testComponent = fixture.componentInstance; const
{ instance } = testComponent.innerWidget;

expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');

testComponent.items.push(2);

fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('2');
});
const fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();

const testComponent = fixture.componentInstance;
const { instance } = testComponent.innerWidget;

expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');

testComponent.items.push(2);
fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('2');
it(`should be able to clear items rendered with ${name}`, () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `<dx-list>
${tpl}
</dx-list>`,
},
});
const fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();

const testComponent = fixture.componentInstance;
const { instance } = testComponent.innerWidget;

expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');

testComponent.items.pop();
expect(testComponent.items.length).toBe(0);
fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(0);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(0);
});
});

it('should be able to replace items by ng-for', () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `
<dx-list>
<dxi-item *ngFor="let item of items" [badge]="10">{{item}}</dxi-item>
</dx-list>
`,
},
[
{ name: '*ngFor', tpl: '<dxi-item *ngFor="let item of items" [badge]="10">{{item}}</dxi-item>' },
{
name: '@for', tpl: `@for (item of items; track item) {
<dxi-item [badge]="10">{{item}}</dxi-item>
}`,
},
].forEach(({ name, tpl }) => {
it(`should be able to replace items by ${name}`, () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `<dx-list>${tpl}</dx-list>`,
},
});
const fixture = TestBed.createComponent(TestContainerComponent);
const testComponent = fixture.componentInstance;

testComponent.items = [1, 2];
fixture.detectChanges();

const { instance } = testComponent.innerWidget;

testComponent.items = [3, 4];
fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('3');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('4');
});
const fixture = TestBed.createComponent(TestContainerComponent);
const testComponent = fixture.componentInstance;

testComponent.items = [1, 2];
fixture.detectChanges();
});

const { instance } = testComponent.innerWidget;
[
{ name: '*ngFor', tpl: '<dxi-item *ngFor="let item of complexItems">{{item.text}}</dxi-item>' },
{
name: '@for', tpl: `@for (item of complexItems; track item.text) {
<dxi-item>{{item.text}}</dxi-item>
}`,
},
].forEach(({ name, tpl }) => {
it(`should respond to items changes rendered with ${name}`, () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `<dx-list>${tpl}</dx-list>`,
},
});

testComponent.items = [3, 4];
fixture.detectChanges();
const fixture = TestBed.createComponent(TestContainerComponent);

expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('3');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('4');
});
fixture.detectChanges();

it('should be able to clear items rendered with *ngFor', () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `
<dx-list>
<dxi-item *ngFor="let item of items">{{item}}</dxi-item>
</dx-list>
`,
},
});
const fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();
const testComponent = fixture.componentInstance;
const { instance } = testComponent.innerWidget;

const testComponent = fixture.componentInstance;
const { instance } = testComponent.innerWidget;
expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Item 1');

expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('1');
const optionSpy = spyOn(instance, 'option').and.callThrough();

testComponent.items.pop();
expect(testComponent.items.length).toBe(0);
fixture.detectChanges();
fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(0);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(0);
});
expect(instance.option).not.toHaveBeenCalled;

it('should respond to items changes rendered with ngFor', () => {
TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `
<dx-list>
<dxi-item *ngFor="let item of complexItems">{{item.text}}</dxi-item>
</dx-list>
`,
},
});
const fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();
testComponent.complexItems.push({ text: 'Item 2' });

const testComponent = fixture.componentInstance;
const { instance } = testComponent.innerWidget;
fixture.detectChanges();

expect(instance?.option('items')?.length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(1);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Item 1');
expect(instance.option).toHaveBeenCalled;
expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Item 1');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('Item 2');

const optionSpy = spyOn(instance, 'option').and.callThrough();
fixture.detectChanges();
expect(instance.option).not.toHaveBeenCalled;
optionSpy.calls.reset();

testComponent.complexItems.push({ text: 'Item 2' });
fixture.detectChanges();
testComponent.complexItems[0].text = 'Changed';

expect(instance.option).toHaveBeenCalled;
expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Item 1');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('Item 2');
fixture.detectChanges();

optionSpy.calls.reset();
testComponent.complexItems[0].text = 'Changed';
fixture.detectChanges();
expect(optionSpy).toHaveBeenCalledTimes(1);
expect(optionSpy.calls.allArgs().length).toBe(1);
expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Changed');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('Item 2');

expect(optionSpy).toHaveBeenCalledTimes(1);
expect(optionSpy.calls.allArgs().length).toBe(1);
expect(instance?.option('items')?.length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content').length).toBe(2);
expect(instance.element().querySelectorAll('.dx-item-content')[0].textContent).toBe('Changed');
expect(instance.element().querySelectorAll('.dx-item-content')[1].textContent).toBe('Item 2');
optionSpy.calls.reset();
optionSpy.calls.reset();
});
});

it('should be able to set option "template" for each item', () => {
Expand Down
Loading