#unit-testing #angular #karma-jasmine
#модульное тестирование #angular #карма-жасмин
Вопрос:
Как мне создать макет данных, которые будут выбраны в datatable PrimeNG?
@Component({
selector: 'p-dataTable',
template: `...`
})
class MockDataTableComponent {
@Input() value;
@Input() selection;
@Output() selectionChange = new EventEmitter();
click( rows: number) {
this.selection = rows;
return this.selection;
}
}
@Component({
selector: 'data-table',
template: `<p-dataTable #datatable></p-dataTable>`
})
class MyTableComponent {
@ViewChild('datatable') datatable;
}
Как мне вручную задать значения для фиктивного выбора в PrimeNG? Я хочу присвоить выделению значение, подобное
this.selection[0]['name'] = "John Doe";
this.selection[0]['age'] = 30;
Как мне это сделать?
Ответ №1:
Просто передайте свой первоначальный выбор в <p-dataTable>
.
@Component({
selector: 'p-dataTable',
template: `...`
})
class MockDataTableComponent {
@Input() value;
@Input() selection;
@Output() selectionChange = new EventEmitter();
click( rows: number) {
this.selection = rows;
return this.selection;
}
}
@Component({
selector: 'data-table',
template: `<p-dataTable [selection]="mockSelection" #datatable></p-dataTable>`
})
class MyTableComponent {
mockSelection = [];
constructor(){
this.mockSelection[0]={};
this.mockSelection[0]['name'] = "John Doe";
this.mockSelection[0]['age'] = 30;
}
@ViewChild('datatable') datatable;
}
Комментарии:
1. В моем случае я фактически перенес настройку выбора в MockDataTableComponent. Спасибо.