Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import { Component, Directive, forwardRef, Host, Inject, InjectionToken, Input } from '@angular/core';
  2. import { CanDisable, CanDisableCtor, mixinDisabled } from '@angular/material';
  3. import { TestBed } from '@angular/core/testing';
  4.  
  5. const CAN_DISABLE = new InjectionToken<CanDisable>('testCanDisable');
  6.  
  7. export function provideCanDisable(component: any) {
  8. return { provide: CAN_DISABLE, useExisting: forwardRef(() => component) };
  9. }
  10.  
  11. class TestComponentBase {}
  12.  
  13. const TestComponentMixinBase: CanDisableCtor & typeof TestComponentBase = mixinDisabled(TestComponentBase);
  14.  
  15. @Directive({
  16. selector: '[testEnabler]'
  17. })
  18. export class TestEnablerDirective {
  19. @Input()
  20. set testEnabler(b: boolean) {
  21. this.host.disabled = !b;
  22. }
  23.  
  24. constructor(@Inject(CAN_DISABLE) private host: CanDisable) {}
  25. }
  26.  
  27. @Component({
  28. selector: 'test-parent-component',
  29. template: `
  30. <div [testEnabler]="true" id="parent">disabled {{disabled}}</div>
  31. `
  32. })
  33. class ParentComponent {}
  34.  
  35. @Component({
  36. template: `
  37. <div id="parentparent">disabled {{disabled}}</div>
  38. <test-parent-component></test-parent-component>
  39. `,
  40. providers: [
  41. provideCanDisable(ParentParentComponent)
  42. ]
  43. })
  44. class ParentParentComponent extends TestComponentMixinBase {}
  45.  
  46. @Component({
  47. selector: 'test-component',
  48. template: `
  49. <div [testEnabler]="true" id="test">disabled {{disabled}}</div>
  50. `,
  51. providers: [
  52. provideCanDisable(TestComponent)
  53. ]
  54. })
  55. class TestComponent extends TestComponentMixinBase {}
  56.  
  57. fdescribe('mixin injection test', () => {
  58. it('should not parse the template if there are no injectable components', () => {
  59. TestBed.configureTestingModule({
  60. declarations: [
  61. TestEnablerDirective,
  62. ParentComponent
  63. ]
  64. });
  65. const fixture = TestBed.createComponent(ParentComponent);
  66. fixture.detectChanges();
  67. expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled ');
  68. });
  69.  
  70. it('should not inject the parent of the containing component', () => {
  71. TestBed.configureTestingModule({
  72. declarations: [
  73. TestEnablerDirective,
  74. ParentComponent,
  75. ParentParentComponent
  76. ]
  77. });
  78. const fixture = TestBed.createComponent(ParentParentComponent);
  79. fixture.componentInstance.disabled = true;
  80. fixture.detectChanges();
  81. // expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled true');
  82. expect(fixture.componentInstance.disabled).toEqual(true);
  83. expect(fixture.nativeElement.querySelector('#parent').textContent).toEqual('disabled true');
  84. });
  85.  
  86. it('should correctly inject the containing component with the mixin', () => {
  87. TestBed.configureTestingModule({
  88. declarations: [
  89. TestEnablerDirective,
  90. TestComponent
  91. ]
  92. });
  93. const fixture = TestBed.createComponent(TestComponent);
  94. fixture.componentInstance.disabled = true;
  95. fixture.detectChanges();
  96. expect(fixture.componentInstance.disabled).toEqual(false);
  97. expect(fixture.nativeElement.querySelector('#test').textContent).toEqual('disabled false');
  98. });
  99. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement