title: How to write the unit testing for the file upload method in the Angular 7 (or 2+)
body: <p>I'm trying to write the unit testing for the file upload method in the angular 7. Getting the below error in the testing window. I'm new for angular unit testing. Could someone help, How to add mock files to get the full code coverage? </p>
  
  <blockquote>
    <p>TypeError: Cannot set property 'value' of undefined</p>
  </blockquote>
  
  <p>Here is my unit test code (spec file),</p>
  
  <pre><code>describe('ImportComponent', () =&gt; {
      let component: ImportComponent;
      let fixture: ComponentFixture&lt;ImportComponent&gt;;
      let element;
  
      beforeEach(
          async(() =&gt; {
              TestBed.configureTestingModule({
                  imports: [ HttpClientModule, RouterTestingModule ],
                  declarations: [ ImportComponent ]
              }).compileComponents();
          })
      );
  
      beforeEach(() =&gt; {
          fixture = TestBed.createComponent(ImportComponent);
          component = fixture.componentInstance;
          element = fixture.nativeElement;
          fixture.detectChanges();
      });
  
      it('should create', () =&gt; {
          expect(component).toBeTruthy();
      });
  
      it('should upload the file', () =&gt; {
          component.importFile();
          const inputEl = element.querySelector('#postal_file');
          const fileList = { 0: { name: 'foo', size: 500001 } };
          inputEl.value = {
              target: {
                  files: fileList
              }
          };
          inputEl.dispatchEvent(new Event('change'));     
      });
  });
  </code></pre>
  
  <p>Method in the component</p>
  
  <pre><code>importFile() {
          const inputEl: HTMLInputElement = this.el.nativeElement.querySelector('#postal_file');
          const fileCount: number = inputEl.files.length;
          const formData = new FormData();
          if (fileCount &gt; 0) {
              formData.append(this.postalFileName, inputEl.files.item(0));
              this.postalService.importPostalCodes(formData).subscribe((data) =&gt; {
                  this.result = data;
              });
          }
      }
  </code></pre>
  
  <p>and the HTML,</p>
  
  <pre><code>&lt;div class="file-import"&gt;
          &lt;form action="" method="post" encType="multipart/form-data"&gt;
              &lt;label for="postal_file"&gt;Choose File&lt;/label&gt;
              &lt;input type="file" name="postal_file" id="postal_file"&gt;
              &lt;button type="button" (click)="importFile()"&gt;Import&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>The unit test is not covered all the code as shown in the below image, please help , how to ge the 100% code coverage.</p>
  
  <p><a href="https://i.stack.imgur.com/79N1b.png" rel="noreferrer">Code coverage image</a></p>