'Javascript Books challenge

I tried to complete a javascript assessment for a job interview but failed

My plan was to process the user object inside findPotentialLikes function but it'll always throw undefined.

It is made using Mocha and Chai testing framework and promises.

Basically my question is: how do i get the user in the findPotentialLikes promise ?

class SocialNetworkQueries {

    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        
     console.log(this.fetchCurentUser);
     // I tried here to get the user in order to process the findPotentialLikes function with no luck

        return Promise.resolve();
        
    }

}


describe('SocialNetworkQueries', () => {
  describe("example from README", () => {

    it("should find potential likes", async () => {
      // given
      const user = {
        id: "mrouk3",
        likes: {
          books: ["Moby Dick", "Ulysses"],
        },
        friends: [{
          id: "YazL",
          likes: {
            books: ["Ulysses", "War and Peace"],
          }
        }],
      };

      // when
      const potentialLikes = await new SocialNetworkQueries({
        fetchCurrentUser: () => Promise.resolve(user),
      }).findPotentialLikes({ minimalScore: 0.3 });

      // then
      expect(potentialLikes).toEqual({
        books: [
          "The Great Gatsby",
          "Don Quixote",
          "War and Peace",
        ],
      });
    });

  });
});


Solution 1:[1]

Given that the fetchCurrentUser argument is provided by the test as a function which returns a promise which resolves to a user value, you would need to retrieve the resolved value from the promise, either via async/await:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    async findPotentialLikes({ minimalScore } = { }) {
        const user = await this.fetchCurrentUser();

        // ...

        return {
            books: [
                "The Great Gatsby",
                "Don Quixote",
                "War and Peace",
            ]
        };
    }
}

or logic in a .then() callback which will execute after the user has resolved:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        return this.fetchCurrentUser().then(
            user => {
                // ...

                return {
                    books: [
                        "The Great Gatsby",
                        "Don Quixote",
                        "War and Peace",
                    ]
                };
            }
        );
    }
}

Either approach is functionally equivalent and results in findPotentialLikes() returning a Promise which resolves with the asserted value from the test.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bosco