Angular 2 - PrimeNG模拟数据表选择

时间:2016-10-24 01:03:21

标签: unit-testing angular karma-jasmine

如何模拟要在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 个答案:

答案 0 :(得分:0)

只需将您的初始选择传递给<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;
}