|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | + |
| 4 | +import {getTableTextColumnMissingParentTableError} from './table-errors'; |
| 5 | +import {CdkTableModule} from './table-module'; |
| 6 | +import {expectTableToMatchContent} from './table.spec'; |
| 7 | +import {TEXT_COLUMN_OPTIONS, TextColumnOptions} from './text-column'; |
| 8 | + |
| 9 | + |
| 10 | +describe('CdkTextColumn', () => { |
| 11 | + let fixture: ComponentFixture<BasicTextColumnApp>; |
| 12 | + let component: BasicTextColumnApp; |
| 13 | + let tableElement: HTMLElement; |
| 14 | + |
| 15 | + beforeEach(async(() => { |
| 16 | + TestBed |
| 17 | + .configureTestingModule({ |
| 18 | + imports: [CdkTableModule], |
| 19 | + declarations: [ |
| 20 | + BasicTextColumnApp, |
| 21 | + MissingTableApp, |
| 22 | + ], |
| 23 | + }) |
| 24 | + .compileComponents(); |
| 25 | + })); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fixture = TestBed.createComponent(BasicTextColumnApp); |
| 29 | + component = fixture.componentInstance; |
| 30 | + fixture.detectChanges(); |
| 31 | + |
| 32 | + tableElement = fixture.nativeElement.querySelector('.cdk-table'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should render the basic columns', () => { |
| 36 | + expectTableToMatchContent(tableElement, [ |
| 37 | + ['PropertyA', 'PropertyB', 'PropertyC'], |
| 38 | + ['a_1', 'b_1', 'c_1'], |
| 39 | + ['a_2', 'b_2', 'c_2'], |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should throw an error if the text column is not in the content of a table', () => { |
| 44 | + expect(() => TestBed.createComponent(MissingTableApp).detectChanges()) |
| 45 | + .toThrowError(getTableTextColumnMissingParentTableError().message); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should allow for alternate header text', () => { |
| 49 | + component.headerTextB = 'column-b'; |
| 50 | + fixture.detectChanges(); |
| 51 | + |
| 52 | + expectTableToMatchContent(tableElement, [ |
| 53 | + ['PropertyA', 'column-b', 'PropertyC'], |
| 54 | + ['a_1', 'b_1', 'c_1'], |
| 55 | + ['a_2', 'b_2', 'c_2'], |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should allow for custom data accessor', () => { |
| 60 | + component.dataAccessorA = (data: TestData) => data.propertyA + '!'; |
| 61 | + fixture.detectChanges(); |
| 62 | + |
| 63 | + expectTableToMatchContent(tableElement, [ |
| 64 | + ['PropertyA', 'PropertyB', 'PropertyC'], |
| 65 | + ['a_1!', 'b_1', 'c_1'], |
| 66 | + ['a_2!', 'b_2', 'c_2'], |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should allow for custom data accessor', () => { |
| 71 | + component.dataAccessorA = (data: TestData) => data.propertyA + '!'; |
| 72 | + fixture.detectChanges(); |
| 73 | + |
| 74 | + expectTableToMatchContent(tableElement, [ |
| 75 | + ['PropertyA', 'PropertyB', 'PropertyC'], |
| 76 | + ['a_1!', 'b_1', 'c_1'], |
| 77 | + ['a_2!', 'b_2', 'c_2'], |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should update values when data changes', () => { |
| 82 | + component.data = [ |
| 83 | + {propertyA: 'changed-a_1', propertyB: 'b_1', propertyC: 'c_1'}, |
| 84 | + {propertyA: 'changed-a_2', propertyB: 'b_2', propertyC: 'c_2'}, |
| 85 | + ]; |
| 86 | + fixture.detectChanges(); |
| 87 | + |
| 88 | + expectTableToMatchContent(tableElement, [ |
| 89 | + ['PropertyA', 'PropertyB', 'PropertyC'], |
| 90 | + ['changed-a_1', 'b_1', 'c_1'], |
| 91 | + ['changed-a_2', 'b_2', 'c_2'], |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('with options', () => { |
| 96 | + function createTestComponent(options: TextColumnOptions<any>) { |
| 97 | + // Reset the previously configured testing module to be able set new providers. |
| 98 | + // The testing module has been initialized in the root describe group for the ripples. |
| 99 | + TestBed.resetTestingModule(); |
| 100 | + TestBed.configureTestingModule({ |
| 101 | + imports: [CdkTableModule], |
| 102 | + declarations: [BasicTextColumnApp], |
| 103 | + providers: [{provide: TEXT_COLUMN_OPTIONS, useValue: options}] |
| 104 | + }); |
| 105 | + |
| 106 | + fixture = TestBed.createComponent(BasicTextColumnApp); |
| 107 | + fixture.detectChanges(); |
| 108 | + |
| 109 | + tableElement = fixture.nativeElement.querySelector('.cdk-table'); |
| 110 | + } |
| 111 | + |
| 112 | + it('should be able to provide a header text transformation', () => { |
| 113 | + const defaultHeaderTextTransform = (name: string) => `${name}!`; |
| 114 | + createTestComponent({defaultHeaderTextTransform}); |
| 115 | + |
| 116 | + expectTableToMatchContent(tableElement, [ |
| 117 | + ['propertyA!', 'propertyB!', 'propertyC!'], |
| 118 | + ['a_1', 'b_1', 'c_1'], |
| 119 | + ['a_2', 'b_2', 'c_2'], |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should be able to provide a general data accessor', () => { |
| 124 | + const defaultDataAccessor = (data: TestData, name: string) => { |
| 125 | + switch (name) { |
| 126 | + case 'propertyA': |
| 127 | + return `A: ${data.propertyA}`; |
| 128 | + case 'propertyB': |
| 129 | + return `B: ${data.propertyB}`; |
| 130 | + case 'propertyC': |
| 131 | + return `C: ${data.propertyC}`; |
| 132 | + default: |
| 133 | + return ''; |
| 134 | + } |
| 135 | + }; |
| 136 | + createTestComponent({defaultDataAccessor}); |
| 137 | + |
| 138 | + expectTableToMatchContent(tableElement, [ |
| 139 | + ['PropertyA', 'PropertyB', 'PropertyC'], |
| 140 | + ['A: a_1', 'B: b_1', 'C: c_1'], |
| 141 | + ['A: a_2', 'B: b_2', 'C: c_2'], |
| 142 | + ]); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +interface TestData { |
| 148 | + propertyA: string; |
| 149 | + propertyB: string; |
| 150 | + propertyC: string; |
| 151 | +} |
| 152 | + |
| 153 | +@Component({ |
| 154 | + template: ` |
| 155 | + <cdk-table [dataSource]="data"> |
| 156 | + <cdk-text-column name="propertyA" [dataAccessor]="dataAccessorA"></cdk-text-column> |
| 157 | + <cdk-text-column name="propertyB" [headerText]="headerTextB"></cdk-text-column> |
| 158 | + <cdk-text-column name="propertyC" [justify]="justifyC"></cdk-text-column> |
| 159 | +
|
| 160 | + <cdk-header-row *cdkHeaderRowDef="displayedColumns"></cdk-header-row> |
| 161 | + <cdk-row *cdkRowDef="let row; columns: displayedColumns"></cdk-row> |
| 162 | + </cdk-table> |
| 163 | + ` |
| 164 | +}) |
| 165 | +class BasicTextColumnApp { |
| 166 | + displayedColumns = ['propertyA', 'propertyB', 'propertyC']; |
| 167 | + |
| 168 | + data: TestData[] = [ |
| 169 | + {propertyA: 'a_1', propertyB: 'b_1', propertyC: 'c_1'}, |
| 170 | + {propertyA: 'a_2', propertyB: 'b_2', propertyC: 'c_2'}, |
| 171 | + ]; |
| 172 | + |
| 173 | + headerTextB: string; |
| 174 | + dataAccessorA: (data: TestData) => string; |
| 175 | + justifyC = 'start'; |
| 176 | +} |
| 177 | + |
| 178 | +@Component({ |
| 179 | + template: ` |
| 180 | + <cdk-text-column name="column-a"></cdk-text-column> |
| 181 | + ` |
| 182 | +}) |
| 183 | +class MissingTableApp { |
| 184 | +} |
0 commit comments