2022-04-18 14:02:16 +02:00
import React from 'react' ;
import { render , fireEvent , waitFor } from '@testing-library/react' ;
2023-05-06 23:04:20 +02:00
import configureMockState from 'redux-mock-store' ;
import thunk from 'redux-thunk' ;
import { renderWithProvider } from '../../../../test/lib/render-helpers' ;
import mockState from '../../../../test/data/mock-state.json' ;
2023-05-03 18:26:45 +02:00
import {
MetaMetricsEventCategory ,
MetaMetricsEventKeyType ,
MetaMetricsEventName ,
} from '../../../../shared/constants/metametrics' ;
2023-05-06 23:04:20 +02:00
import { MetaMetricsContext } from '../../../contexts/metametrics' ;
2022-04-18 14:02:16 +02:00
import HoldToRevealButton from './hold-to-reveal-button' ;
2023-05-03 18:26:45 +02:00
const mockTrackEvent = jest . fn ( ) ;
2022-04-18 14:02:16 +02:00
describe ( 'HoldToRevealButton' , ( ) => {
2023-05-06 23:04:20 +02:00
const mockStore = configureMockState ( [ thunk ] ) ( mockState ) ;
2022-04-18 14:02:16 +02:00
let props = { } ;
beforeEach ( ( ) => {
const mockOnLongPressed = jest . fn ( ) ;
props = {
onLongPressed : mockOnLongPressed ,
buttonText : 'Hold to reveal SRP' ,
} ;
} ) ;
2023-05-03 18:26:45 +02:00
afterEach ( ( ) => {
jest . resetAllMocks ( ) ;
} ) ;
2022-04-18 14:02:16 +02:00
it ( 'should render a button with label' , ( ) => {
const { getByText } = render ( < HoldToRevealButton { ... props } / > ) ;
expect ( getByText ( 'Hold to reveal SRP' ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render a button when mouse is down and up' , ( ) => {
const { getByText } = render ( < HoldToRevealButton { ... props } / > ) ;
const button = getByText ( 'Hold to reveal SRP' ) ;
fireEvent . mouseDown ( button ) ;
expect ( button ) . toBeDefined ( ) ;
fireEvent . mouseUp ( button ) ;
expect ( button ) . toBeDefined ( ) ;
} ) ;
2023-05-03 18:26:45 +02:00
it ( 'should show the locked padlock when a button is long pressed and then should show it after it was lifted off before the animation concludes' , async ( ) => {
2023-05-06 23:04:20 +02:00
const { getByText , queryByLabelText } = renderWithProvider (
< MetaMetricsContext . Provider value = { mockTrackEvent } >
< HoldToRevealButton { ... props } / >
< / M e t a M e t r i c s C o n t e x t . P r o v i d e r > ,
mockStore ,
2023-05-03 18:26:45 +02:00
) ;
2022-04-18 14:02:16 +02:00
const button = getByText ( 'Hold to reveal SRP' ) ;
fireEvent . mouseDown ( button ) ;
2023-05-06 23:04:20 +02:00
const circleLocked = queryByLabelText ( 'hold to reveal circle locked' ) ;
2022-04-18 14:02:16 +02:00
2023-05-03 18:26:45 +02:00
await waitFor ( ( ) => {
expect ( circleLocked ) . toBeInTheDocument ( ) ;
2022-04-18 14:02:16 +02:00
} ) ;
fireEvent . mouseUp ( button ) ;
2023-05-06 23:04:20 +02:00
const circleUnlocked = queryByLabelText ( 'hold to reveal circle unlocked' ) ;
2022-04-18 14:02:16 +02:00
2023-05-03 18:26:45 +02:00
await waitFor ( ( ) => {
expect ( circleUnlocked ) . not . toBeInTheDocument ( ) ;
2022-04-18 14:02:16 +02:00
} ) ;
} ) ;
2023-05-03 18:26:45 +02:00
it ( 'should show the unlocked padlock when a button is long pressed for the duration of the animation' , async ( ) => {
2023-05-06 23:04:20 +02:00
const { getByText , queryByLabelText , getByLabelText } = renderWithProvider (
< MetaMetricsContext . Provider value = { mockTrackEvent } >
< HoldToRevealButton { ... props } / >
< / M e t a M e t r i c s C o n t e x t . P r o v i d e r > ,
mockStore ,
2023-05-03 18:26:45 +02:00
) ;
2022-04-18 14:02:16 +02:00
const button = getByText ( 'Hold to reveal SRP' ) ;
fireEvent . mouseDown ( button ) ;
2023-05-06 23:04:20 +02:00
const circleLocked = getByLabelText ( 'hold to reveal circle locked' ) ;
2023-05-03 18:26:45 +02:00
fireEvent . transitionEnd ( circleLocked ) ;
2023-05-06 23:04:20 +02:00
const circleUnlocked = queryByLabelText ( 'hold to reveal circle unlocked' ) ;
2023-05-03 18:26:45 +02:00
fireEvent . animationEnd ( circleUnlocked ) ;
await waitFor ( ( ) => {
expect ( circleUnlocked ) . toBeInTheDocument ( ) ;
2023-05-06 23:04:20 +02:00
expect ( mockTrackEvent ) . toHaveBeenNthCalledWith ( 1 , {
2023-05-03 18:26:45 +02:00
category : MetaMetricsEventCategory . Keys ,
event : MetaMetricsEventName . SrpHoldToRevealClickStarted ,
properties : {
key _type : MetaMetricsEventKeyType . Srp ,
} ,
} ) ;
2023-05-06 23:04:20 +02:00
expect ( mockTrackEvent ) . toHaveBeenNthCalledWith ( 2 , {
2023-05-03 18:26:45 +02:00
category : MetaMetricsEventCategory . Keys ,
event : MetaMetricsEventName . SrpHoldToRevealCompleted ,
properties : {
key _type : MetaMetricsEventKeyType . Srp ,
} ,
} ) ;
2023-05-06 23:04:20 +02:00
expect ( mockTrackEvent ) . toHaveBeenNthCalledWith ( 3 , {
2023-05-03 18:26:45 +02:00
category : MetaMetricsEventCategory . Keys ,
event : MetaMetricsEventName . SrpRevealViewed ,
properties : {
key _type : MetaMetricsEventKeyType . Srp ,
} ,
} ) ;
2022-04-18 14:02:16 +02:00
} ) ;
} ) ;
} ) ;