Add ability to mock captcha via process.env.CAPTCHA

process.env.NODE_ENV must be set to test
This commit is contained in:
Jerko Steiner 2019-11-03 19:20:22 -04:00
parent a9f37103bd
commit 0ef21394e3
2 changed files with 22 additions and 2 deletions

View File

@ -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')
})
})

View File

@ -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(),
}
}