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

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page