use-recaptcha.spec.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import '../../helpers/bootstrap-3'
  2. import { useRecaptcha } from '@/shared/hooks/use-recaptcha'
  3. import * as ReactGoogleRecaptcha from 'react-google-recaptcha'
  4. const ReCaptcha = () => {
  5. const { ref: recaptchaRef, getReCaptchaToken } = useRecaptcha()
  6. const handleClick = async () => {
  7. await getReCaptchaToken()
  8. }
  9. return (
  10. <>
  11. <ReactGoogleRecaptcha.ReCAPTCHA
  12. ref={recaptchaRef}
  13. size="invisible"
  14. sitekey="123456"
  15. badge="inline"
  16. />
  17. <button onClick={handleClick}>Click</button>
  18. </>
  19. )
  20. }
  21. describe('useRecaptcha', function () {
  22. it('should reset the captcha', function () {
  23. cy.spy(ReactGoogleRecaptcha.ReCAPTCHA.prototype, 'reset').as('resetSpy')
  24. cy.spy(ReactGoogleRecaptcha.ReCAPTCHA.prototype, 'executeAsync').as(
  25. 'executeAsyncSpy'
  26. )
  27. cy.mount(<ReCaptcha />)
  28. cy.findByRole('button', { name: /click/i }).click()
  29. cy.get('@resetSpy').should('have.been.calledOnce')
  30. cy.get('@executeAsyncSpy').should('have.been.calledOnce')
  31. })
  32. })