/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.dev/license
 */

import {Component, Directive, ElementRef} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {hasClass} from '@angular/private/testing';
import {expect} from '@angular/private/testing/matchers';

describe('non-bindable', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TestComponent, TestDirective],
    });
  });

  it('should not interpolate children', () => {
    const template = '<div>{{text}}<span ngNonBindable>{{text}}</span></div>';
    const fixture = createTestComponent(template);

    fixture.detectChanges();
    expect(fixture.nativeElement).toHaveText('foo{{text}}');
  });

  it('should ignore directives on child nodes', () => {
    const template = '<div ngNonBindable><span id=child test-dec>{{text}}</span></div>';
    const fixture = createTestComponent(template);
    fixture.detectChanges();

    // We must use getDOM().querySelector instead of fixture.query here
    // since the elements inside are not compiled.
    const span = fixture.nativeElement.querySelector('#child');
    expect(hasClass(span, 'compiled')).toBeFalsy();
  });

  it('should trigger directives on the same node', () => {
    const template = '<div><span id=child ngNonBindable test-dec>{{text}}</span></div>';
    const fixture = createTestComponent(template);
    fixture.detectChanges();
    const span = fixture.nativeElement.querySelector('#child');
    expect(hasClass(span, 'compiled')).toBeTruthy();
  });
});

@Directive({
  selector: '[test-dec]',
  standalone: false,
})
class TestDirective {
  constructor(el: ElementRef) {
    el.nativeElement.classList.add('compiled');
  }
}

@Component({
  selector: 'test-cmp',
  template: '',
  standalone: false,
})
class TestComponent {
  text: string;
  constructor() {
    this.text = 'foo';
  }
}

function createTestComponent(template: string): ComponentFixture<TestComponent> {
  return TestBed.overrideComponent(TestComponent, {set: {template: template}}).createComponent(
    TestComponent,
  );
}
