Skip to content

Commit

Permalink
fix/MSSDK-2112: Fix behaviour if challange called (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelacio authored Jan 24, 2025
1 parent 42570a3 commit b7307ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
40 changes: 37 additions & 3 deletions src/hooks/__tests__/useCaptchaVerification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ describe('useCaptchaVerification', () => {
expect(error).toBe('');
});

it('should handle getCaptchaToken successfully', async () => {
it('should handle getCaptchaToken if token doesnt exist successfully', async () => {
const mockExecute = vi.fn().mockResolvedValue('raw-token');
const mockGetValue = vi.fn().mockReturnValue('');
const mockNormalizeCaptchaToken = vi.spyOn(
captchaUtils,
'normalizeCaptchaToken'
Expand All @@ -55,12 +56,13 @@ describe('useCaptchaVerification', () => {

const { result } = renderHook(() => useCaptchaVerification());
Object.defineProperty(result.current.recaptchaRef, 'current', {
value: { execute: mockExecute },
value: { execute: mockExecute, getValue: mockGetValue },
configurable: true
});

const captchaResult = await result.current.getCaptchaToken();

expect(mockGetValue).toHaveBeenCalled();
expect(mockExecute).toHaveBeenCalled();
expect(mockNormalizeCaptchaToken).toHaveBeenCalledWith('raw-token');
expect(mockValidateCaptcha).toHaveBeenCalledWith('normalized-token');
Expand All @@ -71,8 +73,40 @@ describe('useCaptchaVerification', () => {
});
});

it('should handle getCaptchaToken if token exist successfully', async () => {
const mockExecute = vi.fn().mockResolvedValue('raw-token');
const mockGetValue = vi.fn().mockReturnValue('raw-token-current');
const mockNormalizeCaptchaToken = vi.spyOn(
captchaUtils,
'normalizeCaptchaToken'
);
mockNormalizeCaptchaToken.mockReturnValue('normalized-token');

const mockValidateCaptcha = vi.spyOn(validators, 'validateCaptcha');
mockValidateCaptcha.mockReturnValue('');

const { result } = renderHook(() => useCaptchaVerification());
Object.defineProperty(result.current.recaptchaRef, 'current', {
value: { execute: mockExecute, getValue: mockGetValue },
configurable: true
});

const captchaResult = await result.current.getCaptchaToken();

expect(mockGetValue).toHaveBeenCalled();
expect(mockExecute).toHaveBeenCalledTimes(0);
expect(mockNormalizeCaptchaToken).toHaveBeenCalledWith('raw-token-current');
expect(mockValidateCaptcha).toHaveBeenCalledWith('normalized-token');
expect(captchaResult).toEqual({
recaptchaError: '',
hasCaptchaSucceeded: true,
captchaToken: 'normalized-token'
});
});

it('should handle getCaptchaToken with validation error', async () => {
const mockExecute = vi.fn().mockResolvedValue('raw-token');
const mockGetValue = vi.fn().mockResolvedValue('');
const mockNormalizeCaptchaToken = vi.spyOn(
captchaUtils,
'normalizeCaptchaToken'
Expand All @@ -84,7 +118,7 @@ describe('useCaptchaVerification', () => {

const { result } = renderHook(() => useCaptchaVerification());
Object.defineProperty(result.current.recaptchaRef, 'current', {
value: { execute: mockExecute },
value: { execute: mockExecute, getValue: mockGetValue },
configurable: true
});
const captchaResult = await result.current.getCaptchaToken();
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useCaptchaVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const useCaptchaVerification = () => {
};

const getCaptchaToken = async (): Promise<VerifyCaptchaResult> => {
const fetchedCaptchaToken = await recaptchaRef.current?.execute();
const currentToken = recaptchaRef.current?.getValue();
const fetchedCaptchaToken =
currentToken || (await recaptchaRef.current?.execute());
const normalizedCaptchaToken = normalizeCaptchaToken(fetchedCaptchaToken);

const recaptchaValidationError = validateCaptchaToken(
Expand Down

0 comments on commit b7307ad

Please sign in to comment.