Angular Testing Check isDirty on ngModel Input2019 Community Moderator ElectionHow should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Running unittest with typical test directory structureAngular HTML bindingCan't bind to 'ngModel' since it isn't a known property of 'input'Angular2 NgModel not getting value in Jasmine test

What (if any) is the reason to buy in small local stores?

How to test the sharpness of a knife?

PTIJ: Which Dr. Seuss books should one obtain?

Would this string work as string?

Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?

Can "few" be used as a subject? If so, what is the rule?

Are hand made posters acceptable in Academia?

Print last inputted byte

Is VPN a layer 3 concept?

Nested Dynamic SOQL Query

How old is Nick Fury?

Have the tides ever turned twice on any open problem?

How to understand 「僕は誰より彼女が好きなんだ。」

Determine voltage drop over 10G resistors with cheap multimeter

How do researchers send unsolicited emails asking for feedback on their works?

Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?

"Marked down as someone wanting to sell shares." What does that mean?

Why is this tree refusing to shed its dead leaves?

Why doesn't the fusion process of the sun speed up?

How can an organ that provides biological immortality be unable to regenerate?

The English Debate

Why do I have a large white artefact on the rendered image?

Is it okay for a cleric of life to use spells like Animate Dead and/or Contagion?

Why I don't get the wanted width of tcbox?



Angular Testing Check isDirty on ngModel Input



2019 Community Moderator ElectionHow should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Running unittest with typical test directory structureAngular HTML bindingCan't bind to 'ngModel' since it isn't a known property of 'input'Angular2 NgModel not getting value in Jasmine test










0















I'm trying to test an input that's declared like this:



<input
#monthElement
maxlength="2"
type="text"
name="month"
ngModel
#month="ngModel"
placeholder="MM"
/>


In some of the functions that are run to validate that the month that's been entered is valid I do a check like this (this is just stubbed out):



isValid(input) 
return input && this.isValidMonth(input.value) && input.isDirty



That isValid function is called from the template, and the month variable is passed into it. Everything works as expected.



In my unit tests, I want to test that isValid function, but it doesn't work because input.isDirty is undefined. This is my test function so far:



it('should declare the month input value to be valid if the value is 01', async () => 
const monthInputElement: DebugElement = fixture.debugElement.query(By.css('input[name="month"]'));
monthInputElement.nativeElement.value = '01';
monthInputElement.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
console.log('monthInputElement.nativeElement.value: ', monthInputElement.nativeElement.value);

console.log('isDirty: ', monthInputElement.nativeElement.dirty);
const isValid = component.isMonthValid(monthInputElement.nativeElement.value);

expect(isValid).toBe(true);
);


I have confirmed with that console.log on the nativeElement.value that the value is changed, but nativeElement.isDirty is undefined. How can I test this component and the function so that I have the same element for the test as the template is passing to the function?



Edit



I've also tried to get isDirty working on an input field with reactive forms:



// my.component.ts
public testFormGroup: FormGroup = new FormGroup(
testField: new FormControl(),
);

<!-- my.component.html -->
<form [formGroup]="testFormGroup">
<input type="text" formControlName="testField" />
</form>

// my.component.spec.ts
component.testFormGroup.controls.testField.setValue('01');
console.log('testField Value', component.testFormGroup.controls.testField.value);
console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
fixture.detectChanges();
fixture.whenStable().then(() =>
console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
expect(component.testFormGroup.controls.testField.dirty).toBe(true);
);


The only difference with the reactive form is that .dirty is false and not undefined.










share|improve this question




























    0















    I'm trying to test an input that's declared like this:



    <input
    #monthElement
    maxlength="2"
    type="text"
    name="month"
    ngModel
    #month="ngModel"
    placeholder="MM"
    />


    In some of the functions that are run to validate that the month that's been entered is valid I do a check like this (this is just stubbed out):



    isValid(input) 
    return input && this.isValidMonth(input.value) && input.isDirty



    That isValid function is called from the template, and the month variable is passed into it. Everything works as expected.



    In my unit tests, I want to test that isValid function, but it doesn't work because input.isDirty is undefined. This is my test function so far:



    it('should declare the month input value to be valid if the value is 01', async () => 
    const monthInputElement: DebugElement = fixture.debugElement.query(By.css('input[name="month"]'));
    monthInputElement.nativeElement.value = '01';
    monthInputElement.nativeElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();
    console.log('monthInputElement.nativeElement.value: ', monthInputElement.nativeElement.value);

    console.log('isDirty: ', monthInputElement.nativeElement.dirty);
    const isValid = component.isMonthValid(monthInputElement.nativeElement.value);

    expect(isValid).toBe(true);
    );


    I have confirmed with that console.log on the nativeElement.value that the value is changed, but nativeElement.isDirty is undefined. How can I test this component and the function so that I have the same element for the test as the template is passing to the function?



    Edit



    I've also tried to get isDirty working on an input field with reactive forms:



    // my.component.ts
    public testFormGroup: FormGroup = new FormGroup(
    testField: new FormControl(),
    );

    <!-- my.component.html -->
    <form [formGroup]="testFormGroup">
    <input type="text" formControlName="testField" />
    </form>

    // my.component.spec.ts
    component.testFormGroup.controls.testField.setValue('01');
    console.log('testField Value', component.testFormGroup.controls.testField.value);
    console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
    fixture.detectChanges();
    fixture.whenStable().then(() =>
    console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
    expect(component.testFormGroup.controls.testField.dirty).toBe(true);
    );


    The only difference with the reactive form is that .dirty is false and not undefined.










    share|improve this question


























      0












      0








      0








      I'm trying to test an input that's declared like this:



      <input
      #monthElement
      maxlength="2"
      type="text"
      name="month"
      ngModel
      #month="ngModel"
      placeholder="MM"
      />


      In some of the functions that are run to validate that the month that's been entered is valid I do a check like this (this is just stubbed out):



      isValid(input) 
      return input && this.isValidMonth(input.value) && input.isDirty



      That isValid function is called from the template, and the month variable is passed into it. Everything works as expected.



      In my unit tests, I want to test that isValid function, but it doesn't work because input.isDirty is undefined. This is my test function so far:



      it('should declare the month input value to be valid if the value is 01', async () => 
      const monthInputElement: DebugElement = fixture.debugElement.query(By.css('input[name="month"]'));
      monthInputElement.nativeElement.value = '01';
      monthInputElement.nativeElement.dispatchEvent(new Event('input'));
      fixture.detectChanges();
      console.log('monthInputElement.nativeElement.value: ', monthInputElement.nativeElement.value);

      console.log('isDirty: ', monthInputElement.nativeElement.dirty);
      const isValid = component.isMonthValid(monthInputElement.nativeElement.value);

      expect(isValid).toBe(true);
      );


      I have confirmed with that console.log on the nativeElement.value that the value is changed, but nativeElement.isDirty is undefined. How can I test this component and the function so that I have the same element for the test as the template is passing to the function?



      Edit



      I've also tried to get isDirty working on an input field with reactive forms:



      // my.component.ts
      public testFormGroup: FormGroup = new FormGroup(
      testField: new FormControl(),
      );

      <!-- my.component.html -->
      <form [formGroup]="testFormGroup">
      <input type="text" formControlName="testField" />
      </form>

      // my.component.spec.ts
      component.testFormGroup.controls.testField.setValue('01');
      console.log('testField Value', component.testFormGroup.controls.testField.value);
      console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
      fixture.detectChanges();
      fixture.whenStable().then(() =>
      console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
      expect(component.testFormGroup.controls.testField.dirty).toBe(true);
      );


      The only difference with the reactive form is that .dirty is false and not undefined.










      share|improve this question
















      I'm trying to test an input that's declared like this:



      <input
      #monthElement
      maxlength="2"
      type="text"
      name="month"
      ngModel
      #month="ngModel"
      placeholder="MM"
      />


      In some of the functions that are run to validate that the month that's been entered is valid I do a check like this (this is just stubbed out):



      isValid(input) 
      return input && this.isValidMonth(input.value) && input.isDirty



      That isValid function is called from the template, and the month variable is passed into it. Everything works as expected.



      In my unit tests, I want to test that isValid function, but it doesn't work because input.isDirty is undefined. This is my test function so far:



      it('should declare the month input value to be valid if the value is 01', async () => 
      const monthInputElement: DebugElement = fixture.debugElement.query(By.css('input[name="month"]'));
      monthInputElement.nativeElement.value = '01';
      monthInputElement.nativeElement.dispatchEvent(new Event('input'));
      fixture.detectChanges();
      console.log('monthInputElement.nativeElement.value: ', monthInputElement.nativeElement.value);

      console.log('isDirty: ', monthInputElement.nativeElement.dirty);
      const isValid = component.isMonthValid(monthInputElement.nativeElement.value);

      expect(isValid).toBe(true);
      );


      I have confirmed with that console.log on the nativeElement.value that the value is changed, but nativeElement.isDirty is undefined. How can I test this component and the function so that I have the same element for the test as the template is passing to the function?



      Edit



      I've also tried to get isDirty working on an input field with reactive forms:



      // my.component.ts
      public testFormGroup: FormGroup = new FormGroup(
      testField: new FormControl(),
      );

      <!-- my.component.html -->
      <form [formGroup]="testFormGroup">
      <input type="text" formControlName="testField" />
      </form>

      // my.component.spec.ts
      component.testFormGroup.controls.testField.setValue('01');
      console.log('testField Value', component.testFormGroup.controls.testField.value);
      console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
      fixture.detectChanges();
      fixture.whenStable().then(() =>
      console.log('testField isDirty', component.testFormGroup.controls.testField.dirty);
      expect(component.testFormGroup.controls.testField.dirty).toBe(true);
      );


      The only difference with the reactive form is that .dirty is false and not undefined.







      angular unit-testing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 16:36







      pjlamb12

















      asked Mar 6 at 23:45









      pjlamb12pjlamb12

      64411029




      64411029






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033943%2fangular-testing-check-isdirty-on-ngmodel-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033943%2fangular-testing-check-isdirty-on-ngmodel-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко