Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/challenge.test.ts (TypeScript)

Code before:
      describe('handling', () => {
        afterEach(async () => {
          await fastifyTestInstance.prisma.user.updateMany({
            where: { email: 'foo@bar.com' },
            data: {
              savedChallenges: []
            }
          });
        });

        test('POST update the user savedchallenges and return them', async () => {
          const now = Date.now();
          const response = await superRequest('/save-challenge', {
            method: 'POST',
            setCookies
          }).send({
            id: multiFileCertProjectId,
            lastSavedDate: now,
            files: updatedMultiFileCertProjectBody.files
          });

          const user = await fastifyTestInstance?.prisma.user.findFirst({
            where: { email: 'foo@bar.com' }
          });

          const savedDate = user?.savedChallenges[0]?.lastSavedDate;

          expect(user).toMatchObject({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.body).toEqual({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(user?.savedChallenges).toHaveLength(1);
          expect(response.statusCode).toBe(200);
        });
      });
    });
  });

Review comment (suggestion):
```suggestion
          const savedDate = user.savedChallenges[0]?.lastSavedDate;
```

Code after:
      describe('handling', () => {
        afterEach(async () => {
          await fastifyTestInstance.prisma.user.updateMany({
            where: { email: 'foo@bar.com' },
            data: {
              savedChallenges: []
            }
          });
        });

        test('POST update the user savedchallenges and return them', async () => {
          const response = await superRequest('/save-challenge', {
            method: 'POST',
            setCookies
          }).send({
            id: multiFileCertProjectId,
            files: updatedMultiFileCertProjectBody.files
          });

          const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
            where: { email: 'foo@bar.com' }
          });

          const savedDate = user.savedChallenges[0]?.lastSavedDate;

          expect(user).toMatchObject({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.body).toEqual({
            savedChallenges: [
              {
                id: multiFileCertProjectId,
                lastSavedDate: savedDate,
                files: updatedMultiFileCertProjectBody.files
              }
            ]
          });
          expect(response.statusCode).toBe(200);
        });
      });
    });
  });

  describe('Unauthenticated user', () => {
    let setCookies: string[];