/**
 * @license
 * Copyright Google Inc. 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 {AttributeMarker} from '@angular/compiler/src/core';
import {MockDirectory, setup} from '@angular/compiler/test/aot/test_util';
import {compile, expectEmit} from './mock_compile';
describe('compiler compliance: bindings', () => {
  const angularFiles = setup({
    compileAngular: false,
    compileFakeCore: true,
    compileAnimations: false,
  });
  describe('text bindings', () => {
    it('should generate interpolation instruction', () => {
      const files: MockDirectory = {
        app: {
          'example.ts': `
          import {Component, NgModule} from '@angular/core';
          @Component({
            selector: 'my-component',
            template: \`
              
Hello {{ name }}
\`
          })
          export class MyComponent {
            name = 'World';
          }
          @NgModule({declarations: [MyComponent]})
          export class MyModule {}
          `
        }
      };
      const template = `
      template:function MyComponent_Template(rf, $ctx$){
        if (rf & 1) {
          $i0$.ɵɵelementStart(0, "div");
          $i0$.ɵɵtext(1);
          $i0$.ɵɵelementEnd();
        }
        if (rf & 2) {
          $r3$.ɵɵadvance(1);
          $i0$.ɵɵtextInterpolate1("Hello ", $ctx$.name, "");
        }
      }`;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect interpolated text binding');
    });
  });
  describe('property bindings', () => {
    it('should generate bind instruction', () => {
      const files: MockDirectory = {
        app: {
          'example.ts': `
          import {Component, NgModule} from '@angular/core';
          @Component({
            selector: 'my-app',
            template: '
'
            })
            class FooCmp {}
          `
        }
      };
      const result = compile(files, angularFiles);
      expect(result.source).not.toContain('i0.ɵɵproperty');
    });
    it('should not remap property names whose names do not correspond to their attribute names',
       () => {
         const files = {
           app: {
             'spec.ts': `
              import {Component, NgModule} from '@angular/core';
              @Component({
                selector: 'my-component',
                template: \`
                  
                   \`
            })
            export class MyComponent {
              myTitle = 'hello';
              buttonId = 'special-button';
            }`
        }
      };
      const result = compile(files, angularFiles);
      const template = `
          …
          template: function MyComponent_Template(rf, ctx) {
            …
            if (rf & 2) {
              $r3$.ɵɵproperty("title", ctx.myTitle)("id", ctx.buttonId)("tabindex", 1);
              $r3$.ɵɵadvance(1);
              $r3$.ɵɵproperty("id", 1)("title", "hello")("someProp", 1 + 2);
            }
          }
        `;
      expectEmit(result.source, template, 'Incorrect template');
    });
  });
  describe('attribute bindings', () => {
    it('should chain multiple attribute bindings into a single instruction', () => {
      const files = {
        app: {
          'example.ts': `
            import {Component} from '@angular/core';
            @Component({
              template: \`
                
                 
              \`
            })
            export class MyComponent {}`
        }
      };
      const result = compile(files, angularFiles);
      const template = `
          …
          template: function MyComponent_Template(rf, ctx) {
            …
            if (rf & 2) {
              $r3$.ɵɵattributeInterpolate1("aria-label", "prefix-", 1 + 3, "");
              $r3$.ɵɵproperty("id", 2);
              $r3$.ɵɵattribute("title", 1)("tabindex", 3);
            }
          }
        `;
      expectEmit(result.source, template, 'Incorrect template');
    });
    it('should not add interpolated attributes to the attribute instruction chain', () => {
      const files = {
        app: {
          'example.ts': `
            import {Component} from '@angular/core';
            @Component({
              template: \`
                
                   \`
            })
            export class MyComponent {
              myTitle = 'hello';
              buttonId = 'special-button';
            }`
        }
      };
      const result = compile(files, angularFiles);
      const template = `
          …
          template: function MyComponent_Template(rf, ctx) {
            …
            if (rf & 2) {
              $r3$.ɵɵattribute("title", ctx.myTitle)("id", ctx.buttonId)("tabindex", 1);
              $r3$.ɵɵadvance(1);
              $r3$.ɵɵattribute("id", 1)("title", "hello")("some-attr", 1 + 2);
            }
          }
        `;
      expectEmit(result.source, template, 'Incorrect template');
    });
    it('should exclude attribute bindings from the attributes array', () => {
      const files: MockDirectory = {
        app: {
          'example.ts': `
          import {Component, NgModule} from '@angular/core';
          @Component({
            selector: 'my-app',
            template: \`
        
        
        
        
        
        
        
        
        
      `);
      const template = `
      …
        if (rf & 2) {
          i0.ɵɵpropertyInterpolateV("title", ["a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i", ctx.nine, "j"]);
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate8("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate7("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate6("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate5("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate4("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate3("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate2("title", "a", ctx.one, "b", ctx.two, "c");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate1("title", "a", ctx.one, "b");
          i0.ɵɵadvance(1);
          i0.ɵɵpropertyInterpolate("title", ctx.one);
      }
      …
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of interpolated properties');
    });
    it('should generate the proper update instructions for interpolated attributes', () => {
      const files: MockDirectory = getAppFiles(`
        
        
        
        
        
        
        
        
        
        
      `);
      const template = `
      …
        if (rf & 2) {
          i0.ɵɵattributeInterpolateV("title", ["a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i", ctx.nine, "j"]);
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate8("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate7("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate6("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate5("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate4("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate3("title", "a", ctx.one, "b", ctx.two, "c", ctx.three, "d");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate2("title", "a", ctx.one, "b", ctx.two, "c");
          i0.ɵɵadvance(1);
          i0.ɵɵattributeInterpolate1("title", "a", ctx.one, "b");
          i0.ɵɵadvance(1);
          i0.ɵɵattribute("title", ctx.one);
      }
      …
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of interpolated properties');
    });
    it('should keep local ref for host element', () => {
      const files: MockDirectory = getAppFiles(`
        
          Hello {{ name }}! 
         
        {{ myRef.id }}
      `);
      const template = `
        …
        consts: [["id", "my-id"], ["myRef", ""]],
        template:function MyComponent_Template(rf, $ctx$){
          if (rf & 1) {
            $i0$.ɵɵelementStart(0, "b", 0, 1);
            $i0$.ɵɵdisableBindings();
            $i0$.ɵɵelementStart(2, "i");
            $i0$.ɵɵtext(3, "Hello {{ name }}!");
            $i0$.ɵɵelementEnd();
            $i0$.ɵɵenableBindings();
            $i0$.ɵɵelementEnd();
            $i0$.ɵɵtext(4);
          }
          if (rf & 2) {
            const $_r0$ = $i0$.ɵɵreference(1);
            $r3$.ɵɵadvance(4);
            $i0$.ɵɵtextInterpolate1(" ", $_r0$.id, " ");
          }
        }
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of local refs for host element');
    });
    it('should not have local refs for nested elements', () => {
      const files: MockDirectory = getAppFiles(`
       
         
      `);
      const template = `
        …
        consts: [["value", "one", "#myInput", ""]],
        template:function MyComponent_Template(rf, $ctx$){
          if (rf & 1) {
            $i0$.ɵɵelementStart(0, "div");
            $i0$.ɵɵdisableBindings();
            $i0$.ɵɵelement(1, "input", 0);
            $i0$.ɵɵtext(2, " {{ myInput.value }} ");
            $i0$.ɵɵenableBindings();
            $i0$.ɵɵelementEnd();
        }
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of local refs for nested elements');
    });
    it('should not process property bindings and listeners', () => {
      const files: MockDirectory = getAppFiles(`
        
      `);
      const template = `
        …
        consts: [["[id]", "my-id", "(click)", "onclick"]],
        template:function MyComponent_Template(rf, $ctx$){
          if (rf & 1) {
            $i0$.ɵɵelementStart(0, "div");
            $i0$.ɵɵdisableBindings();
            $i0$.ɵɵelement(1, "div", 0);
            $i0$.ɵɵenableBindings();
            $i0$.ɵɵelementEnd();
        }
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of property bindings and listeners');
    });
    it('should not generate extra instructions for elements with no children', () => {
      const files: MockDirectory = getAppFiles(`
        
      `);
      const template = `
        template:function MyComponent_Template(rf, $ctx$){
          if (rf & 1) {
            $i0$.ɵɵelement(0, "div");
          }
        }
      `;
      const result = compile(files, angularFiles);
      expectEmit(result.source, template, 'Incorrect handling of elements with no children');
    });
  });
});