Karma TypeError caused by a typeless angular component input The Next CEO of Stack Overflow@Directive v/s @Component in AngularAngular - Use pipes in services and componentsAngular pass callback function to child component as @InputAngular2 DomSanitizer issueAngular2 NgModel not getting value in Jasmine testAngular 2 Karma Test 'component-name' is not a known elementMocking an Angular Service that has RxJS Observables for testingAngular testing with keycloack “user is not logged in”Using materialize-css (v 1.0.0) in Angular 5 does not workNgZone undefined when testing Angular component

If the heap is initialized for security, then why is the stack uninitialized?

Do I need to enable Dev Hub in my PROD Org?

What exact does MIB represent in SNMP? How is it different from OID?

What flight has the highest ratio of time difference to flight time?

Is 'diverse range' a pleonastic phrase?

Written every which way

Is there a difference between "Fahrstuhl" and "Aufzug"

What is the result of assigning to std::vector<T>::begin()?

Contours of a clandestine nature

Multiple labels for a single equation

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

If a black hole is created from light, can this black hole then move at speed of light?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Complex fractions

Are there any limitations on attacking while grappling?

How do I reset passwords on multiple websites easily?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Would a completely good Muggle be able to use a wand?

How do I go from 300 unfinished/half written blog posts, to published posts?

Interfacing a button to MCU (and PC) with 50m long cable

Why do we use the plural of movies in this phrase "We went to the movies last night."?

Rotate a column

Is it possible to search for a directory/file combination?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?



Karma TypeError caused by a typeless angular component input



The Next CEO of Stack Overflow@Directive v/s @Component in AngularAngular - Use pipes in services and componentsAngular pass callback function to child component as @InputAngular2 DomSanitizer issueAngular2 NgModel not getting value in Jasmine testAngular 2 Karma Test 'component-name' is not a known elementMocking an Angular Service that has RxJS Observables for testingAngular testing with keycloack “user is not logged in”Using materialize-css (v 1.0.0) in Angular 5 does not workNgZone undefined when testing Angular component










0















I am receiving this error:



TypeError: Cannot read property 'url' of undefined


Cause by this section of code:



 export class ImageCardComponent implements OnInit {
@Input() image: any; // Can be an image or imageToUpload
imageUrls = [];


...

ngOnInit()
this.imageUrls.push((this.image as any).url);
this.image.outOfSync = true;



test code



describe('ImageCardComponent', () => 
let component: ImageCardComponent;
let fixture: ComponentFixture<ImageCardComponent>;

beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [ ImageCardComponent ],
imports: [FormsModule],
)
.compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(ImageCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.image = getImageGetResponseMock().image;
);

it('should create', () =>
expect(component).toBeTruthy();
);
);


mock data



export function getImageGetResponseMock() 
return
id: 1,
image: [

id: 2,
height: 90,
width: 160,
format: 'jpg',
url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
tags: ['testTag']

]
;



How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?










share|improve this question




























    0















    I am receiving this error:



    TypeError: Cannot read property 'url' of undefined


    Cause by this section of code:



     export class ImageCardComponent implements OnInit {
    @Input() image: any; // Can be an image or imageToUpload
    imageUrls = [];


    ...

    ngOnInit()
    this.imageUrls.push((this.image as any).url);
    this.image.outOfSync = true;



    test code



    describe('ImageCardComponent', () => 
    let component: ImageCardComponent;
    let fixture: ComponentFixture<ImageCardComponent>;

    beforeEach(async(() =>
    TestBed.configureTestingModule(
    declarations: [ ImageCardComponent ],
    imports: [FormsModule],
    )
    .compileComponents();
    ));

    beforeEach(() =>
    fixture = TestBed.createComponent(ImageCardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.image = getImageGetResponseMock().image;
    );

    it('should create', () =>
    expect(component).toBeTruthy();
    );
    );


    mock data



    export function getImageGetResponseMock() 
    return
    id: 1,
    image: [

    id: 2,
    height: 90,
    width: 160,
    format: 'jpg',
    url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
    tags: ['testTag']

    ]
    ;



    How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?










    share|improve this question


























      0












      0








      0








      I am receiving this error:



      TypeError: Cannot read property 'url' of undefined


      Cause by this section of code:



       export class ImageCardComponent implements OnInit {
      @Input() image: any; // Can be an image or imageToUpload
      imageUrls = [];


      ...

      ngOnInit()
      this.imageUrls.push((this.image as any).url);
      this.image.outOfSync = true;



      test code



      describe('ImageCardComponent', () => 
      let component: ImageCardComponent;
      let fixture: ComponentFixture<ImageCardComponent>;

      beforeEach(async(() =>
      TestBed.configureTestingModule(
      declarations: [ ImageCardComponent ],
      imports: [FormsModule],
      )
      .compileComponents();
      ));

      beforeEach(() =>
      fixture = TestBed.createComponent(ImageCardComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
      component.image = getImageGetResponseMock().image;
      );

      it('should create', () =>
      expect(component).toBeTruthy();
      );
      );


      mock data



      export function getImageGetResponseMock() 
      return
      id: 1,
      image: [

      id: 2,
      height: 90,
      width: 160,
      format: 'jpg',
      url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
      tags: ['testTag']

      ]
      ;



      How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?










      share|improve this question
















      I am receiving this error:



      TypeError: Cannot read property 'url' of undefined


      Cause by this section of code:



       export class ImageCardComponent implements OnInit {
      @Input() image: any; // Can be an image or imageToUpload
      imageUrls = [];


      ...

      ngOnInit()
      this.imageUrls.push((this.image as any).url);
      this.image.outOfSync = true;



      test code



      describe('ImageCardComponent', () => 
      let component: ImageCardComponent;
      let fixture: ComponentFixture<ImageCardComponent>;

      beforeEach(async(() =>
      TestBed.configureTestingModule(
      declarations: [ ImageCardComponent ],
      imports: [FormsModule],
      )
      .compileComponents();
      ));

      beforeEach(() =>
      fixture = TestBed.createComponent(ImageCardComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
      component.image = getImageGetResponseMock().image;
      );

      it('should create', () =>
      expect(component).toBeTruthy();
      );
      );


      mock data



      export function getImageGetResponseMock() 
      return
      id: 1,
      image: [

      id: 2,
      height: 90,
      width: 160,
      format: 'jpg',
      url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
      tags: ['testTag']

      ]
      ;



      How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?







      angular typescript karma-jasmine






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 15:10







      Tyler B. Joudrey

















      asked Mar 8 at 14:29









      Tyler B. JoudreyTyler B. Joudrey

      549




      549






















          1 Answer
          1






          active

          oldest

          votes


















          1














          imageUrl is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.



          You should be mocking the objects needed in your tests and then write your test cases.



          const imageUrl = image: 'testImage.png', url:'image_url';


          Now After TestBed set up, have a beforeEach block to mock the needed inputs:



          beforeEach(() => 
          const fixture = TestBed.createComponent(ImageCardComponent );
          component = fixture.componentInstance;
          component.imageUrl= imageUrl;
          );

          it('should have image url info', () =>
          expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
          );





          share|improve this answer























          • Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

            – Tyler B. Joudrey
            Mar 8 at 15:05











          • Can you share the test code here if possible to see what's going on?

            – nircraft
            Mar 8 at 15:07











          • I added the test code, and the mock for an image

            – Tyler B. Joudrey
            Mar 8 at 15:10











          • Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

            – nircraft
            Mar 8 at 15:23






          • 1





            Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

            – nircraft
            Mar 8 at 15:53











          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%2f55065265%2fkarma-typeerror-caused-by-a-typeless-angular-component-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          imageUrl is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.



          You should be mocking the objects needed in your tests and then write your test cases.



          const imageUrl = image: 'testImage.png', url:'image_url';


          Now After TestBed set up, have a beforeEach block to mock the needed inputs:



          beforeEach(() => 
          const fixture = TestBed.createComponent(ImageCardComponent );
          component = fixture.componentInstance;
          component.imageUrl= imageUrl;
          );

          it('should have image url info', () =>
          expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
          );





          share|improve this answer























          • Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

            – Tyler B. Joudrey
            Mar 8 at 15:05











          • Can you share the test code here if possible to see what's going on?

            – nircraft
            Mar 8 at 15:07











          • I added the test code, and the mock for an image

            – Tyler B. Joudrey
            Mar 8 at 15:10











          • Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

            – nircraft
            Mar 8 at 15:23






          • 1





            Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

            – nircraft
            Mar 8 at 15:53















          1














          imageUrl is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.



          You should be mocking the objects needed in your tests and then write your test cases.



          const imageUrl = image: 'testImage.png', url:'image_url';


          Now After TestBed set up, have a beforeEach block to mock the needed inputs:



          beforeEach(() => 
          const fixture = TestBed.createComponent(ImageCardComponent );
          component = fixture.componentInstance;
          component.imageUrl= imageUrl;
          );

          it('should have image url info', () =>
          expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
          );





          share|improve this answer























          • Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

            – Tyler B. Joudrey
            Mar 8 at 15:05











          • Can you share the test code here if possible to see what's going on?

            – nircraft
            Mar 8 at 15:07











          • I added the test code, and the mock for an image

            – Tyler B. Joudrey
            Mar 8 at 15:10











          • Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

            – nircraft
            Mar 8 at 15:23






          • 1





            Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

            – nircraft
            Mar 8 at 15:53













          1












          1








          1







          imageUrl is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.



          You should be mocking the objects needed in your tests and then write your test cases.



          const imageUrl = image: 'testImage.png', url:'image_url';


          Now After TestBed set up, have a beforeEach block to mock the needed inputs:



          beforeEach(() => 
          const fixture = TestBed.createComponent(ImageCardComponent );
          component = fixture.componentInstance;
          component.imageUrl= imageUrl;
          );

          it('should have image url info', () =>
          expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
          );





          share|improve this answer













          imageUrl is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.



          You should be mocking the objects needed in your tests and then write your test cases.



          const imageUrl = image: 'testImage.png', url:'image_url';


          Now After TestBed set up, have a beforeEach block to mock the needed inputs:



          beforeEach(() => 
          const fixture = TestBed.createComponent(ImageCardComponent );
          component = fixture.componentInstance;
          component.imageUrl= imageUrl;
          );

          it('should have image url info', () =>
          expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 14:41









          nircraftnircraft

          1,9881524




          1,9881524












          • Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

            – Tyler B. Joudrey
            Mar 8 at 15:05











          • Can you share the test code here if possible to see what's going on?

            – nircraft
            Mar 8 at 15:07











          • I added the test code, and the mock for an image

            – Tyler B. Joudrey
            Mar 8 at 15:10











          • Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

            – nircraft
            Mar 8 at 15:23






          • 1





            Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

            – nircraft
            Mar 8 at 15:53

















          • Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

            – Tyler B. Joudrey
            Mar 8 at 15:05











          • Can you share the test code here if possible to see what's going on?

            – nircraft
            Mar 8 at 15:07











          • I added the test code, and the mock for an image

            – Tyler B. Joudrey
            Mar 8 at 15:10











          • Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

            – nircraft
            Mar 8 at 15:23






          • 1





            Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

            – nircraft
            Mar 8 at 15:53
















          Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

          – Tyler B. Joudrey
          Mar 8 at 15:05





          Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail

          – Tyler B. Joudrey
          Mar 8 at 15:05













          Can you share the test code here if possible to see what's going on?

          – nircraft
          Mar 8 at 15:07





          Can you share the test code here if possible to see what's going on?

          – nircraft
          Mar 8 at 15:07













          I added the test code, and the mock for an image

          – Tyler B. Joudrey
          Mar 8 at 15:10





          I added the test code, and the mock for an image

          – Tyler B. Joudrey
          Mar 8 at 15:10













          Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

          – nircraft
          Mar 8 at 15:23





          Once you do component.image = getImageGetResponseMock().image; you should run fixture.detectChanges(); to make sure your changes are available to be used

          – nircraft
          Mar 8 at 15:23




          1




          1





          Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

          – nircraft
          Mar 8 at 15:53





          Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…

          – nircraft
          Mar 8 at 15:53



















          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%2f55065265%2fkarma-typeerror-caused-by-a-typeless-angular-component-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

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived