Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, Directive, forwardRef, Host, Inject, InjectionToken, Input } from '@angular/core';
- import { CanDisable, CanDisableCtor, mixinDisabled } from '@angular/material';
- import { TestBed } from '@angular/core/testing';
- const CAN_DISABLE = new InjectionToken<CanDisable>('testCanDisable');
- export function provideCanDisable(component: any) {
- return { provide: CAN_DISABLE, useExisting: forwardRef(() => component) };
- }
- class TestComponentBase {}
- const TestComponentMixinBase: CanDisableCtor & typeof TestComponentBase = mixinDisabled(TestComponentBase);
- @Directive({
- selector: '[testEnabler]'
- })
- export class TestEnablerDirective {
- @Input()
- set testEnabler(b: boolean) {
- this.host.disabled = !b;
- }
- constructor(@Inject(CAN_DISABLE) private host: CanDisable) {}
- }
- @Component({
- selector: 'test-parent-component',
- template: `
- <div [testEnabler]="true" id="parent">disabled {{disabled}}</div>
- `
- })
- class ParentComponent {}
- @Component({
- template: `
- <div id="parentparent">disabled {{disabled}}</div>
- <test-parent-component></test-parent-component>
- `,
- providers: [
- provideCanDisable(ParentParentComponent)
- ]
- })
- class ParentParentComponent extends TestComponentMixinBase {}
- @Component({
- selector: 'test-component',
- template: `
- <div [testEnabler]="true" id="test">disabled {{disabled}}</div>
- `,
- providers: [
- provideCanDisable(TestComponent)
- ]
- })
- class TestComponent extends TestComponentMixinBase {}
- fdescribe('mixin injection test', () => {
- it('should not parse the template if there are no injectable components', () => {
- TestBed.configureTestingModule({
- declarations: [
- TestEnablerDirective,
- ParentComponent
- ]
- });
- const fixture = TestBed.createComponent(ParentComponent);
- fixture.detectChanges();
- expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled ');
- });
- it('should not inject the parent of the containing component', () => {
- TestBed.configureTestingModule({
- declarations: [
- TestEnablerDirective,
- ParentComponent,
- ParentParentComponent
- ]
- });
- const fixture = TestBed.createComponent(ParentParentComponent);
- fixture.componentInstance.disabled = true;
- fixture.detectChanges();
- // expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled true');
- expect(fixture.componentInstance.disabled).toEqual(true);
- expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled true');
- });
- it('should correctly inject the containing component with the mixin', () => {
- TestBed.configureTestingModule({
- declarations: [
- TestEnablerDirective,
- TestComponent
- ]
- });
- const fixture = TestBed.createComponent(TestComponent);
- fixture.componentInstance.disabled = true;
- fixture.detectChanges();
- expect(fixture.componentInstance.disabled).toEqual(false);
- expect(fixture.nativeElement.querySelector('#test').textContent).toEqual('disabled false');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement