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

Code before:
          email: unusedEmailTwo
        });

        expect(failResponse?.statusCode).toEqual(429);

        expect(failResponse?.body).toEqual({
          type: 'info',
          message: `Please wait 5 minutes to resend an authentication link.`
        });
      });

      test('PUT sends an email to the new email address', async () => {
        jest
          .spyOn(fastifyTestInstance.prisma.authToken, 'create')
          .mockImplementationOnce(() =>
            // @ts-expect-error This is a mock implementation, all we're
            // interested in is the id.
            Promise.resolve({
              id: '123'
            })
          );
        await superPut('/update-my-email').send({
          email: unusedEmailOne
        });

        const expectedLink = `http://localhost:3000/confirm-email?email=${Buffer.from(unusedEmailOne).toString('base64')}&token=123&emailChange=true`;
        expect(sendEmailSpy).toHaveBeenCalledWith({
          from: 'team@freecodecamp.org',
          to: unusedEmailOne,
          subject:
            'Please confirm your updated email address for freeCodeCamp.org',
          text: `Please confirm this email address for freeCodeCamp.org:

${expectedLink}

Happy coding!

- The freeCodeCamp.org Team
`
        });
      });

      test('PUT creates an auth token record for the requesting user', async () => {
        const noToken = await fastifyTestInstance.prisma.authToken.findFirst({
          where: { userId: defaultUserId }
        });
        expect(noToken).toBeNull();

        await superPut('/update-my-email').send({
          email: unusedEmailOne
        });

Review comment (suggestion):
```suggestion
        const expectedLink = `${API_LOCATION}/confirm-email?email=${Buffer.from(unusedEmailOne).toString('base64')}&token=123&emailChange=true`;
```

Code after:
          email: unusedEmailTwo
        });

        expect(failResponse?.statusCode).toEqual(429);

        expect(failResponse?.body).toEqual({
          type: 'info',
          message: `Please wait 5 minutes to resend an authentication link.`
        });
      });

      test('PUT sends an email to the new email address', async () => {
        jest
          .spyOn(fastifyTestInstance.prisma.authToken, 'create')
          .mockImplementationOnce(() =>
            // @ts-expect-error This is a mock implementation, all we're
            // interested in is the id.
            Promise.resolve({
              id: '123'
            })
          );
        await superPut('/update-my-email').send({
          email: unusedEmailOne
        });

        const expectedLink = `${API_LOCATION}/confirm-email?email=${Buffer.from(unusedEmailOne).toString('base64')}&token=123&emailChange=true`;
        expect(sendEmailSpy).toHaveBeenCalledWith({
          from: 'team@freecodecamp.org',
          to: unusedEmailOne,
          subject:
            'Please confirm your updated email address for freeCodeCamp.org',
          text: `Please confirm this email address for freeCodeCamp.org:

${expectedLink}

Happy coding!

- The freeCodeCamp.org Team
`
        });
      });

      test('PUT creates an auth token record for the requesting user', async () => {
        const noToken = await fastifyTestInstance.prisma.authToken.findFirst({
          where: { userId: defaultUserId }
        });
        expect(noToken).toBeNull();

        await superPut('/update-my-email').send({
          email: unusedEmailOne
        });