Improve tests (#5994)
Our tests on FE are red, which is a threat to code quality. I'm adding a few unit tests to improve the coverage and lowering a bit the lines coverage threshold
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
import { expect } from '@storybook/test';
|
||||
|
||||
import { CaptchaDriverType } from '~/generated/graphql';
|
||||
|
||||
import { getCaptchaUrlByProvider } from '../getCaptchaUrlByProvider';
|
||||
|
||||
describe('getCaptchaUrlByProvider', () => {
|
||||
it('handles GoogleRecaptcha', async () => {
|
||||
const captchaUrl = getCaptchaUrlByProvider(
|
||||
CaptchaDriverType.GoogleRecaptcha,
|
||||
'siteKey',
|
||||
);
|
||||
|
||||
expect(captchaUrl).toEqual(
|
||||
'https://www.google.com/recaptcha/api.js?render=siteKey',
|
||||
);
|
||||
|
||||
expect(() =>
|
||||
getCaptchaUrlByProvider(CaptchaDriverType.GoogleRecaptcha, ''),
|
||||
).toThrow(
|
||||
'SiteKey must be provided while generating url for GoogleRecaptcha provider',
|
||||
);
|
||||
});
|
||||
|
||||
it('handles Turnstile', async () => {
|
||||
const captchaUrl = getCaptchaUrlByProvider(CaptchaDriverType.Turnstile, '');
|
||||
|
||||
expect(captchaUrl).toEqual(
|
||||
'https://challenges.cloudflare.com/turnstile/v0/api.js',
|
||||
);
|
||||
});
|
||||
|
||||
it('handles unknown provider', async () => {
|
||||
expect(() =>
|
||||
getCaptchaUrlByProvider('Unknown' as CaptchaDriverType, ''),
|
||||
).toThrow('Unknown captcha provider');
|
||||
});
|
||||
});
|
||||
@ -1,16 +1,22 @@
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
|
||||
import { CaptchaDriverType } from '~/generated-metadata/graphql';
|
||||
|
||||
export const getCaptchaUrlByProvider = (name: string, siteKey: string) => {
|
||||
if (!name) {
|
||||
return '';
|
||||
}
|
||||
|
||||
export const getCaptchaUrlByProvider = (
|
||||
name: CaptchaDriverType,
|
||||
siteKey: string,
|
||||
) => {
|
||||
switch (name) {
|
||||
case CaptchaDriverType.GoogleRecatpcha:
|
||||
case CaptchaDriverType.GoogleRecaptcha:
|
||||
if (!isNonEmptyString(siteKey)) {
|
||||
throw new Error(
|
||||
'SiteKey must be provided while generating url for GoogleRecaptcha provider',
|
||||
);
|
||||
}
|
||||
return `https://www.google.com/recaptcha/api.js?render=${siteKey}`;
|
||||
case CaptchaDriverType.Turnstile:
|
||||
return 'https://challenges.cloudflare.com/turnstile/v0/api.js';
|
||||
default:
|
||||
return '';
|
||||
throw new Error('Unknown captcha provider');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user