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

Code before:
            completedChallenges: [
              { id: 'bd7158d8c442eddfaeb5bd18', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b03', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b04', completedDate: 123456789 },
              { id: '587d78b0367417b2b2512b05', completedDate: 123456789 },
              { id: 'bd7158d8c242eddfaeb5bd13', completedDate: 123456789 }
            ],
            is2018DataVisCert: true,
            isA2EnglishCert: true,
            isB1EnglishCert: true,
            isApisMicroservicesCert: true,
            isCollegeAlgebraPyCertV8: true,
            isDataAnalysisPyCertV7: true,
            isFoundationalCSharpCertV8: true,
            isRelationalDatabaseCertV8: true,
            isRelationalDatabaseCertV9: true,
            isRespWebDesignCert: false,
            isRespWebDesignCertV9: true,
            isSciCompPyCertV7: true,
            isFullStackDeveloperCertV9: true
          }
        });

        const spy = vi.spyOn(fastifyTestInstance, 'sendEmail');

        const response = await superRequest('/certificate/verify', {
          method: 'PUT',
          setCookies
        }).send({
          certSlug: Certification.RespWebDesign
        });

        expect(spy).toHaveBeenCalled();
        expect(response.status).toBe(200);
      });

      test('should return 200 if all went well', async () => {
        await fastifyTestInstance.prisma.user.updateMany({
          where: { email: defaultUserEmail },
          data: {
            completedChallenges: [
              { id: 'bd7158d8c442eddfaeb5bd18', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b03', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b04', completedDate: 123456789 },
              { id: '587d78b0367417b2b2512b05', completedDate: 123456789 },
              { id: 'bd7158d8c242eddfaeb5bd13', completedDate: 123456789 }
            ],
            isRespWebDesignCert: false
          }
        });


Review comment (suggestion):
The list of claimed certs here does not make sense. Most of these are not "current" certs, and some of the current certs have been removed.

It seems like the desired logic is:
- Camper must claim a select few certs in order to claim the `isFullStackDeveloperCertV9`
- Once `isFull...V9` is claimed, send email

Ideally, we would have two tests linked:
1) Claim all certs needed to claim full stack
2) Claim full stack, and ensure email is sent

Code after:
            completedChallenges: [
              { id: 'bd7158d8c442eddfaeb5bd18', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b03', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b04', completedDate: 123456789 },
              { id: '587d78b0367417b2b2512b05', completedDate: 123456789 },
              { id: 'bd7158d8c242eddfaeb5bd13', completedDate: 123456789 }
            ],
            isFullStackDeveloperCertV9: true
          }
        });

        const spy = vi.spyOn(fastifyTestInstance, 'sendEmail');

        const response = await superRequest('/certificate/verify', {
          method: 'PUT',
          setCookies
        }).send({
          certSlug: Certification.RespWebDesign
        });

        expect(spy).toHaveBeenCalled();
        expect(response.status).toBe(200);
      });

      test('should return 200 if all went well', async () => {
        await fastifyTestInstance.prisma.user.updateMany({
          where: { email: defaultUserEmail },
          data: {
            completedChallenges: [
              { id: 'bd7158d8c442eddfaeb5bd18', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b03', completedDate: 123456789 },
              { id: '587d78af367417b2b2512b04', completedDate: 123456789 },
              { id: '587d78b0367417b2b2512b05', completedDate: 123456789 },
              { id: 'bd7158d8c242eddfaeb5bd13', completedDate: 123456789 }
            ],
            isRespWebDesignCert: false
          }
        });

        const response = await superRequest('/certificate/verify', {
          method: 'PUT',
          setCookies
        }).send({
          certSlug: Certification.RespWebDesign
        });

        const user = await fastifyTestInstance.prisma.user.findFirst({
          where: { email: defaultUserEmail }
        });

        expect(user).toMatchObject({ isRespWebDesignCert: true });