clr-angular-static-fix
Version:
1. Install Clarity Icons package through npm:
111 lines (97 loc) • 4 kB
text/typescript
/*
* Copyright (c) 2016-2018 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { Component, Directive, NgModule, Type, ViewContainerRef } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DynamicWrapper } from '../../utils/host-wrapping/dynamic-wrapper';
import { ClrHostWrappingModule } from '../../utils/host-wrapping/host-wrapping.module';
import { ClrCommonFormsModule } from './common.module';
import { ControlIdService } from './providers/control-id.service';
import { WrappedFormControl } from './wrapped-control';
/*
* Components representing generic form controls.
*/
class GenericWrapper implements DynamicWrapper {
_dynamic = false;
}
class GenericControl extends WrappedFormControl<GenericWrapper> {
constructor(vcr: ViewContainerRef) {
super(GenericWrapper, vcr);
}
}
class CommonFormsTestModule {}
/*
* Actual test components, one for each case we support
*/
class NoWrapperNoId {}
class NoWrapperWithId {}
class WithWrapperNoId {}
class WithWrapperWithId {}
export default function(): void {
describe('Common forms integration', () => {
function assertMatching<T>(testComponent: Type<T>, projectedLabels: boolean, expectedId?: string) {
return function() {
TestBed.configureTestingModule({ imports: [CommonFormsTestModule], declarations: [testComponent] });
const fixture = TestBed.createComponent(testComponent);
fixture.detectChanges();
if (!expectedId) {
const wrapperDebug = fixture.debugElement.query(By.directive(GenericWrapper));
expectedId = wrapperDebug.injector.get(ControlIdService).id;
}
const input = fixture.nativeElement.querySelector('input');
expect(input.getAttribute('id')).toBe(expectedId, 'input has the wrong id attribute');
const labels = ['#container-view-label-before', '#container-view-label-after'];
if (projectedLabels) {
labels.push('#test-view-label-before', '#test-view-label-after');
}
for (const labelSelector of labels) {
const label = fixture.nativeElement.querySelector(labelSelector);
expect(label.getAttribute('for')).toBe(expectedId, labelSelector + ' has the wrong for attribute');
}
};
}
it('works without an explicit wrapper and without an id', assertMatching(NoWrapperNoId, false));
it('works without an explicit wrapper and with an id', assertMatching(NoWrapperWithId, false, 'hello'));
it('works with an explicit wrapper and without an id', assertMatching(WithWrapperNoId, true));
it('works with an explicit wrapper and with an id', assertMatching(WithWrapperWithId, true, 'hello'));
});
}