blessing-skin-server/resources/assets/tests/components/Captcha.test.tsx
Zephyr Lykos 178664ed83
refactor: migrate toolchain, part 1
- Switched to pnpm
- Updated to typescript 4.9.5
- JSX transforms switched to react-jsx
- Use swc for typescript transforms
- Remove deprecated dependencies
- Cleanup unused types
- Some ESM fixes
- PostCSS config now lives in package.json
- Bump support target

TODO:
- switch to vite or plain esbuild
- jest to vitest / uvu
- jquery to cash-dom
- pure ESM (lodash-es, etc.)
- drop prettier & husky (lint-staged is okay)
- drop wasm cli & xterm.js
- update bootstrap
2023-07-10 19:58:18 +08:00

66 lines
1.7 KiB
TypeScript

import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import Reaptcha from 'reaptcha'
import { t } from '@/scripts/i18n'
import Captcha from '@/components/Captcha'
describe('picture captcha', () => {
it('retrieve value', async () => {
const ref = React.createRef<Captcha>()
const { getByPlaceholderText } = render(<Captcha ref={ref} />)
fireEvent.input(getByPlaceholderText(t('auth.captcha')), {
target: { value: 'abc' },
})
expect(await ref.current?.execute()).toBe('abc')
})
it('refresh on click', () => {
const spy = jest.spyOn(Date, 'now')
const ref = React.createRef<Captcha>()
const { getByAltText } = render(<Captcha ref={ref} />)
fireEvent.click(getByAltText(t('auth.captcha')))
expect(spy).toBeCalled()
})
it('refresh programatically', () => {
const spy = jest.spyOn(Date, 'now')
const ref = React.createRef<Captcha>()
render(<Captcha ref={ref} />)
ref.current?.reset()
expect(spy).toBeCalled()
})
})
describe('recaptcha', () => {
beforeEach(() => {
window.blessing.extra = { recaptcha: 'sitekey', invisible: false }
})
it('retrieve value', async () => {
window.blessing.extra.invisible = true
const spy = jest.spyOn(Reaptcha.prototype, 'execute')
const ref = React.createRef<Captcha>()
render(<Captcha ref={ref} />)
const value = await ref.current?.execute()
expect(spy).toBeCalled()
expect(value).toBe('token')
})
it('refresh programatically', () => {
const spy = jest.spyOn(Reaptcha.prototype, 'reset')
const ref = React.createRef<Captcha>()
render(<Captcha ref={ref} />)
ref.current?.reset()
expect(spy).toBeCalled()
})
})