From 0ef21394e3e784a8eebe79fb942904380be487d2 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Sun, 3 Nov 2019 19:20:22 -0400 Subject: [PATCH] Add ability to mock captcha via process.env.CAPTCHA process.env.NODE_ENV must be set to test --- packages/captcha/src/Captcha.test.ts | 14 ++++++++++++++ packages/captcha/src/Captcha.ts | 10 ++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 packages/captcha/src/Captcha.test.ts diff --git a/packages/captcha/src/Captcha.test.ts b/packages/captcha/src/Captcha.test.ts new file mode 100644 index 0000000..6e3fd71 --- /dev/null +++ b/packages/captcha/src/Captcha.test.ts @@ -0,0 +1,14 @@ +import { getValue } from './Captcha' + +describe('getValue', () => { + it('returns value by default', () => { + expect(getValue('test')).toBe('test') + }) + + it('returns process.env.CAPTCHA value (for testing)', () => { + expect(getValue('1234', { + NODE_ENV: 'test', + CAPTCHA: '5678', + })).toBe('5678') + }) +}) diff --git a/packages/captcha/src/Captcha.ts b/packages/captcha/src/Captcha.ts index 01b68da..b896d91 100644 --- a/packages/captcha/src/Captcha.ts +++ b/packages/captcha/src/Captcha.ts @@ -15,14 +15,20 @@ declare global { } } +/** + * To make it easier to pass captcha in tests + */ +export function getValue(value: string, env = process.env) { + return env.NODE_ENV === 'test' && env.CAPTCHA ? env.CAPTCHA : value +} + export function createCaptcha( value: string, type: CaptchaType, ): Captcha { return { - value, + value: getValue(value), type, timestamp: Date.now(), } - }