jeudi 28 février 2019

How do I test that forEach is calling savePost three times?

I am learning how to write tests in JavaScript and I have this code here:

function handlePosts() {
    var posts = [
      { id: 23, title: 'Me Gusta JS' },
      { id: 52, title: 'Ciudad Código' },
      { id: 105, title: 'Programar Ya' }
    ];

    for (var i = 0; i < posts.length; i++) {
      savePost(posts[i]);
    }
}

that calls savePost three times, but I am wanting to ensure that when I or someone else utilizes, specifically the forEach helper method, that one of my tests looks for the forEach actually calling savePost three times.

I already developed a test to check that the forEach exists, in other words its being used as opposed to some other array helper method, but not sure how to test that its doing what it should be doing.

describe('forEach', function() {
    it('forEach method exists', () => {
        expect(forEach).toBeDefined();
    });

    it('forEach is calling savePost three times', () => {

    });
});

If anyone could walk me through this, not just looking for the answer, looking to learn how to think about this.

I imagine something like expect(savePost.length).toEqual(3);, but I am not sure.

Aucun commentaire:

Enregistrer un commentaire