/**
 * @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.io/license
 */

import {absoluteFrom} from '@angular/compiler-cli/src/ngtsc/file_system';
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {runTsurgeMigration} from '../testing';
import {OutputMigration} from './output_migration';

describe('output migration', () => {
  beforeEach(() => {
    initMockFileSystem('Native');
  });

  it('should work', async () => {
    const migration = new OutputMigration();
    const fs = await runTsurgeMigration(migration, [
      {
        name: absoluteFrom('/app.component.ts'),
        isProgramRootFile: true,
        contents: `
          import {Output, Component, EventEmitter} from '@angular/core';

          @Component()
          class AppComponent {
            @Output() clicked = new EventEmitter<void>();
          }
        `,
      },
    ]);

    expect(fs.readFile(absoluteFrom('/app.component.ts'))).toContain(
      '// TODO: Actual migration logic',
    );
  });

  it('should not migrate if there is a problematic usage', async () => {
    const migration = new OutputMigration();
    const fs = await runTsurgeMigration(migration, [
      {
        name: absoluteFrom('/app.component.ts'),
        isProgramRootFile: true,
        contents: `
          import {Output, Component, EventEmitter} from '@angular/core';

          @Component()
          export class AppComponent {
            @Output() clicked = new EventEmitter<void>();
          }
        `,
      },
      {
        name: absoluteFrom('/other.component.ts'),
        isProgramRootFile: true,
        contents: `
          import {AppComponent} from './app.component';

          const cmp: AppComponent = null!;
          cmp.clicked.pipe().subscribe();
        `,
      },
    ]);

    expect(fs.readFile(absoluteFrom('/app.component.ts'))).not.toContain('TODO');
  });
});
